- •Foreword
- •Introduction
- •Scope
- •Conformance
- •Normative references
- •Definitions
- •Notational conventions
- •Acronyms and abbreviations
- •General description
- •Language overview
- •Getting started
- •Types
- •Predefined types
- •Conversions
- •Array types
- •Type system unification
- •Variables and parameters
- •Automatic memory management
- •Expressions
- •Statements
- •Classes
- •Constants
- •Fields
- •Methods
- •Properties
- •Events
- •Operators
- •Indexers
- •Instance constructors
- •Destructors
- •Static constructors
- •Inheritance
- •Static classes
- •Partial type declarations
- •Structs
- •Interfaces
- •Delegates
- •Enums
- •Namespaces and assemblies
- •Versioning
- •Extern Aliases
- •Attributes
- •Generics
- •Why generics?
- •Creating and consuming generics
- •Multiple type parameters
- •Constraints
- •Generic methods
- •Anonymous methods
- •Iterators
- •Lexical structure
- •Programs
- •Grammars
- •Lexical grammar
- •Syntactic grammar
- •Grammar ambiguities
- •Lexical analysis
- •Line terminators
- •Comments
- •White space
- •Tokens
- •Unicode escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Boolean literals
- •Integer literals
- •Real literals
- •Character literals
- •String literals
- •The null literal
- •Operators and punctuators
- •Pre-processing directives
- •Conditional compilation symbols
- •Pre-processing expressions
- •Declaration directives
- •Conditional compilation directives
- •Diagnostic directives
- •Region control
- •Line directives
- •Pragma directives
- •Basic concepts
- •Application startup
- •Application termination
- •Declarations
- •Members
- •Namespace members
- •Struct members
- •Enumeration members
- •Class members
- •Interface members
- •Array members
- •Delegate members
- •Member access
- •Declared accessibility
- •Accessibility domains
- •Protected access for instance members
- •Accessibility constraints
- •Signatures and overloading
- •Scopes
- •Name hiding
- •Hiding through nesting
- •Hiding through inheritance
- •Namespace and type names
- •Unqualified name
- •Fully qualified names
- •Automatic memory management
- •Execution order
- •Types
- •Value types
- •The System.ValueType type
- •Default constructors
- •Struct types
- •Simple types
- •Integral types
- •Floating point types
- •The decimal type
- •The bool type
- •Enumeration types
- •Reference types
- •Class types
- •The object type
- •The string type
- •Interface types
- •Array types
- •Delegate types
- •Boxing and unboxing
- •Boxing conversions
- •Unboxing conversions
- •Variables
- •Variable categories
- •Static variables
- •Instance variables
- •Instance variables in classes
- •Instance variables in structs
- •Array elements
- •Value parameters
- •Reference parameters
- •Output parameters
- •Local variables
- •Default values
- •Definite assignment
- •Initially assigned variables
- •Initially unassigned variables
- •Precise rules for determining definite assignment
- •General rules for statements
- •Block statements, checked, and unchecked statements
- •Expression statements
- •Declaration statements
- •If statements
- •Switch statements
- •While statements
- •Do statements
- •For statements
- •Break, continue, and goto statements
- •Throw statements
- •Return statements
- •Try-catch statements
- •Try-finally statements
- •Try-catch-finally statements
- •Foreach statements
- •Using statements
- •Lock statements
- •General rules for simple expressions
- •General rules for expressions with embedded expressions
- •Invocation expressions and object creation expressions
- •Simple assignment expressions
- •&& expressions
- •|| expressions
- •! expressions
- •?: expressions
- •Anonymous method expressions
- •Yield statements
- •Variable references
- •Atomicity of variable references
- •Conversions
- •Implicit conversions
- •Identity conversion
- •Implicit numeric conversions
- •Implicit enumeration conversions
- •Implicit reference conversions
- •Boxing conversions
- •Implicit type parameter conversions
- •Implicit constant expression conversions
- •User-defined implicit conversions
- •Explicit conversions
- •Explicit numeric conversions
- •Explicit enumeration conversions
- •Explicit reference conversions
- •Unboxing conversions
- •User-defined explicit conversions
- •Standard conversions
- •Standard implicit conversions
- •Standard explicit conversions
- •User-defined conversions
- •Permitted user-defined conversions
- •Evaluation of user-defined conversions
- •User-defined implicit conversions
- •User-defined explicit conversions
- •Anonymous method conversions
- •Method group conversions
- •Expressions
- •Expression classifications
- •Values of expressions
- •Operators
- •Operator precedence and associativity
- •Operator overloading
- •Unary operator overload resolution
- •Binary operator overload resolution
- •Candidate user-defined operators
- •Numeric promotions
- •Unary numeric promotions
- •Binary numeric promotions
- •Member lookup
- •Base types
- •Function members
- •Argument lists
- •Overload resolution
- •Applicable function member
- •Better function member
- •Better conversion
- •Function member invocation
- •Invocations on boxed instances
- •Primary expressions
- •Literals
- •Simple names
- •Invariant meaning in blocks
- •Parenthesized expressions
- •Member access
- •Identical simple names and type names
- •Invocation expressions
- •Method invocations
- •Delegate invocations
- •Element access
- •Array access
- •Indexer access
- •This access
- •Base access
- •Postfix increment and decrement operators
- •The new operator
- •Object creation expressions
- •Array creation expressions
- •Delegate creation expressions
- •The typeof operator
- •The checked and unchecked operators
- •Default value expression
- •Anonymous methods
- •Anonymous method signatures
- •Anonymous method blocks
- •Outer variables
- •Captured outer variables
- •Instantiation of local variables
- •Anonymous method evaluation
- •Implementation example
- •Unary expressions
- •Unary plus operator
- •Unary minus operator
- •Logical negation operator
- •Bitwise complement operator
- •Prefix increment and decrement operators
- •Cast expressions
- •Arithmetic operators
- •Multiplication operator
- •Division operator
- •Remainder operator
- •Addition operator
- •Subtraction operator
- •Shift operators
- •Relational and type-testing operators
- •Integer comparison operators
- •Floating-point comparison operators
- •Decimal comparison operators
- •Boolean equality operators
- •Enumeration comparison operators
- •Reference type equality operators
- •String equality operators
- •Delegate equality operators
- •The is operator
- •The as operator
- •Logical operators
- •Integer logical operators
- •Enumeration logical operators
- •Boolean logical operators
- •Conditional logical operators
- •Boolean conditional logical operators
- •User-defined conditional logical operators
- •Conditional operator
- •Assignment operators
- •Simple assignment
- •Compound assignment
- •Event assignment
- •Expression
- •Constant expressions
- •Boolean expressions
- •Statements
- •End points and reachability
- •Blocks
- •Statement lists
- •The empty statement
- •Labeled statements
- •Declaration statements
- •Local variable declarations
- •Local constant declarations
- •Expression statements
- •Selection statements
- •The if statement
- •The switch statement
- •Iteration statements
- •The while statement
- •The do statement
- •The for statement
- •The foreach statement
- •Jump statements
- •The break statement
- •The continue statement
- •The goto statement
- •The return statement
- •The throw statement
- •The try statement
- •The checked and unchecked statements
- •The lock statement
- •The using statement
- •The yield statement
- •Namespaces
- •Compilation units
- •Namespace declarations
- •Extern alias directives
- •Using directives
- •Using alias directives
- •Using namespace directives
- •Namespace members
- •Type declarations
- •Qualified alias member
- •Classes
- •Class declarations
- •Class modifiers
- •Abstract classes
- •Sealed classes
- •Static classes
- •Class base specification
- •Base classes
- •Interface implementations
- •Class body
- •Partial declarations
- •Class members
- •Inheritance
- •The new modifier
- •Access modifiers
- •Constituent types
- •Static and instance members
- •Nested types
- •Fully qualified name
- •Declared accessibility
- •Hiding
- •this access
- •Reserved member names
- •Member names reserved for properties
- •Member names reserved for events
- •Member names reserved for indexers
- •Member names reserved for destructors
- •Constants
- •Fields
- •Static and instance fields
- •Readonly fields
- •Using static readonly fields for constants
- •Versioning of constants and static readonly fields
- •Volatile fields
- •Field initialization
- •Variable initializers
- •Static field initialization
- •Instance field initialization
- •Methods
- •Method parameters
- •Value parameters
- •Reference parameters
- •Output parameters
- •Parameter arrays
- •Static and instance methods
- •Virtual methods
- •Override methods
- •Sealed methods
- •Abstract methods
- •External methods
- •Method body
- •Method overloading
- •Properties
- •Static and instance properties
- •Accessors
- •Virtual, sealed, override, and abstract accessors
- •Events
- •Field-like events
- •Event accessors
- •Static and instance events
- •Virtual, sealed, override, and abstract accessors
- •Indexers
- •Indexer overloading
- •Operators
- •Unary operators
- •Binary operators
- •Conversion operators
- •Instance constructors
- •Constructor initializers
- •Instance variable initializers
- •Constructor execution
- •Default constructors
- •Private constructors
- •Optional instance constructor parameters
- •Static constructors
- •Destructors
- •Structs
- •Struct declarations
- •Struct modifiers
- •Struct interfaces
- •Struct body
- •Struct members
- •Class and struct differences
- •Value semantics
- •Inheritance
- •Assignment
- •Default values
- •Boxing and unboxing
- •Meaning of this
- •Field initializers
- •Constructors
- •Destructors
- •Static constructors
- •Struct examples
- •Database integer type
- •Database boolean type
- •Arrays
- •Array types
- •The System.Array type
- •Array creation
- •Array element access
- •Array members
- •Array covariance
- •Arrays and the generic IList interface
- •Array initializers
- •Interfaces
- •Interface declarations
- •Interface modifiers
- •Base interfaces
- •Interface body
- •Interface members
- •Interface methods
- •Interface properties
- •Interface events
- •Interface indexers
- •Interface member access
- •Fully qualified interface member names
- •Interface implementations
- •Explicit interface member implementations
- •Interface mapping
- •Interface implementation inheritance
- •Interface re-implementation
- •Abstract classes and interfaces
- •Enums
- •Enum declarations
- •Enum modifiers
- •Enum members
- •The System.Enum type
- •Enum values and operations
- •Delegates
- •Delegate declarations
- •Delegate instantiation
- •Delegate invocation
- •Exceptions
- •Causes of exceptions
- •The System.Exception class
- •How exceptions are handled
- •Common Exception Classes
- •Attributes
- •Attribute classes
- •Attribute usage
- •Positional and named parameters
- •Attribute parameter types
- •Attribute specification
- •Attribute instances
- •Compilation of an attribute
- •Run-time retrieval of an attribute instance
- •Reserved attributes
- •The AttributeUsage attribute
- •The Conditional attribute
- •Conditional Methods
- •Conditional Attribute Classes
- •The Obsolete attribute
- •Unsafe code
- •Unsafe contexts
- •Pointer types
- •Fixed and moveable variables
- •Pointer conversions
- •Pointers in expressions
- •Pointer indirection
- •Pointer member access
- •Pointer element access
- •The address-of operator
- •Pointer increment and decrement
- •Pointer arithmetic
- •Pointer comparison
- •The sizeof operator
- •The fixed statement
- •Stack allocation
- •Dynamic memory allocation
- •Generics
- •Generic class declarations
- •Type parameters
- •The instance type
- •Members of generic classes
- •Static fields in generic classes
- •Static constructors in generic classes
- •Accessing protected members
- •Overloading in generic classes
- •Parameter array methods and type parameters
- •Overriding and generic classes
- •Operators in generic classes
- •Nested types in generic classes
- •Generic struct declarations
- •Generic interface declarations
- •Uniqueness of implemented interfaces
- •Explicit interface member implementations
- •Generic delegate declarations
- •Constructed types
- •Type arguments
- •Open and closed types
- •Base classes and interfaces of a constructed type
- •Members of a constructed type
- •Accessibility of a constructed type
- •Conversions
- •Using alias directives
- •Generic methods
- •Generic method signatures
- •Virtual generic methods
- •Calling generic methods
- •Inference of type arguments
- •Using a generic method with a delegate
- •Constraints
- •Satisfying constraints
- •Member lookup on type parameters
- •Type parameters and boxing
- •Conversions involving type parameters
- •Iterators
- •Iterator blocks
- •Enumerator interfaces
- •Enumerable interfaces
- •Yield type
- •This access
- •Enumerator objects
- •The MoveNext method
- •The Current property
- •The Dispose method
- •Enumerable objects
- •The GetEnumerator method
- •Implementation example
- •Lexical grammar
- •Line terminators
- •White space
- •Comments
- •Unicode character escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Operators and punctuators
- •Pre-processing directives
- •Syntactic grammar
- •Basic concepts
- •Types
- •Expressions
- •Statements
- •Classes
- •Structs
- •Arrays
- •Interfaces
- •Enums
- •Delegates
- •Attributes
- •Generics
- •Grammar extensions for unsafe code
- •Undefined behavior
- •Implementation-defined behavior
- •Unspecified behavior
- •Other Issues
- •Capitalization styles
- •Pascal casing
- •Camel casing
- •All uppercase
- •Capitalization summary
- •Word choice
- •Namespaces
- •Classes
- •Interfaces
- •Enums
- •Static fields
- •Parameters
- •Methods
- •Properties
- •Events
- •Case sensitivity
- •Avoiding type name confusion
- •Documentation Comments
- •Introduction
- •Recommended tags
- •<code>
- •<example>
- •<exception>
- •<list>
- •<para>
- •<param>
- •<paramref>
- •<permission>
- •<remarks>
- •<returns>
- •<seealso>
- •<summary>
- •<value>
- •Processing the documentation file
- •ID string format
- •ID string examples
- •An example
- •C# source code
- •Resulting XML
C# LANGUAGE SPECIFICATION
Type |
Case |
Notes |
|
|
|
Class |
PascalCase |
|
|
|
|
Class, attribute |
PascalCase |
Has a suffix of Attribute |
|
|
|
Class, exception |
PascalCase |
Has a suffix of Exception |
|
|
|
Constant |
PascalCase |
|
|
|
|
Enum type |
PascalCase |
|
|
|
|
Enum value |
PascalCase |
|
|
|
|
Event |
PascalCase |
|
|
|
|
Field, non-public instance |
camelCase |
|
|
|
|
Field, public instance |
PascalCase |
Rarely used (use a property instead) |
|
|
|
Interface |
PascalCase |
Has a prefix of I |
|
|
|
Local variable |
camelCase |
|
|
|
|
Method |
PascalCase |
|
|
|
|
Namespace |
PascalCase |
|
|
|
|
Parameter |
camelCase |
|
|
|
|
Property |
PascalCase |
|
|
|
|
1
2C.2 Word choice
3• Do avoid using class names duplicated in heavily used namespaces. For example, don’t use the
4following for a class name.
5 |
System |
Collections |
Forms |
UI |
6• Do not use abbreviations in identifiers.
7• If you must use abbreviations, do use camelCase for any abbreviation containing more than two
8characters, even if this is not the usual abbreviation.
9C.3 Namespaces
10The general rule for namespace naming is CompanyName.TechnologyName.
11• Do avoid the possibility of two published namespaces having the same name, by prefixing namespace
12names with a company name or other well-established brand. For example, Microsoft.Office for the
13Office Automation classes provided by Microsoft.
14• Do use PascalCase, and separate logical components with periods (as in
15Microsoft.Office.PowerPoint). If your brand employs non-traditional casing, do follow the
16casing defined by your brand, even if it deviates from normal namespace casing (for example,
17NeXT.WebObjects, and ee.cummings).
18• Do use plural namespace names where appropriate. For example, use System.Collections rather
19than System.Collection. Exceptions to this rule are brand names and abbreviations. For example,
20use System.IO not System.IOs.
21• Do not have namespaces and classes with the same name.
22C.4 Classes
23• Do name classes with nouns or noun phrases.
464
Annex C Naming guidelines
1• Do use PascalCase.
2• Do use sparingly, abbreviations in class names.
3• Do not use any prefix (such as “C”, for example). Where possible, avoid starting with the letter “I”,
4since that is the recommended prefix for interface names. If you must start with that letter, make sure the
5second character is lowercase, as in IdentityStore.
6• Do not use any underscores.
7public class FileStream { … }
8public class Button { … }
9public class String { … }
10C.5 Interfaces
11• Do name interfaces with nouns or noun phrases, or adjectives describing behavior. For example,
12IComponent (descriptive noun), ICustomAttributeProvider (noun phrase), and IPersistable
13(adjective).
14• Do use PascalCase.
15• Do use sparingly, abbreviations in interface names.
16• Do not use any underscores.
17• Do prefix interface names with the letter “I”, to indicate that the type is an interface.
18• Do use similar names when defining a class/interface pair where the class is a standard implementation
19of the interface. The names should differ only by the “I” prefix in the interface name. This approach is
20used for the interface IComponent and its standard implementation, Component.
21public interface IComponent { … }
22public class Component : IComponent { … }
23public interface IServiceProvider{ … }
24public interface IFormatable { … }
25C.6 Enums
26• Do use PascalCase for enums.
27• Do use PascalCase for enum value names.
28• Do use sparingly, abbreviations in enum names.
29• Do not use a family-name prefix on enum.
30• Do not use any “Enum” suffix on enum types.
31• Do use a singular name for enums.
32• Do use a plural name for bit fields.
33• Do define enumerated values using an enum if they are used in a parameter or property. This gives
34development tools a chance at knowing the possible values for a property or parameter.
35public enum FileMode
36{
37 |
Create, |
38 |
CreateNew, |
39 |
Open, |
40 |
OpenOrCreate, |
41Truncate
42}
43• Do use the Flags custom attribute if the numeric values are meant to be bitwise ORed together.
465
C# LANGUAGE SPECIFICATION
1[Flags]
2public enum Bindings
3{
4 |
CreateInstance, |
5 |
DefaultBinding, |
6 |
ExcatBinding, |
7 |
GetField, |
8 |
GetProperty, |
9 |
IgnoreCase, |
10 |
InvokeMethod, |
11 |
NonPublic, |
12 |
OABinding, |
13 |
SetField, |
14 |
SetProperty, |
15Static
16}
17• Do use int as the underlying type of an enum. (An exception to this rule is if the enum represents flags
18and there are more than 32 flags, or the enum might grow to that many flags in the future, or the type
19needs to be different from int for backward compatibility.)
20• Do use enums only if the value can be completely expressed as a set of bit flags. Do not use enums for
21open sets (such as operating system version).
22C.7 Static fields
23• Do name static members with nouns, noun phrases, or abbreviations for nouns.
24• Do name static members using PascalCase.
25• Do not use Hungarian-type prefixes on static member names.
26C.8 Parameters
27• Do use descriptive names such that a parameter’s name and type clearly imply its meaning.
28• Do name parameters using camelCase.
29• Do prefer names based on a parameter’s meaning, to names based on the parameter’s type. It is likely
30that development tools will provide the information about type in a convenient way, so the parameter
31name can be put to better use describing semantics rather than type.
32• Do not reserve parameters for future use. If more data is need in the next version, a new overload can be
33added.
34• Do not use Hungarian-type prefixes.
35Type GetType(string typeName)
36string Format(string format, object[] args)
37C.9 Methods
38• Do name methods with verbs or verb phrases.
39• Do name methods with PascalCase.
40 RemoveAll() GetCharArray() Invoke()
41C.10 Properties
42• Do name properties using noun or noun phrases.
43• Do name properties with PascalCase.
44• Consider having a property with the same as a type. When declaring a property with the same name as a
45type, also make the type of the property be that type. In other words, the following is okay
466
Annex C Naming guidelines
1public enum Color { … }
2public class Control
3{
4public Color Color { get { … } set { … } }
5}
6but this is not
7public enum Color { … }
8public class Control
9{
10public int Color { get { … } set { … } }
11}
12In the latter case, it will not be possible to refer to the members of the Color enum because Color.Xxx
13will be interpreted as being a member access that first gets the value of the Color property (of type
14int) and then accesses a member of that value (which would have to be an instance member of
15System.Int32).
16C.11 Events
17• Do name event handlers with the EventHandler suffix.
18public delegate void MouseEventHandler(object sender, MouseEvent e);
19• Do use two parameters named sender and e. The sender parameter represents the object that raised the
20event, and this parameter is always of type object, even if it is possible to employ a more specific type.
21The state associated with the event is encapsulated in an instance e of an event class. Use an appropriate
22and specific event class for its type.
23public delegate void MouseEventHandler(object sender, MouseEvent e);
24• Do name event argument classes with the EventArgs suffix.
25public class MouseEventArgs : EventArgs
26{
27 |
int x; |
28 |
int y; |
29 |
public MouseEventArgs(int x, int y) { |
30 |
this.x = x; |
31 |
this.y = y; |
32 |
} |
33 |
public int X { get { return x; } } |
34public int Y { get { return y; } }
35}
36• Do name event names that have a concept of preand post-operation using the present and past tense (do
37not use BeforeXxx/AfterXxx pattern). For example, a close event that could be canceled would have a
38Closing and Closed event.
39public event ControlEventHandler ControlAdded;
40• Consider naming events with a verb.
41C.12 Case sensitivity
42• Don’t use names that require case sensitivity. Components might need to be usable from both case-
43sensitive and case-insensitive languages. Since case-insensitive languages cannot distinguish between
44two names within the same context that differ only by case, components must avoid this situation.
45Examples of what not to do:
46• Don’t have two namespaces whose names differ only by case.
47namespace ee.cummings;
48namespace Ee.Cummings;
467