- •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
1A declaration of a new member hides an inherited member only within the scope of the new member.
2[Example:
3class Base
4{
5public static void F() {}
6}
7class Derived: Base
8{
9 |
new private static void F() {} // Hides Base.F in Derived only |
10}
11class MoreDerived: Derived
12{
13 |
static void G() { F(); } |
// Invokes Base.F |
14}
15In the example above, the declaration of F in Derived hides the F that was inherited from Base, but since
16the new F in Derived has private access, its scope does not extend to MoreDerived. Thus, the call F() in
17MoreDerived.G is valid and will invoke Base.F. end example]
1810.8 Namespace and type names
19Several contexts in a C# program require a namespace-name or a type-name to be specified.
20namespace-name:
21 |
namespace-or-type-name |
22 |
type-name: |
23 |
namespace-or-type-name |
24 |
namespace-or-type-name: |
25 |
identifier type-argument-listopt |
26 |
qualified-alias-member |
27 |
namespace-or-type-name . identifier type-argument-listop |
28A namespace-name is a namespace-or-type-name that refers to a namespace.
29Following resolution as described below, the namespace-or-type-name of a namespace-name shall refer to a
30namespace, or otherwise a compile-time error occurs. Type arguments (§26.5.1) shall not be present in a
31namespace-name (only types can have type arguments).
32A type-name is a namespace-or-type-name that refers to a type. Following resolution as described below, the
33namespace-or-type-name of a type-name shall refer to a type, or otherwise a compile-time error occurs.
34The syntax and semantics of qualified-alias-member are defined in §16.7.
35A namespace-or-type-name that is not a qualified-alias-member has one of four forms:
36• I
37• I<A1, ..., AK>
38• N.I
39• N.I<A1, ..., AK>
40where I is a single identifier, N is a namespace-or-type-name and <A1, ..., AK> is an optional type-
41argument-list. When no type-argument-list is specified, consider K to be zero.
42The meaning of a namespace-or-type-name is determined as follows:
43• If the namespace-or-type-name is a qualified-alias-member, the meaning is as specified in §16.7.
44• Otherwise, if the namespace-or-type-name is of the form I or of the form I<A1, ..., AK>:
98
Chapter 10 Basic concepts
1o If K is zero and the namespace-or-type-name appears within the body of a generic method
2declaration (§26.6) and if that declaration includes a type parameter (§26.1.1) with name I, then the
3namespace-or-type-name refers to that type parameter.
4o Otherwise, if the namespace-or-type-name appears within the body of a type declaration, then for
5each instance type T (§26.1.2), starting with the instance type of that type declaration and continuing
6with the instance type of each enclosing class or struct declaration (if any):
7• If K is zero and the declaration of T includes a type parameter with name I, then the namespace-
8 |
or-type-name refers to that type parameter. |
9 |
• Otherwise, if T contains a nested accessible type having name I and K type parameters, then the |
10 |
namespace-or-type-name refers to that type constructed with the given type arguments. If there |
11 |
is more than one such type, the type declared within the more derived type is selected. [Note: |
12 |
Non-type members (constants, fields, methods, properties, indexers, operators, instance |
13 |
constructors, destructors, and static constructors) and type members with a different number of |
14 |
type parameters are ignored when determining the meaning of the namespace-or-type-name. end |
15 |
note] |
16o Otherwise, for each namespace N, starting with the namespace in which the namespace-or-type-
17name occurs, continuing with each enclosing namespace (if any), and ending with the global
18namespace, the following steps are evaluated until an entity is located:
19• If K is zero and I is the name of a namespace in N, then:
20 |
o If the location where the namespace-or-type-name occurs is enclosed by a namespace |
21 |
declaration for N and the namespace declaration contains an extern-alias-directive or using- |
22 |
alias-directive that associates the name I with a namespace or type, then the namespace-or- |
23 |
type-name is ambiguous and a compile-time error occurs. |
24o Otherwise, the namespace-or-type-name refers to the namespace named I in N.
25• Otherwise, if N contains an accessible type having name I and K type parameters, then:
26 |
o If K is zero and the location where the namespace-or-type-name occurs is enclosed by a |
27 |
namespace declaration for N and the namespace declaration contains an extern-alias- |
28 |
directive or using-alias-directive that associates the name I with a namespace or type, then |
29 |
the namespace-or-type-name is ambiguous and a compile-time error occurs. |
30 |
o Otherwise, the namespace-or-type-name refers to the type constructed with the given type |
31 |
arguments. |
32 |
• Otherwise, if the location where the namespace-or-type-name occurs is enclosed by a |
33 |
namespace declaration for N: |
34 |
o If K is zero and the namespace declaration contains an extern-alias-directive or using-alias- |
35 |
directive that associates the name I with an imported namespace or type, then the |
36 |
namespace-or-type-name refers to that namespace or type. |
37 |
o Otherwise, if the namespaces imported by the using-namespace-directives of the namespace |
38 |
declaration contain exactly one type having name I and K type parameters, then the |
39 |
namespace-or-type-name refers to that type constructed with the given type arguments. |
40 |
o Otherwise, if the namespaces imported by the using-namespace-directives of the namespace |
41 |
declaration contain more than one type having name I and K type parameters, then the |
42 |
namespace-or-type-name is ambiguous and an error occurs. |
43o Otherwise, the namespace-or-type-name is undefined and a compile-time error occurs.
44• Otherwise, the namespace-or-type-name is of the form N.I or of the form N.I<A1, ..., AK>. N is first
45resolved as a namespace-or-type-name. If the resolution of N is not successful, a compile-time error
46occurs. Otherwise, N.I or N.I<A1, ..., AK> is resolved as follows:
99
C# LANGUAGE SPECIFICATION
1o If K is zero and N refers to a namespace and N contains a nested namespace with name I, then the
2namespace-or-type-name refers to that nested namespace.
3o Otherwise, if N refers to a namespace and N contains an accessible type having name I and K type
4parameters, then the namespace-or-type-name refers to that type constructed with the given type
5arguments.
6o Otherwise, if N refers to a (possibly constructed) class or struct type and N contains a nested
7accessible type having name I and K type parameters, then the namespace-or-type-name refers to
8that type constructed with the given type arguments. If there is more than one such type, the type
9declared within the more derived type is selected.
10o Otherwise, N.I is an invalid namespace-or-type-name, and a compile-time error occurs.
11A namespace-or-type-name is permitted to reference a static class (§17.1.1.3) if
12• The namespace-or-type-name is the T in a namespace-or-type-name of the form T.I, or
13• The namespace-or-type-name is the T in a typeof-expression (§14.5.11) of the form typeof(T)
1410.8.1 Unqualified name
15Every namespace declaration and type declaration has an unqualified name determined as follows:
16• For a namespace declaration, the unqualified name is the qualified-identifier specified in the declaration.
17• For a type declaration with no type-parameter-list, the unqualified name is the identifier specified in the
18declaration.
19• For a type declaration with K type parameters, the unqualified name is the identifier specified in the
20declaration, followed by the < token, K-1 commas and the > token.
2110.8.2 Fully qualified names
22Every namespace declaration and type declaration has a fully qualified name, which uniquely identifies the
23namespace or type amongst all others. The fully qualified name of a namespace or type declaration with
24unqualified name N is determined as follows:
25• If the declaration is contained directly in a compilation unit and not nested in any other declaration, its
26fully qualified name is N.
27• Otherwise, its fully qualified name is S.N, where S is the fully qualified name of the immediately
28enclosing namespace or type declaration.
29In other words, the fully qualified name of a declaration is the complete hierarchical path of identifiers (and
30generic dimensionality specifiers) that lead to the type or namespace, starting from the global namespace.
31The fully qualified name of a declaration shall uniquely identify the namespace, non-generic type or generic
32instance type (§26.1.2) associated with the declaration. It is a compile-time error for the same fully qualified
33name to refer to two distinct entities. In particular:
34• It is an error for both a namespace declaration and a type declaration to have the same fully qualified
35name.
36• It is an error for two different kinds of type declarations to have the same fully qualified name (for
37example, if both a struct and class declaration have the same fully qualified name).
38• It is an error for a type declaration without the partial modifier to have the same fully qualified name
39as another type declaration (§17.1.4).
40[Example: The example below shows several namespace and type declarations along with their associated
41fully qualified names.
42 |
class A {} |
// A |
100
