Telerik® JustCode™ by Progress

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:

Print List Using Templates 1

Define a variable that will generate random numbers for the collection.

Print List Using Templates 2

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; _ < _; _++) { }'".

Print List Using Templates 3

Telerik® JustCode™ inserts the code fragment for you.

Print List Using Templates 4

Notice the help window that appears in the lower right corner of Visual Studio code editor.

Print List Using Templates 5

Use Tab to navigate to the next variable in the code fragment.

Use the arrow keys to choose from different variable names suggested by Telerik® 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:

Print List Using Templates 6

Add method for printing the numbers.

Print List Using Templates 7

In the method body insert "foreach (_ _ in _) { }" template.

Print List Using Templates 8

Telerik® 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.

Print List Using Templates 9

Accept the template by pressing Enter and in its body insert "Console.WriteLine(_)" template.

Print List Using Templates 10

Telerik® 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.

Print List Using Templates 11

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.

See Also

Other Resources