- •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 class declaration can contain declarations of constants, fields, methods, properties, events, indexers,
2operators, instance constructors, destructors, static constructors, and types.
3The members of object and string correspond directly to the members of the class types they alias:
4• The members of object are the members of the System.Object class.
5• The members of string are the members of the System.String class.
610.4.5 Interface members
7The members of an interface are the members declared in the interface and in all base interfaces of the
8interface. [Note: The members in class object are not, strictly speaking, members of any interface (§20.2).
9However, the members in class object are available via member lookup in any interface type (§14.3). end
10note]
1110.4.6 Array members
12The members of an array are the members inherited from class System.Array.
1310.4.7 Delegate members
14The members of a delegate are the members inherited from class System.Delegate.
1510.5 Member access
16Declarations of members allow control over member access. The accessibility of a member is established by
17the declared accessibility (§10.5.1) of the member combined with the accessibility of the immediately
18containing type, if any.
19When access to a particular member is allowed, the member is said to be accessible. Conversely, when
20access to a particular member is disallowed, the member is said to be inaccessible. Access to a member is
21permitted when the textual location in which the access takes place is included in the accessibility domain
22(§10.5.2) of the member.
2310.5.1 Declared accessibility
24The declared accessibility of a member can be one of the following:
25• Public, which is selected by including a public modifier in the member declaration. The intuitive
26meaning of public is “access not limited”.
27• Protected, which is selected by including a protected modifier in the member declaration. The
28intuitive meaning of protected is “access limited to the containing class or types derived from the
29containing class”.
30• Internal, which is selected by including an internal modifier in the member declaration. The intuitive
31meaning of internal is “access limited to this program”.
32• Protected internal, which is selected by including both a protected and an internal modifier in the
33member declaration. The intuitive meaning of protected internal is “access limited to this program
34or types derived from the containing class”.
35• Private, which is selected by including a private modifier in the member declaration. The intuitive
36meaning of private is “access limited to the containing type”.
37Depending on the context in which a member declaration takes place, only certain types of declared
38accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers,
39the context in which the declaration takes place determines the default declared accessibility.
40• Namespaces implicitly have public declared accessibility. No access modifiers are allowed on
41namespace declarations.
88
Chapter 10 Basic concepts
1• Types declared in compilation units or namespaces can have public or internal declared
2accessibility and default to internal declared accessibility.
3• Class members can have any of the five kinds of declared accessibility and default to private declared
4accessibility. [Note: A type declared as a member of a class can have any of the five kinds of declared
5accessibility, whereas a type declared as a member of a namespace can have only public or internal
6declared accessibility. end note]
7• Struct members can have public, internal, or private declared accessibility and default to
8private declared accessibility because structs are implicitly sealed. Struct members introduced in a
9struct (that is, not inherited by that struct) cannot have protected or protected internal declared
10accessibility. [Note: A type declared as a member of a struct can have public, internal, or private
11declared accessibility, whereas a type declared as a member of a namespace can have only public or
12internal declared accessibility. end note]
13• Interface members implicitly have public declared accessibility. No access modifiers are allowed on
14interface member declarations.
15• Enumeration members implicitly have public declared accessibility. No access modifiers are allowed
16on enumeration member declarations.
1710.5.2 Accessibility domains
18The accessibility domain of a member consists of the (possibly disjoint) sections of program text in which
19access to the member is permitted. For purposes of defining the accessibility domain of a member, a member
20is said to be top-level if it is not declared within a type, and a member is said to be nested if it is declared
21within another type. Furthermore, the text of a program is defined as all source text contained in all source
22files of that program, and the source text of a type is defined as all source text contained between the
23opening and closing “{” and “}” tokens in the class-body, struct-body, interface-body, or enum-body of all
24declarations for the type (including, possibly, multiple partial declarations and all types that are nested
25within the type).
26The accessibility domain of a predefined type (such as object, int, or double) is unlimited.
27The accessibility domain of a top-level type T that is declared in a program P is defined as follows:
28• If the declared accessibility of T is public, the accessibility domain of T is the program text of P and
29any program that references P.
30• If the declared accessibility of T is internal, the accessibility domain of T is the program text of P.
31[Note: From these definitions it follows that the accessibility domain of a top-level type is always at least the
32program text of the program in which that type is declared. end note]
33The accessibility domain of a nested member M declared in a type T within a program P, is defined as
34follows (noting that M itself might possibly be a type):
35• If the declared accessibility of M is public, the accessibility domain of M is the accessibility domain
36of T.
37• If the declared accessibility of M is protected internal, let D be the union of the program text of P
38and the program text of any type derived from T, which is declared outside P. The accessibility domain
39of M is the intersection of the accessibility domain of T with D.
40• If the declared accessibility of M is protected, let D be the union of the program text of T and the
41program text of any type derived from T. The accessibility domain of M is the intersection of the
42accessibility domain of T with D.
43• If the declared accessibility of M is internal, the accessibility domain of M is the intersection of the
44accessibility domain of T with the program text of P.
45• If the declared accessibility of M is private, the accessibility domain of M is the program text of T.
89
C# LANGUAGE SPECIFICATION
1[Note: From these definitions it follows that the accessibility domain of a nested member is always at least
2the program text of the type in which the member is declared. Furthermore, it follows that the accessibility
3domain of a member is never more inclusive than the accessibility domain of the type in which the member
4is declared. end note]
5[Note: In intuitive terms, when a type or member M is accessed, the following steps are evaluated to ensure
6that the access is permitted:
7• First, if M is declared within a type (as opposed to a compilation unit or a namespace), a compile-time
8error occurs if that type is not accessible.
9• Then, if M is public, the access is permitted.
10• Otherwise, if M is protected internal, the access is permitted if it occurs within the program in
11which M is declared, or if it occurs within a class derived from the class in which M is declared and takes
12place through the derived class type (§10.5.3).
13• Otherwise, if M is protected, the access is permitted if it occurs within the class in which M is declared,
14or if it occurs within a class derived from the class in which M is declared and takes place through the
15derived class type (§10.5.3).
16• Otherwise, if M is internal, the access is permitted if it occurs within the program in which M is
17declared.
18• Otherwise, if M is private, the access is permitted if it occurs within the type in which M is declared.
19• Otherwise, the type or member is inaccessible, and a compile-time error occurs.
20end note]
21[Example: In the following code
22public class A
23{
24 |
public static int X; |
25 |
internal static int Y; |
26private static int Z;
27}
28internal class B
29{
30 |
public static int X; |
31 |
internal static int Y; |
32 |
private static int Z; |
33 |
public class C |
34 |
{ |
35 |
public static int X; |
36 |
internal static int Y; |
37 |
private static int Z; |
38 |
} |
39 |
private class D |
40 |
{ |
41 |
public static int X; |
42 |
internal static int Y; |
43 |
private static int Z; |
44}
45}
46the classes and members have the following accessibility domains:
47• The accessibility domain of A and A.X is unlimited.
48• The accessibility domain of A.Y, B, B.X, B.Y, B.C, B.C.X, and B.C.Y is the program text of the
49containing program.
50• The accessibility domain of A.Z is the program text of A.
90
Chapter 10 Basic concepts
1• The accessibility domain of B.Z and B.D is the program text of B, including the program text of B.C
2and B.D.
3• The accessibility domain of B.C.Z is the program text of B.C.
4• The accessibility domain of B.D.X and B.D.Y is the program text of B, including the program text of
5B.C and B.D.
6• The accessibility domain of B.D.Z is the program text of B.D.
7As the example illustrates, the accessibility domain of a member is never larger than that of a containing
8type. For example, even though all X members have public declared accessibility, all but A.X have
9accessibility domains that are constrained by a containing type. end example]
10As described in §10.4, all members of a base class, except for instance constructors, destructors, and
11static constructors are inherited by derived types. This includes even private members of a base class.
12However, the accessibility domain of a private member includes only the program text of the type in
13which the member is declared. [Example: In the following code
14class A
15{
16 |
int x; |
|
17 |
static void F(B b) { |
|
18 |
b.x = 1; |
// Ok |
19}
20}
21class B: A
22{
23 |
static |
void |
F(B b) { |
24 |
b.x |
= 1; |
// Error, x not accessible |
25}
26}
27the B class inherits the private member x from the A class. Because the member is private, it is only
28accessible within the class-body of A. Thus, the access to b.x succeeds in the A.F method, but fails in the
29B.F method. end example]
3010.5.3 Protected access for instance members
31When a protected instance member is accessed outside the program text of the class in which it is
32declared, and when a protected internal instance member is accessed outside the program text of the
33program in which it is declared, the access is required to take place through an instance of the derived class
34type in which the access occurs. Let B be a base class that declares a protected instance member M, and let D
35be a class that derives from B. Within the class-body of D, access to M can take one of the following forms:
36• An unqualified type-name or primary-expression of the form M.
37• A primary-expression of the form E.M, provided the type of E is D or a class derived from D.
38• A primary-expression of the form base.M.
39In addition to these forms of access, a derived class can access a protected instance constructor of a base
40class in a constructor-initializer (§17.10.1).
41[Example: In the following code
42public class A
43{
44 |
protected int x; |
|
45 |
static void F(A a, B b) { |
|
46 |
a.x = 1; |
// Ok |
47 |
b.x = 1; |
// Ok |
48}
49}
91
