Partager via


Learn to use the code editor with C#

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

In this 10-minute introduction to the code editor in Visual Studio, we'll add code to a file to look at some of the ways that Visual Studio makes writing, navigating, and understanding C# code easier.

Tip

If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

This article assumes you're already familiar with C#. If you aren't, we suggest you look at a tutorial such as Get started with C# and ASP.NET Core in Visual Studio first.

Tip

To follow along with this article, make sure you have the C# settings selected for Visual Studio. For 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 some code to it.

  1. Open Visual Studio.

  2. From the File menu on the menu bar, choose New > File, or press Ctrl+N.

  3. In the New File dialog box, under the General category, choose Visual C# Class, and then choose Open.

    A new file opens in the editor with the skeleton of a C# class. (Notice that we don't have to create a full Visual Studio project to gain some of the benefits that the code editor offers; all you need is a code file!)

    Screenshot of a C# code file in Visual Studio.

Use code snippets

Visual Studio provides useful code snippets that you can use to quickly and easily generate commonly used code blocks. Code snippets are available for different programming languages including C#, Visual Basic, and C++.

Let's add the C# void Main snippet to our file.

The list includes snippets for creating a class, a constructor, a for loop, an if or switch statement, and more.

Comment out code

Collapse code blocks

We don't want to see the empty constructor that was generated for Class1, so to unclutter our view of the code, let's collapse it. Choose the small gray box with the minus sign inside it in the margin of the first line of the constructor. Or, if you prefer to use the keyboard, place the cursor anywhere in the constructor code and press Ctrl+M, Ctrl+M.

View symbol definitions

Use IntelliSense to complete words

Refactor a name

Next steps

See also