- •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
1interface ITextBox: IControl
2{
3void SetText(string text);
4}
5interface IListBox: IControl
6{
7void SetItems(string[] items);
8}
9class ComboBox: IControl, ITextBox, IListBox
10{
11 |
void |
IControl.Paint() {…} |
12 |
void |
ITextBox.SetText(string text) {…} |
13void IListBox.SetItems(string[] items) {…}
14}
15it is not possible to have separate implementations for the IControl named in the base class list, the
16IControl inherited by ITextBox, and the IControl inherited by IListBox. Indeed, there is no notion of
17a separate identity for these interfaces. Rather, the implementations of ITextBox and IListBox share the
18same implementation of IControl, and ComboBox is simply considered to implement three interfaces,
19IControl, ITextBox, and IListBox. end example]
20The members of a base class participate in interface mapping. [Example: In the following code
21interface Interface1
22{
23void F();
24}
25class Class1
26{
27 |
public void F() {} |
28public void G() {}
29}
30class Class2: Class1, Interface1
31{
32new public void G() {}
33}
34the method F in Class1 is used in Class2's implementation of Interface1. end example]
3520.4.3 Interface implementation inheritance
36A class inherits all interface implementations provided by its base classes.
37Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface
38mappings it inherits from its base classes. [Example: In the declarations
39interface IControl
40{
41void Paint();
42}
43class Control: IControl
44{
45public void Paint() {…}
46}
47class TextBox: Control
48{
49new public void Paint() {…}
50}
51the Paint method in TextBox hides the Paint method in Control, but it does not alter the mapping of
52Control.Paint onto IControl.Paint, and calls to Paint through class instances and interface
53instances will have the following effects
346
Chapter 20 Interfaces
1Control c = new Control();
2TextBox t = new TextBox();
3IControl ic = c;
4IControl it = t;
5 |
c.Paint(); |
// invokes Control.Paint(); |
6 |
t.Paint(); |
// invokes TextBox.Paint(); |
7 |
ic.Paint(); |
// invokes Control.Paint(); |
8 |
it.Paint(); |
// invokes Control.Paint(); |
9end example]
10However, when an interface method is mapped onto a virtual method in a class, it is possible for derived
11classes to override the virtual method and alter the implementation of the interface. [Example: Rewriting the
12declarations above to
13interface IControl
14{
15void Paint();
16}
17class Control: IControl
18{
19public virtual void Paint() {…}
20}
21class TextBox: Control
22{
23public override void Paint() {…}
24}
25the following effects will now be observed
26Control c = new Control();
27TextBox t = new TextBox();
28IControl ic = c;
29IControl it = t;
30 |
c.Paint(); |
// invokes Control.Paint(); |
31 |
t.Paint(); |
// invokes TextBox.Paint(); |
32 |
ic.Paint(); |
// invokes Control.Paint(); |
33 |
it.Paint(); |
// invokes TextBox.Paint(); |
34end example]
35Since explicit interface member implementations cannot be declared virtual, it is not possible to override an
36explicit interface member implementation. However, it is perfectly valid for an explicit interface member
37implementation to call another method, and that other method can be declared virtual to allow derived
38classes to override it. [Example:
39interface IControl
40{
41void Paint();
42}
43class Control: IControl
44{
45 |
void IControl.Paint() { PaintControl(); } |
46protected virtual void PaintControl() {…}
47}
48class TextBox: Control
49{
50protected override void PaintControl() {…}
51}
52Here, classes derived from Control can specialize the implementation of IControl.Paint by overriding
53the PaintControl method. end example]
347
C# LANGUAGE SPECIFICATION
120.4.4 Interface re-implementation
2A class that inherits an interface implementation is permitted to re-implement the interface by including it in
3the base class list.
4A re-implementation of an interface follows exactly the same interface mapping rules as an initial
5implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the
6interface mapping established for the re-implementation of the interface. [Example: In the declarations
7interface IControl
8{
9void Paint();
10}
11class Control: IControl
12{
13void IControl.Paint() {…}
14}
15class MyControl: Control, IControl
16{
17public void Paint() {}
18}
19the fact that Control maps IControl.Paint onto Control.IControl.Paint doesn’t affect the re-
20implementation in MyControl, which maps IControl.Paint onto MyControl.Paint. end example]
21Inherited public member declarations and inherited explicit interface member declarations participate in the
22interface mapping process for re-implemented interfaces. [Example:
23interface IMethods
24{
25 |
void F(); |
26 |
void G(); |
27 |
void H(); |
28void I();
29}
30class Base: IMethods
31{
32 |
void IMethods.F() {} |
33 |
void IMethods.G() {} |
34 |
public void H() {} |
35public void I() {}
36}
37class Derived: Base, IMethods
38{
39 |
public void F() {} |
40void IMethods.H() {}
41}
42Here, the implementation of IMethods in Derived maps the interface methods onto Derived.F,
43Base.IMethods.G, Derived.IMethods.H, and Base.I. end example]
44When a class implements an interface, it implicitly also implements all of that interface’s base interfaces.
45Likewise, a re-implementation of an interface is also implicitly a re-implementation of all of the interface’s
46base interfaces. [Example:
47interface IBase
48{
49void F();
50}
51interface IDerived: IBase
52{
53void G();
54}
55class C: IDerived
56{
57 |
void IBase.F() {…} |
348
Chapter 20 Interfaces
1void IDerived.G() {…}
2}
3class D: C, IDerived
4{
5 |
public void F() {…} |
6public void G() {…}
7}
8Here, the re-implementation of IDerived also re-implements IBase, mapping IBase.F onto D.F. end
9example]
1020.4.5 Abstract classes and interfaces
11Like a non-abstract class, an abstract class shall provide implementations of all members of the interfaces
12that are listed in the base class list of the class. However, an abstract class is permitted to map interface
13methods onto abstract methods. [Example:
14interface IMethods
15{
16 |
void F(); |
17void G();
18}
19abstract class C: IMethods
20{
21 |
public abstract void F(); |
22public abstract void G();
23}
24Here, the implementation of IMethods maps F and G onto abstract methods, which shall be overridden in
25non-abstract classes that derive from C. end example]
26Explicit interface member implementations cannot be abstract, but explicit interface member
27implementations are of course permitted to call abstract methods. [Example:
28interface IMethods
29{
30 |
void F(); |
31void G();
32}
33abstract class C: IMethods
34{
35 |
void IMethods.F() { FF(); } |
36 |
void IMethods.G() { GG(); } |
37 |
protected abstract void FF(); |
38protected abstract void GG();
39}
40Here, non-abstract classes that derive from C would be required to override FF and GG, thus providing the
41actual implementation of IMethods. end example]
349
