Identifier names - rules and conventions - C# (original) (raw)

An identifier 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:

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 named if. These verbatim identifiers are primarily for interoperability with identifiers declared in other languages.

For a complete definition of valid identifiers, see the Identifiers 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 use PascalCase for type names, namespaces, and all public members. In addition, the dotnet/docs team uses the following conventions, adopted from the .NET Runtime team's coding style:

Tip

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

In the following examples, guidance pertaining to elements marked public is also applicable when working with protected and protected internal elements, all of which are intended to be visible to external callers.

Pascal case

Use pascal casing ("PascalCasing") when naming a class, interface, struct, or delegate 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 an interface, use pascal casing in addition to prefixing the name with an I. This prefix clearly indicates to consumers that it's an interface.

public interface IWorkerQueue
{
}

When naming public 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, see Positional syntax for property definition.

Camel case

Use camel casing ("camelCasing") when naming private or internal 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 with static fields that are private or internal, use the s_ prefix and for thread static use t_.

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 about generic type parameters in the C# programming guide.

public interface ISessionChannel<TSession> { /*...*/ }  
public delegate TOutput Converter<TInput, TOutput>(TInput from);  
public class List<T> { /*...*/ }  
public int IComparer<T>() => 0;  
public delegate bool Predicate<T>(T item);  
public struct Nullable<T> where T : struct { /*...*/ }  
public interface ISessionChannel<TSession>  
{  
    TSession Session { get; }  
}  

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

var currentPerformanceCounterCategory = new System.Diagnostics.  
    PerformanceCounterCategory();