How do I code in code blocks

Using the Code Editor with C #

  • 3 minutes to read

This ten-minute introduction to the Visual Studio Code Editor adds code to a file to demonstrate how Visual Studio makes it easy to write, navigate, and understand C # code.


If you don't already have Visual Studio installed, you can download it for free from the Visual Studio Downloads page.


If you don't already have Visual Studio installed, you can download it for free from the Visual Studio Downloads page.

This article assumes that you are already familiar with C #. If that's not the case, the first thing to do is to watch tutorials like Getting Started with C # and ASP.NET Core in Visual Studio.


To complete the operations in this article, make sure that you have selected C # settings for Visual Studio. For more information about selecting settings for the Integrated Development Environment (IDE), see Select environment settings.

Create a new code file

Start by creating a new file and adding this code to it.

  1. Open Visual Studio.
  1. Open Visual Studio. Press ESC, or click in the start window Continue without codeto open the development environment.
  1. Select in the menu file on the menu bar New > file or press CTRL+N.

  2. Click in the dialog box new file under the category Generally on the option Visual C # class, and then click to open.

    A new file with the skeleton of a C # class opens in the editor. (Note that you don't need to build a full Visual Studio project to take advantage of the code editor; you just need a code file.)

Use snippets of code

Visual Studio provides useful Code snippets ready that you can use to generate frequently used blocks of code quickly and easily. The code snippets are available for a variety of programming languages, including C #, Visual Basic, and C ++. Add the C # snippet to your file.

  1. Place your cursor just above the last closing curly bracket } in the file and enter the characters (this stands for - don't worry too much if you don't know what this means).

    A pop-up dialog box appears with information about the code snippet.

  2. Press the twice TAB KEYto paste the code snippet.

    The method signature is then added to the file. The Main () method is the entry point for C # applications.

The code snippets available vary depending on the programming language. You can view the code snippets available for your language by clicking To edit > IntelliSense > Insert cutout or by pressing CTRL+K, CTRL+X Press, then select the folder for your language. For C # the list looks like this:

The list contains, among other things, excerpts for creating a class, a constructor, a for loop and an if or switch statement.

Commenting out code

The toolbar - the row of buttons below the menu bar in Visual Studio - can increase your coding productivity. For example, you can toggle the IntelliSense exit mode (IntelliSense is a coding aid that displays, among other things, a list of matching methods), shrink or enlarge a line indent, or comment out code that you don't want compiled. This section comments out code.

  1. Paste the following code in the method body.

  2. Although the variable is not used at this point, it should not be completely deleted because it may be needed later. Instead, comment out these lines. Assign the complete definition of the semicolon that is set at the end and then click the button in the toolbar Comment out the selected lines (Comment out selected lines). If you'd rather use the keyboard, press CTRL+K, CTRL+C..

    The C # comment characters are added to the beginning of each selected line to comment out the code.

Reduce blocks of code

No empty constructor should be displayed for the generated one. So reduce the view of the code so that unnecessary information is hidden. Click the little gray box with the minus sign in the margin next to the first line of the constructor. If you prefer to use the keyboard, place the cursor anywhere in the constructor code and press CTRL+M., CTRL+M..

The block of code is reduced to the first line, followed by an ellipsis (). Now click on the same gray box, this time with a plus sign, to expand the block of code again, or press twice CTRL+M., CTRL+M.. This feature is known as slicing and is particularly useful when you want to collapse long methods or entire classes.

View symbol definitions

The Visual Studio editor makes it easier to check the definition of a type or a method, among other things. For example, you can navigate to the file that contains the definition by clicking anywhere that the icon is referenced, Go to definition select or F12 to press. You can also use the View Definition option. This is a faster option that still has the focus on the file. The definition of type is shown below.

  1. Right-click any representation of and choose from the content menu See definition out. Alternatively, press OLD+F12.

    A pop-up window appears with the definition of the class. You can scroll the pop-up window or even see the definition of another type of code being viewed.

  2. Close the definition window you have viewed by clicking on the small "x" in the upper right corner of the pop-up window.

Using IntelliSense to Complete Words

IntelliSense is an important resource in coding. This gives you information about available members of a type or parameter details for different overloads of a method. You can also use IntelliSense to complete words after enough characters have been entered to make them unique. Add a single line of code to print the ordered strings in the console window. This is the default location for program output.

  1. Under the variable, start typing the following code:

    Then IntelliSense will show you Tooltip to the symbol.

  2. Press the TAB KEYto insert the rest of the word using the IntelliSense word completion feature.

  3. Complete the block of code so that it looks like the following code. You can practice using snippets of code one more time by typing and then typing the TAB KEY Press to generate the code.

Refactoring a name

Nobody succeeds in coding on the first try. So it is very likely that at some point you will want to change the name of a variable or method. Test the Refactoring feature of Visual Studio to rename the variable to.

  1. Place the cursor over the definition of the variable and click in the context menu (right-click) Rename, or press CTRL+R., CTRL+R..

    The dialog box appears Rename displayed in the upper right corner of the editor.

  2. Enter the name you want Words a. Please note that the reference to in the query is also automatically renamed. Check the box Include comments in the dialog box Renamebefore you hit the ENTER to press.

  3. Press the ENTER.

    Both instances of and the reference to in the code comment have been renamed.

Next Steps

See also