Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
66
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 55: Naming Conventions

This topic outlines some basic naming conventions used when writing in the C# language. Like all conventions, they are not enforced by the compiler, but will ensure readability between developers.

For comprehensive .NET framework design guidelines, see docs.microsoft.com/dotnet/standard/design-guidelines.

Section 55.1: Capitalization conventions

The following terms describe di erent ways to case identifiers.

Pascal Casing

The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three or more characters. For example: BackColor

Camel Casing

The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: backColor

Uppercase

All letters in the identifier are capitalized. For example: IO

Rules

When an identifier consists of multiple words, do not use separators, such as underscores ("_") or hyphens ("-"), between words. Instead, use casing to indicate the beginning of each word.

The following table summarizes the capitalization rules for identifiers and provides examples for the di erent types of identifiers:

Identifier

Case

Example

Local variable

Camel carName

Class

Pascal AppDomain

Enumeration type

Pascal ErrorLevel

Enumeration values

Pascal FatalError

Event

Pascal ValueChanged

Exception class

Pascal WebException

Read-only static field Pascal RedValue

Interface

Pascal IDisposable

Method

Pascal ToString

Namespace

Pascal System.Drawing

Parameter

Camel typeName

Property

Pascal BackColor

More information can be found on MSDN.

GoalKicker.com – C# Notes for Professionals

285

Section 55.2: Enums

Use a singular name for most Enums

public enum Volume

{

Low,

Medium, High

}

Use a plural name for Enum types that are bit fields

[Flags]

public enum MyColors

{

Yellow = 1,

Green = 2,

Red = 4,

Blue = 8

}

Note: Always add the FlagsAttribute to a bit field Enum type.

Do not add 'enum' as a su x

public enum VolumeEnum // Incorrect

Do not use the enum name in each entry

public enum Color

{

ColorBlue, // Remove Color, unnecessary

ColorGreen,

}

Section 55.3: Interfaces

Interfaces should be named with nouns or noun phrases, or adjectives that describe behaviour. For example

IComponent uses a descriptive noun, ICustomAttributeProvider uses a noun phrase and IPersistable uses an adjective.

Interface names should be prefixed with the letter I, to indicate that the type is an interface, and Pascal case should be used.

Below are correctly named interfaces:

public interface IServiceProvider public interface IFormatable

Section 55.4: Exceptions

Add 'exception' as a su x

Custom exception names should be su xed with "-Exception".

Below are correctly named exceptions:

GoalKicker.com – C# Notes for Professionals

286

public class MyCustomException : Exception public class FooException : Exception

Section 55.5: Private fields

There are two common conventions for private fields: camelCase and _camelCaseWithLeadingUnderscore.

Camel case

public class Rational

{

private readonly int numerator; private readonly int denominator;

public Rational(int numerator, int denominator)

{

// "this" keyword is required to refer to the class-scope field this.numerator = numerator;

this.denominator = denominator;

}

}

Camel case with underscore

public class Rational

{

private readonly int _numerator; private readonly int _denominator;

public Rational(int numerator, int denominator)

{

// Names are unique, so "this" keyword is not required

_numerator = numerator; _denominator = denominator;

}

}

Section 55.6: Namespaces

The general format for namespaces is:

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>].

Examples include:

Fabrikam.Math

Litware.Security

Prefixing namespace names with a company name prevents namespaces from di erent companies from having the same name.

GoalKicker.com – C# Notes for Professionals

287