Bagikan melalui


Editing Code (Visual C#)

The Visual C# Code Editor is a word processor for writing source code. Just as Microsoft Word provides extensive support for sentences, paragraphs, and grammar, the C# Code Editor does the same for C# syntax and the .NET Framework. This support can be grouped into five main categories:

  • IntelliSense: Continually updated basic documentation on .NET Framework classes and methods as you type them in the editor, and automatic code generation.

  • Refactoring: Intelligent restructuring of your code base as it evolves over the course of a development project.

  • Code snippets: A library you can browse that contains frequently repeated code patterns.

  • Wavy underlines: Visual notifications of misspelled words, semantic errors, erroneous syntax, and warning situations as you type.

  • Readability aids: Outlining, code formatting, and colorization.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

IntelliSense

IntelliSense is the name of a set of related features that are designed to minimize the time that you spend looking for help and to help you enter code more accurately and efficiently. These features all provide basic information about language keywords, .NET Framework types, and method signatures as you type them in the editor. The information is displayed in ToolTips, list boxes, and smart tags.

Note

Many of the features in IntelliSense are shared with other Visual Studio languages and are documented with illustrations in the Coding Aids node of the MSDN library. The following sections provide a brief overview of IntelliSense, with links to the complete documentation.

Completion Lists

As you enter source code in the editor, IntelliSense displays a list box that contains all the C# keywords and .NET Framework classes. If it finds a match in the list box for the name that you are typing, it selects the item. If the selected item is what you want, you can just press TAB and IntelliSense will finish entering the name or keyword for you. For more information, see Completion Lists in C#.

Completion List in C#

Quick Info

When you hover the pointer over a .NET Framework type, IntelliSense will display a Quick Info ToolTip that contains basic documentation about that type. For more information, see Quick Info.

List Members

When you enter a .NET Framework type into the Code Editor, and then type the dot operator (.), IntelliSense displays a list box that contains the members of that type. When you make a selection and press TAB, IntelliSense enters the member name. For more information, see List Members.

Parameter Info

When you enter a method name in the Code Editor, and then type an opening parentheses, IntelliSense will display a Parameter Info ToolTip that shows the order and types of the method's parameters. If the method is overloaded, you can scroll down through all the overloaded signatures. For more information, see Parameter Info.

Parameter Info with Overloads Image

Add usings/Add Qualifier

Sometimes you may try to create an instance of a .NET Framework class without a sufficiently qualified name. When this occurs, IntelliSense displays a smart tag after the unresolved identifier. When you click the smart tag, IntelliSense displays a list of using directives or qualifiers that will enable the identifier to be resolved. When you select one from the list, IntelliSense either adds the directive to the top of the source code file or inserts the qualifier in front of the unresolved identifier. For more information, see Add using.

Refactoring

As a code base grows and evolves over the course of a development project, it is sometimes desirable to make changes in order to make it more readable to humans, or more portable. For example, you may want to split some methods up into smaller methods, or change method parameters, or rename identifiers. The Refactoring feature, which you can find by right-clicking in the Code Editor, does all this in a way that is much more convenient, intelligent, and complete than traditional tools such as search and replace. For more information, see Refactoring.

Code Snippets

Code snippets are small units of typically used C# source code that you can enter accurately and quickly with only several keystrokes. The code snippet menu is accessed by right-clicking in the Code Editor. You can browse from among the many snippets provided with Visual C#, and you can also create your own. For more information, see Code Snippets (C#).

Wavy underlines

Wavy underlines give you instant feedback on errors in your code as you type. A red wavy underline identifies a syntax error, such as a missing semicolon or mismatched braces, or a semantic error, such as the attempt to assign a string literal to a variable of type int. A green wavy underline identifies a potential compiler warning, and blue identifies compiler errors. The following illustration shows a red wavy underline:

Wavy underline showing declaration error

Readability Aids

Code Formatting

The C# code editor automatically formats code when you press the ; or } key, or when you paste code into the editor. To manually invoke code formatting, click the Format Document or Format Selection from the Edit, Advanced menu. You can change code formatting behavior by modifying options in the Formatting, C#, Text Editor, Options Dialog Box and the Advanced, C#, Text Editor, Options Dialog Box.

Outlining

The Code Editor automatically treats namespaces, classes, and methods as regions that you can collapse in order to make other parts of the source code file easier to find and read. You can also create your own collapsible regions by surrounding code with the #region and #endregion directives.

Collapsed methods

Colorization

The editor gives different colors to the various categories of identifiers in a C# source code file. For more information, see Code Colorization.

See Also

Other Resources

Using the Visual C# IDE

Change History

Date

History

Reason

July 2008

Added text about wavy underlines and semantic errors.

SP1 feature change.