- •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
18.2.4 Type system unification
2C# provides a “unified type system”. All types—including value types—derive from the type object. It is
3possible to call object methods on any value, even values of “primitive” types such as int. The example
4using System;
5class Test
6{
7 |
static void Main() { |
8 |
Console.WriteLine(3.ToString()); |
9}
10}
11calls the object-defined ToString method on an integer literal, resulting in the output “3”.
12The example
13class Test
14{
15 |
static void Main() { |
|
16 |
int i = 123; |
|
17 |
object o = i; |
// boxing |
18 |
int j = (int) o; |
// unboxing |
19}
20}
21is more interesting. An int value can be converted to object and back again to int. This example shows
22both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an
23object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite.
24When an object box is cast back to its original value type, the value is copied out of the box and into the
25appropriate storage location.
26This type system unification provides value types with the benefits of object-ness without introducing
27unnecessary overhead. For programs that don’t need int values to act like objects, int values are simply
2832-bit values. For programs that need int values to behave like objects, this capability is available on
29demand. This ability to treat value types as objects bridges the gap between value types and reference types
30that exists in most languages. For example, a Stack class can provide Push and Pop methods that take and
31return object values.
32public class Stack
33{
34 |
public object Pop() {…} |
35public void Push(object o) {…}
36}
37Because C# has a unified type system, the Stack class can be used with elements of any type, including
38value types like int.
398.3 Variables and parameters
40Variables represent storage locations. Every variable has a type that determines what values can be stored in
41the variable. Local variables are variables that are declared in function members such as methods,
42properties, and indexers. A local variable is defined by specifying a type name and a declarator that specifies
43the variable name and an optional initial value, as in:
44int a;
45int b = 1;
46but it is also possible for a local variable declaration to include multiple declarators. The declarations of a
47and b can be rewritten as:
48int a, b = 1;
49A variable shall be assigned before its value can be obtained. The example
22
Chapter 8 Language overview
1class Test
2{
3 |
static void Main() { |
4 |
int a; |
5 |
int b = 1; |
6 |
int c = a + b; // error, a not yet assigned |
7 |
… |
8}
9}
10results in a compile-time error because it attempts to use the variable a before it is assigned a value. The
11rules governing definite assignment are defined in §12.3.
12A field (§17.4) is a variable that is associated with a class or struct, or an instance of a class or struct. A field
13declared with the static modifier defines a static variable, and a field declared without this modifier
14defines an instance variable. A static field is associated with a type, whereas an instance variable is
15associated with an instance. The example
16using Personnel.Data;
17class Employee
18{
19 |
private static DataSet ds; |
20 |
public string Name; |
21 |
public decimal Salary; |
22…
23}
24shows an Employee class that has a private static variable and two public instance variables.
25Formal parameter declarations also define variables. There are four kinds of parameters: value parameters,
26reference parameters, output parameters, and parameter arrays.
27A value parameter is used for “in” parameter passing, in which the value of an argument is passed into a
28method, and modifications of the parameter do not impact the original argument. A value parameter refers to
29its own variable, one that is distinct from the corresponding argument. This variable is initialized by copying
30the value of the corresponding argument. The example
31using System;
32class Test
33{
34 |
static void F(int p) { |
35 |
Console.WriteLine("p = {0}", p); |
36 |
p++; |
37 |
} |
38 |
static void Main() { |
39 |
int a = 1; |
40 |
Console.WriteLine("pre: a = {0}", a); |
41 |
F(a); |
42 |
Console.WriteLine("post: a = {0}", a); |
43}
44}
45shows a method F that has a value parameter named p. The example produces the output:
46pre: a = 1
47p = 1
48post: a = 1
49even though the value parameter p is modified.
50A reference parameter is used for “by reference” parameter passing, in which the parameter acts as an alias
51for a caller-provided argument. A reference parameter does not itself define a variable, but rather refers to
52the variable of the corresponding argument. Modifications of a reference parameter impact the
53corresponding argument. A reference parameter is declared with a ref modifier. The example
54using System;
23
C# LANGUAGE SPECIFICATION
1class Test
2{
3 |
static |
void Swap(ref int a, ref int b) { |
4 |
int |
t = a; |
5 |
a = |
b; |
6 |
b = t; |
|
7 |
} |
|
8 |
static void Main() { |
|
9 |
int x = 1; |
|
10 |
int y = 2; |
|
11 |
|
|
12 |
Console.WriteLine("pre: x = {0}, y = {1}", x, y); |
|
13 |
Swap(ref x, ref y); |
|
14 |
Console.WriteLine("post: x = {0}, y = {1}", x, y); |
15}
16}
17shows a Swap method that has two reference parameters. The output produced is:
18pre: x = 1, y = 2
19post: x = 2, y = 1
20The ref keyword shall be used in both the declaration of the formal parameter and in uses of it. The use of
21ref at the call site calls special attention to the parameter, so that a developer reading the code will
22understand that the value of the argument could change as a result of the call.
23An output parameter is similar to a reference parameter, except that the initial value of the caller-provided
24argument is unimportant. An output parameter is declared with an out modifier. The example
25using System;
26class Test
27{
28 |
static void Divide(int a, int b, out int result, out int remainder) { |
29 |
result = a / b; |
30 |
remainder = a % b; |
31 |
} |
32 |
static void Main() { |
33 |
for (int i = 1; i < 10; i++) |
34 |
for (int j = 1; j < 10; j++) { |
35 |
int ans, r; |
36 |
Divide(i, j, out ans, out r); |
37 |
Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r); |
38 |
} |
39}
40}
41shows a Divide method that includes two output parameters—one for the result of the division and another
42for the remainder.
43For value, reference, and output parameters, there is a one-to-one correspondence between caller-provided
44arguments and the parameters used to represent them. A parameter array enables a many-to-one
45relationship: many arguments can be represented by a single parameter array. In other words, parameter
46arrays enable variable length argument lists.
47A parameter array is declared with a params modifier. There can be only one parameter array for a given
48method, and it shall always be the last parameter specified. The type of a parameter array is always a single
49dimensional array type. A caller can either pass a single argument of this array type, or any number of
50arguments of the element type of this array type. For instance, the example
51using System;
24
Chapter 8 Language overview
1class Test
2{
3 |
static void F(params int[] args) { |
||
4 |
Console.WriteLine("# of arguments: {0}", args.Length); |
||
5 |
for (int i = 0; i < args.Length; i++) |
||
6 |
Console.WriteLine("\targs[{0}] = {1}", i, args[i]); |
||
7 |
} |
|
|
8 |
static void Main() { |
||
9 |
F(); |
|
|
10 |
F(1); |
|
|
11 |
F(1, 2); |
|
|
12 |
F(1, |
2, |
3); |
13 |
F(new int[] {1, 2, 3, 4}); |
14}
15}
16shows a method F that takes a variable number of int arguments, and several invocations of this method.
17The output is:
18# of arguments: 0
19# of arguments: 1
20args[0] = 1
21# of arguments: 2
22 |
args[0] = 1 |
23args[1] = 2
24# of arguments: 3
25 |
args[0] |
= |
1 |
26 |
args[1] |
= |
2 |
27args[2] = 3
28# of arguments: 4
29 |
args[0] = 1 |
|
30 |
args[1] = |
2 |
31 |
args[2] = |
3 |
32args[3] = 4
33Most of the examples presented in this introduction use the WriteLine method of the Console class. The
34argument substitution behavior of this method, as exhibited in the example
35int a = 1, b = 2;
36Console.WriteLine("a = {0}, b = {1}", a, b);
37is accomplished using a parameter array. The WriteLine method provides several overloaded methods for
38the common cases in which a small number of arguments are passed, and one method that uses a parameter
39array.
40namespace System
41{
42 |
public class Console |
43 |
{ |
44 |
public static void WriteLine(string s) {…} |
45 |
public static void WriteLine(string s, object a) {…} |
46 |
public static void WriteLine(string s, object a, object b) {…} |
47 |
… |
48 |
public static void WriteLine(string s, params object[] args) {…} |
49}
50}
518.4 Automatic memory management
52Manual memory management requires developers to manage the allocation and de-allocation of blocks of
53memory. Manual memory management can be both time-consuming and difficult. In C#, automatic memory
54management is provided so that developers are freed from this burdensome task. In the vast majority of
55cases, automatic memory management increases code quality and enhances developer productivity without
56negatively impacting either expressiveness or performance.
57The example
58using System;
25
C# LANGUAGE SPECIFICATION
1public class Stack
2{
3 |
private Node first = null; |
4 |
public bool Empty { |
5 |
get { |
6 |
return (first == null); |
7 |
} |
8 |
} |
9 |
public object Pop() { |
10 |
if (first == null) |
11 |
throw new Exception("Can't Pop from an empty Stack."); |
12 |
else { |
13 |
object temp = first.Value; |
14 |
first = first.Next; |
15 |
return temp; |
16 |
} |
17 |
} |
18 |
public void Push(object o) { |
19 |
first = new Node(o, first); |
20 |
} |
21 |
class Node |
22 |
{ |
23 |
public Node Next; |
24 |
public object Value; |
25 |
public Node(object value): this(value, null) {} |
26 |
public Node(object value, Node next) { |
27 |
Next = next; |
28 |
Value = value; |
29 |
} |
30}
31}
32shows a Stack class implemented as a linked list of Node instances. Node instances are created in the Push
33method and are garbage collected when no longer needed. A Node instance becomes eligible for garbage
34collection when it is no longer possible for any code to access it. For instance, when an item is removed
35from the Stack, the associated Node instance becomes eligible for garbage collection.
36The example
37class Test
38{
39 |
static void Main() { |
40 |
Stack s = new Stack(); |
41 |
for (int i = 0; i < 10; i++) |
42 |
s.Push(i); |
43 |
s = null; |
44}
45}
46shows code that uses the Stack class. A Stack is created and initialized with 10 elements, and then
47assigned the value null. Once the variable s is assigned null, the Stack and the associated 10 Node
48instances become eligible for garbage collection. The garbage collector is permitted to clean up immediately,
49but is not required to do so.
50The garbage collector underlying C# might work by moving objects around in memory, but this motion is
51invisible to most C# developers. For developers who are generally content with automatic memory
52management but sometimes need fine-grained control or that extra bit of performance, C# provides the
53ability to write “unsafe” code. Such code can deal directly with pointer types and object addresses, however,
54C# requires the programmer to fix objects to temporarily prevent the garbage collector from moving them.
55This “unsafe” code feature is in fact a “safe” feature from the perspective of both developers and users.
56Unsafe code shall be clearly marked in the code with the modifier unsafe, so developers can't possibly use
57unsafe language features accidentally, and the compiler and the execution engine work together to ensure
26