- •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
Chapter 17 Classes
1public abstract int Z { get; set; }
2}
3X is a virtual read-only property, Y is a virtual read-write property, and Z is an abstract read-write property.
4Because Z is abstract, the containing class A shall also be declared abstract.
5A class that derives from A is show below:
6class B: A
7{
8 |
int z; |
9 |
public override int X { |
10 |
get { return base.X + 1; } |
11 |
} |
12 |
public override int Y { |
13 |
set { base.Y = value < 0? 0: value; } |
14 |
} |
15 |
public override int Z { |
16 |
get { return z; } |
17 |
set { z = value; } |
18}
19}
20Here, the declarations of X, Y, and Z are overriding property declarations. Each property declaration exactly
21matches the accessibility modifiers, type, and name of the corresponding inherited property. The get
22accessor of X and the set accessor of Y use the base keyword to access the inherited accessors. The
23declaration of Z overrides both abstract accessors—thus, there are no outstanding abstract function members
24in B, and B is permitted to be a non-abstract class. end example]
2517.7 Events
26An event is a member that enables an object or class to provide notifications. Clients can attach executable
27code for events by supplying event handlers.
28Events are declared using event-declarations:
29event-declaration:
30 |
attributesopt event-modifiersopt event type variable-declarators ; |
31 |
attributesopt event-modifiersopt event type member-name |
32 |
{ event-accessor-declarations } |
33 |
event-modifiers: |
34 |
event-modifier |
35 |
event-modifiers event-modifier |
36 |
event-modifier: |
37 |
new |
38 |
public |
39 |
protected |
40 |
internal |
41 |
private |
42 |
static |
43 |
virtual |
44 |
sealed |
45 |
override |
46 |
abstract |
47extern
48event-accessor-declarations:
49 |
add-accessor-declaration remove-accessor-declaration |
50 |
remove-accessor-declaration add-accessor-declaration |
299
|
C# LANGUAGE SPECIFICATION |
|
1 |
add-accessor-declaration: |
|
2 |
attributesopt |
add block |
3 |
remove-accessor-declaration: |
|
4 |
attributesopt |
remove block |
5An event-declaration can include a set of attributes (§24) and a valid combination of the four access
6modifiers (§17.2.3), the new (§17.2.2), static (§17.5.2, §17.7.3), virtual (§17.5.3, §17.7.4), override
7(§17.5.4, §17.7.4), sealed (§17.5.5), abstract (§17.5.6, §17.7.4), and extern modifiers.
8Event declarations are subject to the same rules as method declarations (§17.5) with regard to valid
9combinations of modifiers.
10The type of an event declaration shall be a delegate-type (§11.2), and that delegate-type shall be at least as
11accessible as the event itself (§10.5.4).
12An event declaration can include event-accessor-declarations. However, if it does not, for non-extern, non-
13abstract events, the compiler shall supply them automatically (§17.7.1); for extern events, the accessors are
14provided externally.
15An event declaration that omits event-accessor-declarations defines one or more events—one for each of the
16variable-declarators. The attributes and modifiers apply to all of the members declared by such an event-
17declaration.
18It is a compile-time error for an event-declaration to include both the abstract modifier and variable-
19initializers or brace-delimited event-accessor-declarations.
20When an event declaration includes an extern modifier, the event is said to be an external event. Because
21an external event declaration provides no actual implementation, it is an error for it to include both the
22extern modifier and event-accessor-declarations.
23An event can be used as the left-hand operand of the += and -= operators (§14.13.3). These operators are
24used, respectively, to attach event handlers to, or to remove event handlers from an event, and the access
25modifiers of the event control the contexts in which such operations are permitted.
26The only operations that are permitted on an event by code that is outside the type in which that event is
27declared, are += and -=. So while such code can add and remove handlers for an event, it cannot directly
28 obtain or modify the underlying list of event handlers. In an operation of the form x += y or x –= y, when
29x is an event and the reference takes place outside the type that contains the declaration of x, the result of the
30operation has type void (as opposed to having the type of x, with the value of x after the assignment). This
31rule prohibits external code from indirectly examining the underlying delegate of an event.
32[Example: The following example shows how event handlers are attached to instances of the Button class:
33public delegate void EventHandler(object sender, EventArgs e);
34public class Button: Control
35{
36public event EventHandler Click;
37}
38public class LoginDialog: Form
39{
40 |
Button OkButton; |
41 |
Button CancelButton; |
42 |
public LoginDialog() { |
43 |
OkButton = new Button(…); |
44 |
OkButton.Click += OkButtonClick; |
45 |
CancelButton = new Button(…); |
46 |
CancelButton.Click += CancelButtonClick; |
47 |
} |
48 |
void OkButtonClick(object sender, EventArgs e) { |
49 |
// Handle OkButton.Click event |
50 |
} |
300
|
Chapter 17 Classes |
1 |
void CancelButtonClick(object sender, EventArgs e) { |
2 |
// Handle CancelButton.Click event |
3}
4}
5Here, the LoginDialog instance constructor creates two Button instances and attaches event handlers to
6the Click events. end example]
717.7.1 Field-like events
8Within the program text of the class or struct that contains the declaration of an event, certain events can be
9used like fields. To be used in this way, an event shall not be abstract or extern, and shall not explicitly
10include event-accessor-declarations. Such an event can be used in any context that permits a field. The field
11contains a delegate (§22), which refers to the list of event handlers that have been added to the event. If no
12event handlers have been added, the field contains null.
13[Example: In the following code
14public delegate void EventHandler(object sender, EventArgs e);
15public class Button: Control
16{
17 |
public event EventHandler Click; |
18 |
protected void OnClick(EventArgs e) { |
19 |
EventHandler toRaise = Click; |
20 |
if (toRaise != null) |
21 |
toRaise(this, e); |
22 |
} |
23 |
public void Reset() { |
24 |
Click = null; |
25}
26}
27Click is used as a field within the Button class. As the example demonstrates, the field can be examined,
28modified, and used in delegate invocation expressions. The OnClick method in the Button class “raises”
29the Click event. The notion of raising an event is precisely equivalent to invoking the delegate represented
30by the event—thus, there are no special language constructs for raising events. Note that the delegate
31invocation is preceded by a check that ensures the delegate is non-null and that the check is made on a local
32copy to ensure thread safety.
33Outside the declaration of the Button class, the Click member can only be used on the left-hand side of
34the += and –= operators, as in
35b.Click += new EventHandler(…);
36which appends a delegate to the invocation list of the Click event, and
37b.Click –= new EventHandler(…);
38which removes a delegate from the invocation list of the Click event. end example]
39When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and
40creates accessors for the event that add or remove event handlers to the delegate field. In order to be thread-
41safe, the addition or removal operations are done while holding the lock (§15.12) on the containing object
42for an instance event, or the type object (§14.5.11) for a static event.
43[Note: Thus, an instance event declaration of the form:
44class X
45{
46public event D Ev;
47}
48shall be compiled to something equivalent to:
49class X
50{
51 |
private D __Ev; // field to hold the delegate |
301
|
C# LANGUAGE SPECIFICATION |
1 |
public event D Ev { |
2 |
add { |
3 |
lock(this) { __Ev = __Ev + value; } |
4 |
} |
5 |
remove { |
6 |
lock(this) { __Ev = __Ev - value; } |
7 |
} |
8}
9}
10Within the class X, references to Ev are compiled to reference the hidden field __Ev instead. The name
11“__Ev” is arbitrary; the hidden field could have any name or no name at all.
12Similarly, a static event declaration of the form:
13class X
14{
15public static event D Ev;
16}
17shall be compiled to something equivalent to:
18class X
19{
20 |
private static D __Ev; // field to hold the delegate |
21 |
public static event D Ev { |
22 |
add { |
23 |
lock(typeof(X)) { __Ev = __Ev + value; } |
24 |
} |
25 |
remove { |
26 |
lock(typeof(X)) { __Ev = __Ev - value; } |
27 |
} |
28}
29}
30end note]
3117.7.2 Event accessors
32[Note: Event declarations typically omit event-accessor-declarations, as in the Button example above. One
33situation for doing so involves the case in which the storage cost of one field per event is not acceptable. In
34such cases, a class can include event-accessor-declarations and use a private mechanism for storing the list
35of event handlers. Similarly, in cases where the handling of an event requires access to external resources,
36event accessors can be used to manage these resources. end note]
37The event-accessor-declarations of an event specify the executable statements associated with adding and
38removing event handlers.
39The accessor declarations consist of an add-accessor-declaration and a remove-accessor-declaration. Each
40accessor declaration consists of the token add or remove followed by a block. The block associated with an
41add-accessor-declaration specifies the statements to execute when an event handler is added, and the block
42associated with a remove-accessor-declaration specifies the statements to execute when an event handler is
43removed.
44Each add-accessor-declaration and remove-accessor-declaration corresponds to a method with a single
45value parameter of the event type, and a void return type. The implicit parameter of an event accessor is
46named value. When an event is used in an event assignment, the appropriate event accessor is used.
47Specifically, if the assignment operator is += then the add accessor is used, and if the assignment operator is
48–= then the remove accessor is used. In either case, the right-hand operand of the assignment operator is
49used as the argument to the event accessor. The block of an add-accessor-declaration or a remove-accessor-
50declaration shall conform to the rules for void methods described in §17.5.8. In particular, return
51statements in such a block are not permitted to specify an expression.
302
