
- •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 8 Language overview
1// machine-generated code in file #1
2partial class Widget
3{
4 |
private int[] counts; |
5 |
public string ToString() { |
6 |
... |
7}
8}
9// programmer-generated code in file #2
10partial class Widget
11{
12 |
private int value; |
13 |
private void Helper() { |
14 |
// ... |
15 |
} |
16 |
public int Process(object obj) { |
17 |
... |
18}
19}
20Widget’s members are the union of all the members in all its parts.
218.8 Structs
22The list of similarities between classes and structs is long—structs can implement interfaces, and can have
23the same kinds of members as classes. Structs differ from classes in several important ways, however:
24structs are value types rather than reference types, and inheritance is not supported for structs. Struct values
25are stored “on the stack” or “in-line”. Careful programmers can sometimes enhance performance through
26judicious use of structs.
27For example, the use of a struct rather than a class for a Point can make a large difference in the number of
28memory allocations performed at run time. The program below creates and initializes an array of 100 points.
29With Point implemented as a class, 101 separate objects are instantiated—one for the array and one each
30for the 100 elements.
31class Point
32{
33 |
public int x, y; |
34 |
public Point(int x, int y) { |
35 |
this.x = x; |
36 |
this.y = y; |
37}
38}
39class Test
40{
41 |
static void Main() { |
42 |
Point[] points = new Point[100]; |
43 |
for (int i = 0; i < 100; i++) |
44 |
points[i] = new Point(i, i*i); |
45}
46}
47If Point is instead implemented as a struct, as in
48struct Point
49{
50 |
public int x, y; |
51 |
public Point(int x, int y) { |
52 |
this.x = x; |
53 |
this.y = y; |
54}
55}
56only one object is instantiated—the one for the array. The Point instances are allocated in-line within the
57array. This optimization can be misused. Using structs instead of classes can also make an application run
43
C# LANGUAGE SPECIFICATION
1slower or take up more memory, as passing a struct instance by value causes a copy of that struct to be
2created.
38.9 Interfaces
4An interface defines a contract. A class or struct that implements an interface shall adhere to its contract.
5Interfaces can contain methods, properties, events, and indexers as members.
6The example
7interface IExample
8{
9 |
string this[int index] { get; set; } |
10 |
event EventHandler E; |
11 |
void F(int value); |
12string P { get; set; }
13}
14public delegate void EventHandler(object sender, EventArgs e);
15shows an interface that contains an indexer, an event E, a method F, and a property P.
16Interfaces can employ multiple inheritance. In the example
17interface IControl
18{
19void Paint();
20}
21interface ITextBox: IControl
22{
23void SetText(string text);
24}
25interface IListBox: IControl
26{
27void SetItems(string[] items);
28}
29interface IComboBox: ITextBox, IListBox {}
30the interface IComboBox inherits from both ITextBox and IListBox.
31Classes and structs can implement multiple interfaces. In the example
32interface IDataBound
33{
34void Bind(Binder b);
35}
36public class EditBox: Control, IControl, IDataBound
37{
38 |
public void Paint() {…} |
39public void Bind(Binder b) {…}
40}
41the class EditBox derives from the class Control and implements both IControl and IDataBound.
42In the previous example, the Paint method from the IControl interface and the Bind method from
43IDataBound interface are implemented using public members on the EditBox class. C# provides an
44alternative way of implementing these methods that allows the implementing class to avoid having these
45members be public. Interface members can be implemented using a qualified name. For example, the
46EditBox class could instead be implemented by providing IControl.Paint and IDataBound.Bind
47methods.
48public class EditBox: IControl, IDataBound
49{
50 |
void IControl.Paint() {…} |
51void IDataBound.Bind(Binder b) {…}
52}
44
Chapter 8 Language overview
1Interface members implemented in this way are called explicit interface members because each member
2explicitly designates the interface member being implemented. Explicit interface members can only be
3called via the interface. For example, the EditBox’s implementation of the Paint method can be called
4only by casting to the IControl interface.
5class Test
6{
7 |
static void Main() { |
|
8 |
EditBox editbox = new EditBox(); |
|
9 |
editbox.Paint(); |
// error: no such method |
10 |
IControl control = editbox; |
|
11 |
control.Paint(); |
// calls EditBox’s Paint implementation |
12}
13}
148.10 Delegates
15Delegates enable scenarios that some other languages have addressed with function pointers. However,
16unlike function pointers, delegates are object-oriented and type-safe.
17A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instance
18encapsulates one or more methods, each of which is referred to as a callable entity. For instance methods, a
19callable entity consists of an instance and a method on that instance. For static methods, a callable entity
20consists of just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all
21of that delegate instance’s methods with that set of arguments.
22An interesting and useful property of a delegate instance is that it does not know or care about the classes of
23the methods it encapsulates; all that matters is that those methods be consistent (§22.1) with the delegate’s
24type. This makes delegates perfectly suited for “anonymous” invocation. This is a powerful capability.
25There are three steps in defining and using delegates: declaration, instantiation, and invocation. Delegates
26are declared using delegate declaration syntax. The example
27delegate void SimpleDelegate();
28declares a delegate named SimpleDelegate that takes no arguments and returns no result.
29The example
30class Test
31{
32 |
static void F() { |
33 |
System.Console.WriteLine("Test.F"); |
34 |
} |
35 |
static void Main() { |
36 |
SimpleDelegate d = new SimpleDelegate(F); |
37 |
d(); |
38}
39}
40creates a SimpleDelegate instance and then immediately calls it.
41There is not much point in instantiating a delegate for a method and then immediately calling that method
42via the delegate, as it would be simpler to call the method directly. Delegates really show their usefulness
43when their anonymity is used. The example
44void MultiCall(SimpleDelegate d, int count) {
45 |
for (int i = 0; i < count; i++) { |
46 |
d(); |
47}
48}
49shows a MultiCall method that repeatedly calls a SimpleDelegate. The MultiCall method doesn’t
50know or care about the type of the target method for the SimpleDelegate, what accessibility that method
51has, or whether or not that method is static. All that matters is that the target method is consistent (§22.1)
52with SimpleDelegate.
45
C# LANGUAGE SPECIFICATION
18.11 Enums
2An enum type declaration defines a type name for a related group of symbolic constants. Enums are used for
3“multiple choice” scenarios, in which a runtime decision is made from a fixed number of choices that are
4known at compile-time.
5The example
6enum Color
7{
8 |
Red, |
9 |
Blue, |
10Green
11}
12class Shape
13{
14 |
public void Fill(Color color) { |
15 |
switch(color) { |
16 |
case Color.Red: |
17 |
… |
18 |
break; |
19 |
case Color.Blue: |
20 |
… |
21 |
break; |
22 |
case Color.Green: |
23 |
… |
24 |
break; |
25 |
default: |
26 |
break; |
27 |
} |
28}
29}
30shows a Color enum and a method that uses this enum. The signature of the Fill method makes it clear
31that the shape can be filled with one of the given colors.
32The use of enums is superior to the use of integer constants—as is common in languages without enums—
33because the use of enums makes the code more readable and self-documenting. The self-documenting nature
34of the code also makes it possible for the development tool to assist with code writing and other “designer”
35activities. For example, the use of Color rather than int for a parameter type enables smart code editors to
36suggest Color values.
378.12 Namespaces and assemblies
38The programs presented so far have stood on their own except for dependence on a few system-provided
39classes such as System.Console. It is far more common, however, for real-world applications to consist of
40several different pieces, each compiled separately. For example, a corporate application might depend on
41several different components, including some developed internally and some purchased from independent
42software vendors.
43Namespaces and assemblies enable this component-based system. Namespaces provide a logical
44organizational system. Namespaces are used both as an “internal” organization system for a program, and as
45an “external” organization system—a way of presenting program elements that are exposed to other
46programs.
47Assemblies are used for physical packaging and deployment. An assembly can contain types, the executable
48code used to implement these types, and references to other assemblies.
49To demonstrate the use of namespaces and assemblies, this subclause revisits the “hello, world” program
50presented earlier, and splits it into two pieces: a class library that provides messages and a console
51application that displays them.
52The class library will contain a single class named HelloMessage. The example
46
Chapter 8 Language overview
1// HelloLibrary.cs
2namespace CSharp.Introduction
3{
4 |
public class HelloMessage |
5 |
{ |
6 |
public string Message { |
7 |
get { |
8 |
return "hello, world"; |
9 |
} |
10 |
} |
11}
12}
13shows the HelloMessage class in a namespace named CSharp.Introduction. The HelloMessage
14class provides a read-only property named Message. Namespaces can nest, and the declaration
15namespace CSharp.Introduction
16{…}
17is shorthand for two levels of namespace nesting:
18namespace CSharp
19{
20 |
namespace Introduction |
21{…}
22}
23The next step in the componentization of “hello, world” is to write a console application that uses the
24HelloMessage class. The fully qualified name (§10.8.1) for the class—
25CSharp.Introduction.HelloMessage—could be used, but this name is quite long and unwieldy. An
26easier way is to use a using namespace directive, which makes it possible to use all of the types in a
27namespace without qualification. The example
28// HelloApp.cs
29using CSharp.Introduction;
30class HelloApp
31{
32 |
static void Main() { |
33 |
HelloMessage m = new HelloMessage(); |
34 |
System.Console.WriteLine(m.Message); |
35}
36}
37shows a using namespace directive that refers to the CSharp.Introduction namespace. The occurrences
38of HelloMessage are shorthand for CSharp.Introduction.HelloMessage.
39C# also enables the definition and use of aliases. A using alias directive defines an alias for a type or
40namespace. Such aliases can be useful in situation in which name collisions occur between two class
41libraries, or when a small number of types from a much larger namespace are being used. The example
42using MessageSource = CSharp.Introduction.HelloMessage;
43shows a using alias directive that defines MessageSource as an alias for the HelloMessage class.
44The code we have written can be compiled into a class library containing the class HelloMessage and an
45application containing the class HelloApp. The details of this compilation step might differ based on the
46compiler or tool being used. A command-line compiler might enable compilation of a class library and an
47application that uses that library with the following command-line invocations:
48csc /target:library HelloLibrary.cs
49csc /reference:HelloLibrary.dll HelloApp.cs
50which produce a class library named HelloLibrary.dll and an application named HelloApp.exe.
47