- •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 25 Unsafe code
1The order in which members are packed into a struct is unspecified.
2For alignment purposes, there can be unnamed padding at the beginning of a struct, within a struct, and at
3the end of the struct. The contents of the bits used as padding are indeterminate.
4When applied to an operand that has struct type, the result is the total number of bytes in a variable of that
5type, including any padding.
625.6 The fixed statement
7In an unsafe context, the embedded-statement (§15) production permits an additional construct, the fixed
8statement, which is used to “fix” a moveable variable such that its address remains constant for the duration
9of the statement.
10embedded-statement:
11 |
... |
12 |
fixed-statement |
13 |
fixed-statement: |
14 |
fixed ( pointer-type fixed-pointer-declarators ) embedded-statement |
15 |
fixed-pointer-declarators: |
16 |
fixed-pointer-declarator |
17 |
fixed-pointer-declarators , fixed-pointer-declarator |
18 |
fixed-pointer-declarator: |
19 |
identifier = fixed-pointer-initializer |
20 |
|
21 |
fixed-pointer-initializer: |
22 |
& variable-reference |
23 |
expression |
24Each fixed-pointer-declarator declares a local variable of the given pointer-type and initializes that local
25variable with the address computed by the corresponding fixed-pointer-initializer. A local variable declared
26in a fixed statement is accessible in any fixed-pointer-initializers occurring to the right of that variable’s
27declaration, and in the embedded-statement of the fixed statement. A local variable declared by a fixed
28statement is considered read-only. A compile-time error occurs if the embedded statement attempts to
29modify this local variable (via assignment or the ++ and -- operators) or pass it as a ref or out parameter.
30It is an error to use a captured local variable (§14.5.14.3.1), value parameter, or parameter array in a fixed
31statement.
32A fixed-pointer-initializer can be one of the following:
33• The token “&” followed by a variable-reference (§12.3.3.27) to a moveable variable (§25.3) of an
34unmanaged type T, provided the type T* is implicitly convertible to the pointer type given in the fixed
35statement. In this case, the initializer computes the address of the given variable, and the variable is
36guaranteed to remain at a fixed address for the duration of the fixed statement.
37• An expression of an array-type with elements of an unmanaged type T, provided the type T* is
38implicitly convertible to the pointer type given in the fixed statement. In this case, the initializer
39computes the address of the first element in the array, and the entire array is guaranteed to remain at a
40fixed address for the duration of the fixed statement. The behavior of the fixed statement is
41implementation-defined if the array expression is null or if the array has zero elements.
42• An expression of type string, provided the type char* is implicitly convertible to the pointer type
43given in the fixed statement. In this case, the initializer computes the address of the first character in
44the string, and the entire string is guaranteed to remain at a fixed address for the duration of the fixed
45statement. The behavior of the fixed statement is implementation-defined if the string expression is
46null.
391
C# LANGUAGE SPECIFICATION
1For each address computed by a fixed-pointer-initializer the fixed statement ensures that the variable
2referenced by the address is not subject to relocation or disposal by the garbage collector for the duration of
3the fixed statement. [Example: If the address computed by a fixed-pointer-initializer references a field of
4an object or an element of an array instance, the fixed statement guarantees that the containing object
5instance is not relocated or disposed of during the lifetime of the statement. end example]
6It is the programmer's responsibility to ensure that pointers created by fixed statements do not survive
7beyond execution of those statements. [Example: When pointers created by fixed statements are passed to
8external APIs, it is the programmer’s responsibility to ensure that the APIs retain no memory of these
9pointers. end example]
10Fixed objects can cause fragmentation of the heap (because they can’t be moved). For that reason, objects
11should be fixed only when absolutely necessary and then only for the shortest amount of time possible.
12[Example: The example
13class Test
14{
15 |
static int x; |
16 |
int y; |
17 |
unsafe static void F(int* p) { |
18 |
*p = 1; |
19 |
} |
20 |
static void Main() { |
21 |
Test t = new Test(); |
22 |
int[] a = new int[10]; |
23 |
unsafe { |
24 |
fixed (int* p = &x) F(p); |
25 |
fixed (int* p = &t.y) F(p); |
26 |
fixed (int* p = &a[0]) F(p); |
27 |
fixed (int* p = a) F(p); |
28 |
} |
29}
30}
31demonstrates several uses of the fixed statement. The first statement fixes and obtains the address of a
32static field, the second statement fixes and obtains the address of an instance field, and the third statement
33fixes and obtains the address of an array element. In each case it would have been an error to use the regular
34& operator since the variables are all classified as moveable variables.
35The third and fourth fixed statements in the example above produce identical results. In general, for an
36array instance a, specifying &a[0] in a fixed statement is the same as simply specifying a.
37Here’s another example of the fixed statement, this time using string:
38class Test
39{
40 |
static string name |
= "xx"; |
41 |
unsafe static void |
F(char* p) { |
42 |
for (int i = 0; p[i] != '\0'; ++i) |
|
43 |
Console.WriteLine(p[i]); |
|
44 |
} |
|
45 |
static void Main() { |
|
46 |
unsafe { |
|
47 |
fixed (char* p = name) F(p); |
|
48 |
fixed (char* p = "xx") F(p); |
|
49 |
} |
|
50}
51}
52end example]
53In an unsafe context array elements of single-dimensional arrays are stored in increasing index order,
54starting with index 0 and ending with index Length – 1. For multi-dimensional arrays, array elements are
392
Chapter 25 Unsafe code
1stored such that the indices of the rightmost dimension are increased first, then the next left dimension, and
2so on to the left.
3Within a fixed statement that obtains a pointer p to an array instance a, the pointer values ranging from p
4to p + a.Length - 1 represent addresses of the elements in the array. Likewise, the variables ranging from
5p[0] to p[a.Length - 1] represent the actual array elements. Given the way in which arrays are stored ,
6we can treat an array of any dimension as though it were linear. [Example:
7using System;
8class Test
9{
10 |
static void Main() { |
11 |
int[,,] a = new int[2,3,4]; |
12 |
unsafe { |
13 |
fixed (int* p = a) { |
14 |
for (int i = 0; i < a.Length; ++i) // treat as linear |
15 |
p[i] = i; |
16 |
} |
17 |
} |
18 |
for (int i = 0; i < 2; ++i) |
19 |
for (int j = 0; j < 3; ++j) { |
20 |
for (int k = 0; k < 4; ++k) |
21 |
Console.Write("[{0},{1},{2}] = {3,2} ", i, j, k, |
22 |
a[i,j,k]); |
23 |
Console.WriteLine(); |
24 |
} |
25}
26}
27which produces the output:
28 |
[0,0,0] = |
0 |
[0,0,1] = |
1 |
[0,0,2] = |
2 |
[0,0,3] = |
3 |
29 |
[0,1,0] = |
4 |
[0,1,1] = |
5 |
[0,1,2] = |
6 |
[0,1,3] = |
7 |
30 |
[0,2,0] = |
8 |
[0,2,1] = |
9 |
[0,2,2] = 10 |
[0,2,3] = 11 |
||
31[1,0,0] = 12 [1,0,1] = 13 [1,0,2] = 14 [1,0,3] = 15
32[1,1,0] = 16 [1,1,1] = 17 [1,1,2] = 18 [1,1,3] = 19
33[1,2,0] = 20 [1,2,1] = 21 [1,2,2] = 22 [1,2,3] = 23
34end example]
35[Example: In the following code
36class Test
37{
38 |
unsafe static void Fill(int* p, int count, int value) { |
39 |
for (; count != 0; count--) *p++ = value; |
40 |
} |
41 |
static void Main() { |
42 |
int[] a = new int[100]; |
43 |
unsafe { |
44 |
fixed (int* p = a) Fill(p, 100, -1); |
45 |
} |
46}
47}
48a fixed statement is used to fix an array so its address can be passed to a method that takes a pointer. end
49example]
50A char* value produced by fixing a non-null string instance always points to a null-terminated string.
51Within a fixed statement that obtains a pointer p to a string instance s, the pointer values ranging from p to
52 p + s.Length - 1 represent addresses of the characters in the string, and the pointer value
53p + s.Length always points to a null character (the character with value '\0').
54Modifying objects of managed type through fixed pointers can result in undefined behavior. [Note: For
55example, because strings are immutable, it is the programmer’s responsibility to ensure that the characters
56referenced by a pointer to a fixed string are not modified. end note]
393
C# LANGUAGE SPECIFICATION
1[Note: The automatic null-termination of strings is particularly convenient when calling external APIs that
2expect “C-style” strings. Note, however, that a string instance is permitted to contain null characters. If such
3null characters are present, the string will appear truncated when treated as a null-terminated char*. end
4note]
525.7 Stack allocation
6In an unsafe context, a local variable declaration (§15.5.1) can include a stack allocation initializer, which
7allocates memory from the call stack.
8local-variable-initializer:
9 |
expression |
10 |
array-initializer |
11 |
stackalloc-initializer |
12 |
stackalloc-initializer: |
13 |
stackalloc unmanaged-type [ expression ] |
14The unmanaged-type indicates the type of the items that will be stored in the newly allocated location, and
15the expression indicates the number of these items. Taken together, these specify the required allocation
16size. Since the size of a stack allocation cannot be negative, it is a compile-time error to specify the number
17of items as a constant-expression that evaluates to a negative value.
18A stack allocation initializer of the form stackalloc T[E] requires T to be an unmanaged type (§25.2) and
19E to be an expression convertible to type int. The construct allocates E * sizeof(T) bytes from the call
20stack and returns a pointer, of type T*, to the newly allocated block. If E is a negative value, then the
21behavior is undefined. If E is zero, then no allocation is made, and the pointer returned is implementation-
22defined. If there is not enough memory available to allocate a block of the given size, a
23System.StackOverflowException is thrown.
24The content of the newly allocated memory is undefined.
25Stack allocation initializers are not permitted in catch or finally blocks (§15.10).
26[Note: There is no way to explicitly free memory allocated using stackalloc. end note] All stack-
27allocated memory blocks created during the execution of a function member are automatically discarded
28when that function member returns. [Note: This corresponds to the alloca function, an extension
29commonly found in C and C++ implementations. end note]
30[Example: In the following code
31using System;
32class Test
33{
34 |
static string IntToString(int value) { |
35 |
int n = value >= 0 ? value : -value; |
36 |
unsafe { |
37 |
char* buffer = stackalloc char[16]; |
38 |
char* p = buffer + 16; |
39 |
do { |
40 |
*--p = (char)(n % 10 + '0'); |
41 |
n /= 10; |
42 |
} while (n != 0); |
43 |
if (value < 0) *--p = '-'; |
44 |
return new string(p, 0, (int)(buffer + 16 - p)); |
45 |
} |
46 |
} |
47 |
static void Main() { |
48 |
Console.WriteLine(IntToString(12345)); |
49 |
Console.WriteLine(IntToString(-999)); |
50}
51}
394
