Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

C# identifier naming rules and conventions

  • 2023-12-15
Feedback

In this article

Anidentifier is the name you assign to a type (class, interface, struct, delegate, or enum), member, variable, or namespace.

Naming rules

Valid identifiers must follow these rules. The C# compiler produces an error for any identifier that doesn't follow these rules:

  • Identifiers must start with a letter or underscore (_).
  • Identifiers can contain Unicode letter characters, decimal digit characters, Unicode connecting characters, Unicode combining characters, or Unicode formatting characters. For more information on Unicode categories, see theUnicode Category Database.

You can declare identifiers that match C# keywords by using the@ prefix on the identifier. The@ isn't part of the identifier name. For example,@if declares an identifier namedif. Theseverbatim identifiers are primarily for interoperability with identifiers declared in other languages.

For a complete definition of valid identifiers, see theIdentifiers article in the C# Language Specification.

Important

The C# language specification only allows letter (Lu, Ll, Lt, Lm, or Nl), digit (Nd), connecting (Pc), combining (Mn or Mc), and formatting (Cf) categories. Anything outside that is automatically replaced using_. This might impact certain Unicode characters.

Naming conventions

In addition to the rules, conventions for identifier names are used throughout the .NET APIs. These conventions provide consistency for names, but the compiler doesn't enforce them. You're free to use different conventions in your projects.

By convention, C# programs usePascalCase for type names, namespaces, and all public members. In addition, thedotnet/docs team uses the following conventions, adopted from the.NET Runtime team's coding style:

  • Interface names start with a capitalI.

  • Attribute types end with the wordAttribute.

  • Enum types use a singular noun for nonflags, and a plural noun for flags.

  • Identifiers shouldn't contain two consecutive underscore (_) characters. Those names are reserved for compiler-generated identifiers.

  • Use meaningful and descriptive names for variables, methods, and classes.

  • Prefer clarity over brevity.

  • Use PascalCase for class names and method names.

  • Use camelCase for method parameters and local variables.

  • Use PascalCase for constant names, both fields and local constants.

  • Private instance fields start with an underscore (_) and the remaining text is camelCased.

  • Static fields start withs_. This convention isn't the default Visual Studio behavior, nor part of theFramework design guidelines, but isconfigurable in editorconfig.

  • Avoid using abbreviations or acronyms in names, except for widely known and accepted abbreviations.

  • Use meaningful and descriptive namespaces that follow the reverse domain name notation.

  • Choose assembly names that represent the primary purpose of the assembly.

  • Avoid using single-letter names, except for simple loop counters. Also, syntax examples that describe the syntax of C# constructs often use the following single-letter names that match the convention used in theC# language specification. Syntax examples are an exception to the rule.

    • UseS for structs,C for classes.
    • UseM for methods.
    • Usev for variables,p for parameters.
    • User forref parameters.

Tip

You can enforce naming conventions that concern capitalization, prefixes, suffixes, and word separators by usingcode-style naming rules.

In the following examples, guidance pertaining to elements markedpublic is also applicable when working withprotected andprotected internal elements, all of which are intended to be visible to external callers.

Pascal case

Use pascal casing ("PascalCasing") when naming aclass,interface,struct, ordelegate type.

public class DataService{}
public record PhysicalAddress(    string Street,    string City,    string StateOrProvince,    string ZipCode);
public struct ValueCoordinate{}
public delegate void DelegateType(string message);

When naming aninterface, use pascal casing in addition to prefixing the name with anI. This prefix clearly indicates to consumers that it's aninterface.

public interface IWorkerQueue{}

When namingpublic members of types, such as fields, properties, events, use pascal casing. Also, use pascal casing for all methods and local functions.

public class ExampleEvents{    // A public field, these should be used sparingly    public bool IsValid;    // An init-only property    public IWorkerQueue WorkerQueue { get; init; }    // An event    public event Action EventProcessing;    // Method    public void StartEventProcessing()    {        // Local function        static int CountQueueItems() => WorkerQueue.Count;        // ...    }}

When writing positional records, use pascal casing for parameters as they're the public properties of the record.

public record PhysicalAddress(    string Street,    string City,    string StateOrProvince,    string ZipCode);

For more information on positional records, seePositional syntax for property definition.

Camel case

Use camel casing ("camelCasing") when namingprivate orinternal fields and prefix them with_. Use camel casing when naming local variables, including instances of a delegate type.

public class DataService{    private IWorkerQueue _workerQueue;}

Tip

When editing C# code that follows these naming conventions in an IDE that supports statement completion, typing_ will show all of the object-scoped members.

When working withstatic fields that areprivate orinternal, use thes_ prefix and for thread static uset_.

public class DataService{    private static IWorkerQueue s_workerQueue;    [ThreadStatic]    private static TimeSpan t_timeSpan;}

When writing method parameters, use camel casing.

public T SomeMethod<T>(int someNumber, bool isValid){}

For more information on C# naming conventions, see the.NET Runtime team's coding style.

Type parameter naming guidelines

The following guidelines apply to type parameters on generic type parameters. Type parameters are the placeholders for arguments in a generic type or a generic method. You can read more aboutgeneric type parameters in the C# programming guide.

  • Do name generic type parameters with descriptive names, unless a single letter name is completely self explanatory and a descriptive name wouldn't add value.

    public interface ISessionChannel<TSession> { /*...*/ }public delegate TOutput Converter<TInput, TOutput>(TInput from);public class List<T> { /*...*/ }
  • Consider usingT as the type parameter name for types with one single letter type parameter.

    public int IComparer<T>() => 0;public delegate bool Predicate<T>(T item);public struct Nullable<T> where T : struct { /*...*/ }
  • Do prefix descriptive type parameter names with "T".

    public interface ISessionChannel<TSession>{    TSession Session { get; }}
  • Consider indicating constraints placed on a type parameter in the name of parameter. For example, a parameter constrained toISession might be calledTSession.

The code analysis ruleCA1715 can be used to ensure that type parameters are named appropriately.

Extra naming conventions

  • Examples that don't includeusing directives, use namespace qualifications. If you know that a namespace is imported by default in a project, you don't have to fully qualify the names from that namespace. Qualified names can be broken after a dot (.) if they're too long for a single line, as shown in the following example.

    var currentPerformanceCounterCategory = new System.Diagnostics.    PerformanceCounterCategory();
  • You don't have to change the names of objects that were created by using the Visual Studio designer tools to make them fit other guidelines.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo