You can use Code Templates to insert commonly used code fragments or surround given code fragment with a
meaningful code block.
Inserting code templates is easy and saves you the monotonous writing of for/foreach and other fragments.
In this How To topic you will learn to use code templates in a few minutes. You will create a simple application
that populates a list with random numbers and then prints it on the console. Before you begin, create a
C# Console application and add a new class to it, i.e. PrintListUsingTemplates.cs. Add a default constructor
to the class and leave its body empty for now. Declare a private collection that will store the randomly
generated numbers. At this point your class should look like this:
Define a variable that will generate random numbers for the collection.
Position the caret on a new line and press Alt+End to
show available templates. In order to generate, i.e. 10 random numbers and insert them in the collection, you
can use a for loop. From the menu choose "Insert 'for (int _ = 0; _ < _; _++) { }'".
JustCode inserts the code fragment for you.
Notice the help window that appears in the lower right corner of Visual Studio code editor.
Use Tab to navigate to the next variable in the code fragment.
Use the arrow keys to choose from different variable names suggested by JustCode.
Press Enter to finish with the customization of the code fragment or Esc to accept it as it is.
Name the iteration variable i and define the limit to 10. In the body of the
for loop add the generated random number to the collection.
At this point your class should look like this:
Add method for printing the numbers.
In the method body insert "foreach (_ _ in _) { }" template.
JustCode recognizes the collection to be iterated, the iterator type
and suggests an iterator name: number. A drop down menu with
all available collections will be shown if there are more than one.
Accept the template by pressing Enter and in its body insert
"Console.WriteLine(_)" template.
JustCode suggests printing number in the console and
if this is what you want, press Enter. The last thing to do is calling
the Print method after the collection is populated.
At this point you can execute the application and test it. You have used three different templates
and as there is no difference in using different templates, from now on you can easily apply them
to improve your coding productivity.