+ All Categories
Home > Documents > C# Language Specification

C# Language Specification

Date post: 11-Apr-2015
Category:
Upload: api-3709550
View: 207 times
Download: 1 times
Share this document with a friend
548
Ecma TC39-TG2/2004/14 C # Language Specification Working Draft 2.7, Jun, 2004 Public Review Document
Transcript
Page 1: C# Language Specification

Ecma TC39-TG2/2004/14

C#

Language Specification Working Draft 2.7, Jun, 2004

Public Review Document

Page 2: C# Language Specification
Page 3: C# Language Specification

Table of Contents

iii

1

Table of Contents 2

3

Foreword......................................................................................................................................................xvii 4

Introduction.................................................................................................................................................. xix 5

1. Scope............................................................................................................................................................. 1 6

2. Conformance ............................................................................................................................................... 3 7

3. Normative references .................................................................................................................................. 5 8

4. Definitions .................................................................................................................................................... 7 9

5. Notational conventions................................................................................................................................ 9 10

6. Acronyms and abbreviations ................................................................................................................... 11 11

7. General description................................................................................................................................... 13 12

8. Language overview.................................................................................................................................... 15 13

8.1 Getting started ....................................................................................................................................... 15 14 8.2 Types ..................................................................................................................................................... 16 15

8.2.1 Predefined types............................................................................................................................. 17 16 8.2.2 Conversions.................................................................................................................................... 19 17 8.2.3 Array types..................................................................................................................................... 20 18 8.2.4 Type system unification ................................................................................................................. 22 19

8.3 Variables and parameters ...................................................................................................................... 22 20 8.4 Automatic memory management .......................................................................................................... 25 21 8.5 Expressions ........................................................................................................................................... 27 22 8.6 Statements ............................................................................................................................................. 28 23 8.7 Classes................................................................................................................................................... 31 24

8.7.1 Constants........................................................................................................................................ 33 25 8.7.2 Fields.............................................................................................................................................. 33 26 8.7.3 Methods.......................................................................................................................................... 34 27 8.7.4 Properties ....................................................................................................................................... 35 28 8.7.5 Events............................................................................................................................................. 36 29 8.7.6 Operators........................................................................................................................................ 37 30 8.7.7 Indexers.......................................................................................................................................... 38 31 8.7.8 Instance constructors...................................................................................................................... 39 32 8.7.9 Destructors ..................................................................................................................................... 40 33 8.7.10 Static constructors ........................................................................................................................ 40 34 8.7.11 Inheritance.................................................................................................................................... 41 35 8.7.12 Static classes ................................................................................................................................ 42 36 8.7.13 Partial type declarations ............................................................................................................... 42 37

8.8 Structs.................................................................................................................................................... 43 38 8.9 Interfaces ............................................................................................................................................... 44 39 8.10 Delegates ............................................................................................................................................. 45 40 8.11 Enums.................................................................................................................................................. 46 41 8.12 Namespaces and assemblies................................................................................................................ 46 42 8.13 Versioning ........................................................................................................................................... 48 43 8.14 Extern Aliases ..................................................................................................................................... 49 44 8.15 Attributes............................................................................................................................................. 51 45 8.16 Generics............................................................................................................................................... 52 46

Page 4: C# Language Specification

C# LANGUAGE SPECIFICATION

iv

8.16.1 Why generics?.............................................................................................................................. 52 1 8.16.2 Creating and consuming generics ................................................................................................ 53 2 8.16.3 Multiple type parameters.............................................................................................................. 54 3 8.16.4 Constraints ................................................................................................................................... 54 4 8.16.5 Generic methods .......................................................................................................................... 56 5

8.17 Anonymous methods........................................................................................................................... 56 6 8.18 Iterators ............................................................................................................................................... 59 7

9. Lexical structure........................................................................................................................................ 63 8

9.1 Programs ............................................................................................................................................... 63 9 9.2 Grammars .............................................................................................................................................. 63 10

9.2.1 Lexical grammar ............................................................................................................................ 63 11 9.2.2 Syntactic grammar ......................................................................................................................... 63 12 9.2.3 Grammar ambiguities..................................................................................................................... 64 13

9.3 Lexical analysis ..................................................................................................................................... 64 14 9.3.1 Line terminators ............................................................................................................................. 65 15 9.3.2 Comments ...................................................................................................................................... 65 16 9.3.3 White space .................................................................................................................................... 66 17

9.4 Tokens ................................................................................................................................................... 67 18 9.4.1 Unicode escape sequences ............................................................................................................. 67 19 9.4.2 Identifiers ....................................................................................................................................... 68 20 9.4.3 Keywords ....................................................................................................................................... 69 21 9.4.4 Literals ........................................................................................................................................... 70 22

9.4.4.1 Boolean literals........................................................................................................................ 70 23 9.4.4.2 Integer literals.......................................................................................................................... 70 24 9.4.4.3 Real literals.............................................................................................................................. 71 25 9.4.4.4 Character literals ..................................................................................................................... 72 26 9.4.4.5 String literals ........................................................................................................................... 73 27 9.4.4.6 The null literal ......................................................................................................................... 74 28

9.4.5 Operators and punctuators.............................................................................................................. 75 29 9.5 Pre-processing directives....................................................................................................................... 75 30

9.5.1 Conditional compilation symbols .................................................................................................. 76 31 9.5.2 Pre-processing expressions ............................................................................................................ 76 32 9.5.3 Declaration directives..................................................................................................................... 77 33 9.5.4 Conditional compilation directives ................................................................................................ 78 34 9.5.5 Diagnostic directives...................................................................................................................... 80 35 9.5.6 Region control................................................................................................................................ 81 36 9.5.7 Line directives................................................................................................................................ 81 37 9.5.8 Pragma directives ........................................................................................................................... 82 38

10. Basic concepts .......................................................................................................................................... 83 39

10.1 Application startup .............................................................................................................................. 83 40 10.2 Application termination....................................................................................................................... 84 41 10.3 Declarations......................................................................................................................................... 84 42 10.4 Members.............................................................................................................................................. 87 43

10.4.1 Namespace members.................................................................................................................... 87 44 10.4.2 Struct members ............................................................................................................................ 87 45 10.4.3 Enumeration members ................................................................................................................. 87 46 10.4.4 Class members ............................................................................................................................. 87 47 10.4.5 Interface members........................................................................................................................ 88 48 10.4.6 Array members............................................................................................................................. 88 49 10.4.7 Delegate members........................................................................................................................ 88 50

10.5 Member access .................................................................................................................................... 88 51 10.5.1 Declared accessibility................................................................................................................... 88 52 10.5.2 Accessibility domains .................................................................................................................. 89 53

Page 5: C# Language Specification

Table of Contents

v

10.5.3 Protected access for instance members ........................................................................................ 91 1 10.5.4 Accessibility constraints............................................................................................................... 92 2

10.6 Signatures and overloading ................................................................................................................. 93 3 10.7 Scopes ................................................................................................................................................. 94 4

10.7.1 Name hiding................................................................................................................................. 96 5 10.7.1.1 Hiding through nesting.......................................................................................................... 96 6 10.7.1.2 Hiding through inheritance.................................................................................................... 97 7

10.8 Namespace and type names................................................................................................................. 98 8 10.8.1 Unqualified name....................................................................................................................... 100 9 10.8.2 Fully qualified names................................................................................................................. 100 10

10.9 Automatic memory management ...................................................................................................... 101 11 10.10 Execution order ............................................................................................................................... 103 12

11. Types ...................................................................................................................................................... 105 13

11.1 Value types........................................................................................................................................ 105 14 11.1.1 The System.ValueType type ...................................................................................................... 106 15 11.1.2 Default constructors ................................................................................................................... 106 16 11.1.3 Struct types................................................................................................................................. 107 17 11.1.4 Simple types............................................................................................................................... 107 18 11.1.5 Integral types.............................................................................................................................. 108 19 11.1.6 Floating point types.................................................................................................................... 109 20 11.1.7 The decimal type........................................................................................................................ 109 21 11.1.8 The bool type ............................................................................................................................. 110 22 11.1.9 Enumeration types...................................................................................................................... 110 23

11.2 Reference types ................................................................................................................................. 110 24 11.2.1 Class types.................................................................................................................................. 111 25 11.2.2 The object type........................................................................................................................... 111 26 11.2.3 The string type ........................................................................................................................... 111 27 11.2.4 Interface types ............................................................................................................................ 111 28 11.2.5 Array types................................................................................................................................. 111 29 11.2.6 Delegate types ............................................................................................................................ 112 30

11.3 Boxing and unboxing ........................................................................................................................ 112 31 11.3.1 Boxing conversions.................................................................................................................... 112 32 11.3.2 Unboxing conversions................................................................................................................ 113 33

12. Variables ................................................................................................................................................ 115 34

12.1 Variable categories ............................................................................................................................ 115 35 12.1.1 Static variables ........................................................................................................................... 115 36 12.1.2 Instance variables ....................................................................................................................... 115 37

12.1.2.1 Instance variables in classes................................................................................................ 115 38 12.1.2.2 Instance variables in structs................................................................................................. 116 39

12.1.3 Array elements ........................................................................................................................... 116 40 12.1.4 Value parameters........................................................................................................................ 116 41 12.1.5 Reference parameters ................................................................................................................. 116 42 12.1.6 Output parameters ...................................................................................................................... 116 43 12.1.7 Local variables ........................................................................................................................... 117 44

12.2 Default values.................................................................................................................................... 117 45 12.3 Definite assignment........................................................................................................................... 118 46

12.3.1 Initially assigned variables......................................................................................................... 119 47 12.3.2 Initially unassigned variables ..................................................................................................... 119 48 12.3.3 Precise rules for determining definite assignment ..................................................................... 119 49

12.3.3.1 General rules for statements ................................................................................................ 120 50 12.3.3.2 Block statements, checked, and unchecked statements....................................................... 120 51 12.3.3.3 Expression statements ......................................................................................................... 120 52 12.3.3.4 Declaration statements ........................................................................................................ 120 53

Page 6: C# Language Specification

C# LANGUAGE SPECIFICATION

vi

12.3.3.5 If statements ........................................................................................................................ 120 1 12.3.3.6 Switch statements................................................................................................................ 121 2 12.3.3.7 While statements ................................................................................................................. 121 3 12.3.3.8 Do statements ...................................................................................................................... 121 4 12.3.3.9 For statements ..................................................................................................................... 121 5 12.3.3.10 Break, continue, and goto statements................................................................................ 122 6 12.3.3.11 Throw statements .............................................................................................................. 122 7 12.3.3.12 Return statements .............................................................................................................. 122 8 12.3.3.13 Try-catch statements ......................................................................................................... 122 9 12.3.3.14 Try-finally statements ....................................................................................................... 122 10 12.3.3.15 Try-catch-finally statements.............................................................................................. 123 11 12.3.3.16 Foreach statements ............................................................................................................ 124 12 12.3.3.17 Using statements ............................................................................................................... 124 13 12.3.3.18 Lock statements................................................................................................................. 124 14 12.3.3.19 General rules for simple expressions ................................................................................ 124 15 12.3.3.20 General rules for expressions with embedded expressions ............................................... 124 16 12.3.3.21 Invocation expressions and object creation expressions ................................................... 125 17 12.3.3.22 Simple assignment expressions......................................................................................... 125 18 12.3.3.23 && expressions ................................................................................................................. 126 19 12.3.3.24 || expressions...................................................................................................................... 126 20 12.3.3.25 ! expressions...................................................................................................................... 127 21 12.3.3.26 ?: expressions .................................................................................................................... 128 22 12.3.3.27 Anonymous method expressions....................................................................................... 128 23 12.3.3.28 Yield statements ................................................................................................................ 129 24

12.4 Variable references............................................................................................................................ 129 25 12.5 Atomicity of variable references ....................................................................................................... 129 26

13. Conversions............................................................................................................................................ 131 27

13.1 Implicit conversions .......................................................................................................................... 131 28 13.1.1 Identity conversion..................................................................................................................... 131 29 13.1.2 Implicit numeric conversions..................................................................................................... 131 30 13.1.3 Implicit enumeration conversions .............................................................................................. 132 31 13.1.4 Implicit reference conversions ................................................................................................... 132 32 13.1.5 Boxing conversions.................................................................................................................... 133 33 13.1.6 Implicit type parameter conversions .......................................................................................... 133 34 13.1.7 Implicit constant expression conversions................................................................................... 133 35 13.1.8 User-defined implicit conversions ............................................................................................. 134 36

13.2 Explicit conversions .......................................................................................................................... 134 37 13.2.1 Explicit numeric conversions..................................................................................................... 134 38 13.2.2 Explicit enumeration conversions .............................................................................................. 135 39 13.2.3 Explicit reference conversions ................................................................................................... 136 40 13.2.4 Unboxing conversions................................................................................................................ 137 41 13.2.5 Unboxing conversions are described further in §11.3.2.Explicit type parameter conversions .. 137 42 13.2.6 User-defined explicit conversions.............................................................................................. 137 43

13.3 Standard conversions......................................................................................................................... 138 44 13.3.1 Standard implicit conversions .................................................................................................... 138 45 13.3.2 Standard explicit conversions .................................................................................................... 138 46

13.4 User-defined conversions.................................................................................................................. 138 47 13.4.1 Permitted user-defined conversions ........................................................................................... 138 48 13.4.2 Evaluation of user-defined conversions ..................................................................................... 138 49 13.4.3 User-defined implicit conversions ............................................................................................. 139 50 13.4.4 User-defined explicit conversions.............................................................................................. 140 51

13.5 Anonymous method conversions ...................................................................................................... 141 52 13.6 Method group conversions ................................................................................................................ 142 53

Page 7: C# Language Specification

Table of Contents

vii

14. Expressions ............................................................................................................................................ 145 1

14.1 Expression classifications ................................................................................................................. 145 2 14.1.1 Values of expressions................................................................................................................. 146 3

14.2 Operators ........................................................................................................................................... 146 4 14.2.1 Operator precedence and associativity....................................................................................... 146 5 14.2.2 Operator overloading ................................................................................................................. 147 6 14.2.3 Unary operator overload resolution ........................................................................................... 148 7 14.2.4 Binary operator overload resolution........................................................................................... 149 8 14.2.5 Candidate user-defined operators............................................................................................... 149 9 14.2.6 Numeric promotions................................................................................................................... 149 10

14.2.6.1 Unary numeric promotions.................................................................................................. 150 11 14.2.6.2 Binary numeric promotions................................................................................................. 150 12

14.3 Member lookup ................................................................................................................................. 151 13 14.3.1 Base types .................................................................................................................................. 152 14

14.4 Function members ............................................................................................................................. 152 15 14.4.1 Argument lists ............................................................................................................................ 154 16 14.4.2 Overload resolution.................................................................................................................... 157 17

14.4.2.1 Applicable function member............................................................................................... 157 18 14.4.2.2 Better function member....................................................................................................... 158 19 14.4.2.3 Better conversion ................................................................................................................ 158 20

14.4.3 Function member invocation...................................................................................................... 158 21 14.4.3.1 Invocations on boxed instances........................................................................................... 160 22

14.5 Primary expressions .......................................................................................................................... 160 23 14.5.1 Literals ....................................................................................................................................... 161 24 14.5.2 Simple names ............................................................................................................................. 161 25

14.5.2.1 Invariant meaning in blocks ................................................................................................ 162 26 14.5.3 Parenthesized expressions.......................................................................................................... 163 27 14.5.4 Member access ........................................................................................................................... 163 28

14.5.4.1 Identical simple names and type names .............................................................................. 165 29 14.5.5 Invocation expressions ............................................................................................................... 165 30

14.5.5.1 Method invocations ............................................................................................................. 165 31 14.5.5.2 Delegate invocations ........................................................................................................... 167 32

14.5.6 Element access ........................................................................................................................... 167 33 14.5.6.1 Array access ........................................................................................................................ 167 34 14.5.6.2 Indexer access ..................................................................................................................... 168 35

14.5.7 This access ................................................................................................................................. 168 36 14.5.8 Base access................................................................................................................................. 169 37 14.5.9 Postfix increment and decrement operators ............................................................................... 170 38 14.5.10 The new operator...................................................................................................................... 171 39

14.5.10.1 Object creation expressions............................................................................................... 171 40 14.5.10.2 Array creation expressions ................................................................................................ 172 41 14.5.10.3 Delegate creation expressions ........................................................................................... 174 42

14.5.11 The typeof operator .................................................................................................................. 176 43 14.5.12 The checked and unchecked operators..................................................................................... 178 44 14.5.13 Default value expression .......................................................................................................... 180 45 14.5.14 Anonymous methods................................................................................................................ 180 46

14.5.14.1 Anonymous method signatures ......................................................................................... 181 47 14.5.14.2 Anonymous method blocks............................................................................................... 181 48 14.5.14.3 Outer variables .................................................................................................................. 181 49 14.5.14.4 Anonymous method evaluation......................................................................................... 184 50 14.5.14.5 Implementation example ................................................................................................... 185 51

14.6 Unary expressions ............................................................................................................................. 187 52 14.6.1 Unary plus operator.................................................................................................................... 187 53 14.6.2 Unary minus operator................................................................................................................. 187 54

Page 8: C# Language Specification

C# LANGUAGE SPECIFICATION

viii

14.6.3 Logical negation operator .......................................................................................................... 188 1 14.6.4 Bitwise complement operator .................................................................................................... 188 2 14.6.5 Prefix increment and decrement operators................................................................................. 189 3 14.6.6 Cast expressions ......................................................................................................................... 189 4

14.7 Arithmetic operators.......................................................................................................................... 190 5 14.7.1 Multiplication operator............................................................................................................... 190 6 14.7.2 Division operator........................................................................................................................ 191 7 14.7.3 Remainder operator.................................................................................................................... 192 8 14.7.4 Addition operator ....................................................................................................................... 193 9 14.7.5 Subtraction operator ................................................................................................................... 195 10

14.8 Shift operators ................................................................................................................................... 197 11 14.9 Relational and type-testing operators ................................................................................................ 198 12

14.9.1 Integer comparison operators ..................................................................................................... 199 13 14.9.2 Floating-point comparison operators ......................................................................................... 199 14 14.9.3 Decimal comparison operators................................................................................................... 200 15 14.9.4 Boolean equality operators......................................................................................................... 200 16 14.9.5 Enumeration comparison operators............................................................................................ 201 17 14.9.6 Reference type equality operators .............................................................................................. 201 18 14.9.7 String equality operators ............................................................................................................ 203 19 14.9.8 Delegate equality operators........................................................................................................ 203 20 14.9.9 The is operator ........................................................................................................................... 204 21 14.9.10 The as operator......................................................................................................................... 204 22

14.10 Logical operators............................................................................................................................. 205 23 14.10.1 Integer logical operators........................................................................................................... 206 24 14.10.2 Enumeration logical operators ................................................................................................. 206 25 14.10.3 Boolean logical operators......................................................................................................... 206 26

14.11 Conditional logical operators .......................................................................................................... 206 27 14.11.1 Boolean conditional logical operators...................................................................................... 207 28 14.11.2 User-defined conditional logical operators .............................................................................. 207 29

14.12 Conditional operator........................................................................................................................ 208 30 14.13 Assignment operators ...................................................................................................................... 209 31

14.13.1 Simple assignment ................................................................................................................... 209 32 14.13.2 Compound assignment ............................................................................................................. 211 33 14.13.3 Event assignment ..................................................................................................................... 211 34

14.14 Expression ....................................................................................................................................... 212 35 14.15 Constant expressions ....................................................................................................................... 212 36 14.16 Boolean expressions........................................................................................................................ 213 37

15. Statements.............................................................................................................................................. 215 38

15.1 End points and reachability ............................................................................................................... 215 39 15.2 Blocks................................................................................................................................................ 217 40

15.2.1 Statement lists ............................................................................................................................ 217 41 15.3 The empty statement ......................................................................................................................... 217 42 15.4 Labeled statements ............................................................................................................................ 218 43 15.5 Declaration statements ...................................................................................................................... 218 44

15.5.1 Local variable declarations ........................................................................................................ 219 45 15.5.2 Local constant declarations ........................................................................................................ 219 46

15.6 Expression statements ....................................................................................................................... 220 47 15.7 Selection statements .......................................................................................................................... 220 48

15.7.1 The if statement.......................................................................................................................... 220 49 15.7.2 The switch statement.................................................................................................................. 221 50

15.8 Iteration statements ........................................................................................................................... 224 51 15.8.1 The while statement ................................................................................................................... 224 52 15.8.2 The do statement ........................................................................................................................ 225 53 15.8.3 The for statement ....................................................................................................................... 225 54

Page 9: C# Language Specification

Table of Contents

ix

15.8.4 The foreach statement ................................................................................................................ 226 1 15.9 Jump statements ................................................................................................................................ 229 2

15.9.1 The break statement ................................................................................................................... 230 3 15.9.2 The continue statement .............................................................................................................. 231 4 15.9.3 The goto statement ..................................................................................................................... 231 5 15.9.4 The return statement................................................................................................................... 232 6 15.9.5 The throw statement................................................................................................................... 233 7

15.10 The try statement............................................................................................................................. 234 8 15.11 The checked and unchecked statements .......................................................................................... 236 9 15.12 The lock statement .......................................................................................................................... 237 10 15.13 The using statement......................................................................................................................... 237 11 15.14 The yield statement ......................................................................................................................... 239 12

16. Namespaces............................................................................................................................................ 243 13

16.1 Compilation units .............................................................................................................................. 243 14 16.2 Namespace declarations .................................................................................................................... 243 15 16.3 Extern alias directives ....................................................................................................................... 244 16 16.4 Using directives................................................................................................................................. 245 17

16.4.1 Using alias directives ................................................................................................................. 245 18 16.4.2 Using namespace directives ....................................................................................................... 249 19

16.5 Namespace members......................................................................................................................... 251 20 16.6 Type declarations .............................................................................................................................. 251 21 16.7 Qualified alias member ..................................................................................................................... 251 22

17. Classes .................................................................................................................................................... 255 23

17.1 Class declarations.............................................................................................................................. 255 24 17.1.1 Class modifiers........................................................................................................................... 255 25

17.1.1.1 Abstract classes ................................................................................................................... 256 26 17.1.1.2 Sealed classes ...................................................................................................................... 256 27 17.1.1.3 Static classes........................................................................................................................ 256 28

17.1.2 Class base specification.............................................................................................................. 257 29 17.1.2.1 Base classes......................................................................................................................... 258 30 17.1.2.2 Interface implementations ................................................................................................... 259 31

17.1.3 Class body .................................................................................................................................. 260 32 17.1.4 Partial declarations ..................................................................................................................... 260 33

17.2 Class members................................................................................................................................... 261 34 17.2.1 Inheritance.................................................................................................................................. 263 35 17.2.2 The new modifier ....................................................................................................................... 264 36 17.2.3 Access modifiers ........................................................................................................................ 264 37 17.2.4 Constituent types........................................................................................................................ 264 38 17.2.5 Static and instance members ...................................................................................................... 264 39 17.2.6 Nested types ............................................................................................................................... 265 40

17.2.6.1 Fully qualified name ........................................................................................................... 265 41 17.2.6.2 Declared accessibility.......................................................................................................... 266 42 17.2.6.3 Hiding.................................................................................................................................. 266 43 17.2.6.4 this access............................................................................................................................ 267 44 17.2.6.5 Access to private and protected members of the containing type ....................................... 267 45

17.2.7 Reserved member names............................................................................................................ 268 46 17.2.7.1 Member names reserved for properties ............................................................................... 268 47 17.2.7.2 Member names reserved for events..................................................................................... 269 48 17.2.7.3 Member names reserved for indexers ................................................................................. 269 49 17.2.7.4 Member names reserved for destructors ............................................................................. 269 50

17.3 Constants ........................................................................................................................................... 269 51 17.4 Fields ................................................................................................................................................. 271 52

17.4.1 Static and instance fields............................................................................................................ 272 53

Page 10: C# Language Specification

C# LANGUAGE SPECIFICATION

x

17.4.2 Readonly fields .......................................................................................................................... 272 1 17.4.2.1 Using static readonly fields for constants............................................................................ 273 2 17.4.2.2 Versioning of constants and static readonly fields.............................................................. 273 3

17.4.3 Volatile fields ............................................................................................................................. 274 4 17.4.4 Field initialization ...................................................................................................................... 275 5 17.4.5 Variable initializers .................................................................................................................... 275 6

17.4.5.1 Static field initialization ...................................................................................................... 276 7 17.4.5.2 Instance field initialization .................................................................................................. 277 8

17.5 Methods............................................................................................................................................. 277 9 17.5.1 Method parameters..................................................................................................................... 279 10

17.5.1.1 Value parameters................................................................................................................. 280 11 17.5.1.2 Reference parameters .......................................................................................................... 280 12 17.5.1.3 Output parameters ............................................................................................................... 281 13 17.5.1.4 Parameter arrays.................................................................................................................. 282 14

17.5.2 Static and instance methods ....................................................................................................... 284 15 17.5.3 Virtual methods.......................................................................................................................... 284 16 17.5.4 Override methods....................................................................................................................... 286 17 17.5.5 Sealed methods .......................................................................................................................... 287 18 17.5.6 Abstract methods........................................................................................................................ 288 19 17.5.7 External methods........................................................................................................................ 289 20 17.5.8 Method body .............................................................................................................................. 290 21 17.5.9 Method overloading ................................................................................................................... 290 22

17.6 Properties........................................................................................................................................... 290 23 17.6.1 Static and instance properties..................................................................................................... 291 24 17.6.2 Accessors ................................................................................................................................... 292 25 17.6.3 Virtual, sealed, override, and abstract accessors ........................................................................ 297 26

17.7 Events ................................................................................................................................................ 299 27 17.7.1 Field-like events ......................................................................................................................... 301 28 17.7.2 Event accessors .......................................................................................................................... 302 29 17.7.3 Static and instance events........................................................................................................... 303 30 17.7.4 Virtual, sealed, override, and abstract accessors ........................................................................ 303 31

17.8 Indexers ............................................................................................................................................. 304 32 17.8.1 Indexer overloading ................................................................................................................... 307 33

17.9 Operators ........................................................................................................................................... 307 34 17.9.1 Unary operators.......................................................................................................................... 309 35 17.9.2 Binary operators ......................................................................................................................... 309 36 17.9.3 Conversion operators ................................................................................................................. 310 37

17.10 Instance constructors ....................................................................................................................... 311 38 17.10.1 Constructor initializers ............................................................................................................. 312 39 17.10.2 Instance variable initializers..................................................................................................... 313 40 17.10.3 Constructor execution .............................................................................................................. 313 41 17.10.4 Default constructors ................................................................................................................. 314 42 17.10.5 Private constructors.................................................................................................................. 315 43 17.10.6 Optional instance constructor parameters ................................................................................ 315 44

17.11 Static constructors ........................................................................................................................... 316 45 17.12 Destructors ...................................................................................................................................... 318 46

18. Structs .................................................................................................................................................... 321 47

18.1 Struct declarations ............................................................................................................................. 321 48 18.1.1 Struct modifiers.......................................................................................................................... 321 49 18.1.2 Struct interfaces.......................................................................................................................... 322 50 18.1.3 Struct body ................................................................................................................................. 322 51

18.2 Struct members.................................................................................................................................. 322 52 18.3 Class and struct differences............................................................................................................... 322 53

18.3.1 Value semantics ......................................................................................................................... 322 54

Page 11: C# Language Specification

Table of Contents

xi

18.3.2 Inheritance.................................................................................................................................. 323 1 18.3.3 Assignment................................................................................................................................. 323 2 18.3.4 Default values ............................................................................................................................ 323 3 18.3.5 Boxing and unboxing ................................................................................................................. 324 4 18.3.6 Meaning of this .......................................................................................................................... 324 5 18.3.7 Field initializers.......................................................................................................................... 324 6 18.3.8 Constructors ............................................................................................................................... 325 7 18.3.9 Destructors ................................................................................................................................. 325 8 18.3.10 Static constructors .................................................................................................................... 325 9

18.4 Struct examples ................................................................................................................................. 326 10 18.4.1 Database integer type ................................................................................................................. 326 11 18.4.2 Database boolean type................................................................................................................ 327 12

19. Arrays..................................................................................................................................................... 329 13

19.1 Array types ........................................................................................................................................ 329 14 19.1.1 The System.Array type............................................................................................................... 330 15

19.2 Array creation.................................................................................................................................... 330 16 19.3 Array element access......................................................................................................................... 330 17 19.4 Array members.................................................................................................................................. 330 18 19.5 Array covariance ............................................................................................................................... 330 19 19.6 Arrays and the generic IList interface ............................................................................................... 331 20 19.7 Array initializers................................................................................................................................ 332 21

20. Interfaces................................................................................................................................................ 335 22

20.1 Interface declarations ........................................................................................................................ 335 23 20.1.1 Interface modifiers ..................................................................................................................... 335 24 20.1.2 Base interfaces ........................................................................................................................... 336 25 20.1.3 Interface body ............................................................................................................................ 336 26

20.2 Interface members ............................................................................................................................. 337 27 20.2.1 Interface methods....................................................................................................................... 338 28 20.2.2 Interface properties .................................................................................................................... 338 29 20.2.3 Interface events .......................................................................................................................... 338 30 20.2.4 Interface indexers ....................................................................................................................... 338 31 20.2.5 Interface member access ............................................................................................................ 339 32

20.3 Fully qualified interface member names ........................................................................................... 340 33 20.4 Interface implementations ................................................................................................................. 341 34

20.4.1 Explicit interface member implementations .............................................................................. 341 35 20.4.2 Interface mapping ...................................................................................................................... 343 36 20.4.3 Interface implementation inheritance......................................................................................... 346 37 20.4.4 Interface re-implementation ....................................................................................................... 348 38 20.4.5 Abstract classes and interfaces................................................................................................... 349 39

21. Enums..................................................................................................................................................... 351 40

21.1 Enum declarations ............................................................................................................................. 351 41 21.2 Enum modifiers ................................................................................................................................. 351 42 21.3 Enum members.................................................................................................................................. 352 43 21.4 The System.Enum type ..................................................................................................................... 354 44 21.5 Enum values and operations.............................................................................................................. 354 45

22. Delegates................................................................................................................................................. 355 46

22.1 Delegate declarations ........................................................................................................................ 355 47 22.2 Delegate instantiation........................................................................................................................ 357 48 22.3 Delegate invocation........................................................................................................................... 357 49

23. Exceptions .............................................................................................................................................. 361 50

23.1 Causes of exceptions ......................................................................................................................... 361 51

Page 12: C# Language Specification

C# LANGUAGE SPECIFICATION

xii

23.2 The System.Exception class .............................................................................................................. 361 1 23.3 How exceptions are handled.............................................................................................................. 361 2 23.4 Common Exception Classes.............................................................................................................. 362 3

24. Attributes ............................................................................................................................................... 365 4

24.1 Attribute classes ................................................................................................................................ 365 5 24.1.1 Attribute usage ........................................................................................................................... 365 6 24.1.2 Positional and named parameters............................................................................................... 366 7 24.1.3 Attribute parameter types........................................................................................................... 367 8

24.2 Attribute specification ....................................................................................................................... 367 9 24.3 Attribute instances............................................................................................................................. 372 10

24.3.1 Compilation of an attribute ........................................................................................................ 372 11 24.3.2 Run-time retrieval of an attribute instance................................................................................. 373 12

24.4 Reserved attributes ............................................................................................................................ 373 13 24.4.1 The AttributeUsage attribute...................................................................................................... 373 14 24.4.2 The Conditional attribute ........................................................................................................... 373 15

24.4.2.1 Conditional Methods........................................................................................................... 373 16 24.4.2.2 Conditional Attribute Classes.............................................................................................. 376 17

24.4.3 The Obsolete attribute ................................................................................................................ 376 18

25. Unsafe code ............................................................................................................................................ 379 19

25.1 Unsafe contexts ................................................................................................................................. 379 20 25.2 Pointer types...................................................................................................................................... 381 21 25.3 Fixed and moveable variables ........................................................................................................... 384 22 25.4 Pointer conversions ........................................................................................................................... 384 23 25.5 Pointers in expressions ...................................................................................................................... 385 24

25.5.1 Pointer indirection...................................................................................................................... 386 25 25.5.2 Pointer member access............................................................................................................... 386 26 25.5.3 Pointer element access ............................................................................................................... 387 27 25.5.4 The address-of operator.............................................................................................................. 388 28 25.5.5 Pointer increment and decrement............................................................................................... 389 29 25.5.6 Pointer arithmetic ....................................................................................................................... 389 30 25.5.7 Pointer comparison .................................................................................................................... 390 31 25.5.8 The sizeof operator..................................................................................................................... 390 32

25.6 The fixed statement ........................................................................................................................... 391 33 25.7 Stack allocation ................................................................................................................................. 394 34 25.8 Dynamic memory allocation ............................................................................................................. 395 35

26. Generics.................................................................................................................................................. 397 36

26.1 Generic class declarations ................................................................................................................. 397 37 26.1.1 Type parameters ......................................................................................................................... 397 38 26.1.2 The instance type ....................................................................................................................... 398 39 26.1.3 Members of generic classes ....................................................................................................... 399 40 26.1.4 Static fields in generic classes.................................................................................................... 399 41 26.1.5 Static constructors in generic classes ......................................................................................... 400 42 26.1.6 Accessing protected members.................................................................................................... 400 43 26.1.7 Overloading in generic classes................................................................................................... 401 44 26.1.8 Parameter array methods and type parameters........................................................................... 402 45 26.1.9 Overriding and generic classes................................................................................................... 402 46 26.1.10 Operators in generic classes ..................................................................................................... 402 47 26.1.11 Nested types in generic classes ................................................................................................ 404 48

26.2 Generic struct declarations ................................................................................................................ 404 49 26.3 Generic interface declarations ........................................................................................................... 405 50

26.3.1 Uniqueness of implemented interfaces ...................................................................................... 405 51 26.3.2 Explicit interface member implementations .............................................................................. 406 52

26.4 Generic delegate declarations............................................................................................................ 406 53

Page 13: C# Language Specification

Table of Contents

xiii

26.5 Constructed types .............................................................................................................................. 407 1 26.5.1 Type arguments.......................................................................................................................... 408 2 26.5.2 Open and closed types................................................................................................................ 408 3 26.5.3 Base classes and interfaces of a constructed type ...................................................................... 408 4 26.5.4 Members of a constructed type .................................................................................................. 409 5 26.5.5 Accessibility of a constructed type ............................................................................................ 410 6 26.5.6 Conversions................................................................................................................................ 410 7 26.5.7 Using alias directives ................................................................................................................. 410 8

26.6 Generic methods................................................................................................................................ 411 9 26.6.1 Generic method signatures......................................................................................................... 411 10 26.6.2 Virtual generic methods ............................................................................................................. 412 11 26.6.3 Calling generic methods............................................................................................................. 413 12 26.6.4 Inference of type arguments....................................................................................................... 413 13 26.6.5 Using a generic method with a delegate..................................................................................... 415 14 26.6.6 No generic properties, events, indexers, operators, constructors, or destructors ....................... 415 15

26.7 Constraints......................................................................................................................................... 415 16 26.7.1 Satisfying constraints ................................................................................................................. 419 17 26.7.2 Member lookup on type parameters........................................................................................... 420 18 26.7.3 Type parameters and boxing ...................................................................................................... 421 19 26.7.4 Conversions involving type parameters ..................................................................................... 422 20

27. Iterators.................................................................................................................................................. 423 21

27.1 Iterator blocks.................................................................................................................................... 423 22 27.1.1 Enumerator interfaces ................................................................................................................ 424 23 27.1.2 Enumerable interfaces................................................................................................................ 424 24 27.1.3 Yield type................................................................................................................................... 424 25 27.1.4 This access ................................................................................................................................. 424 26

27.2 Enumerator objects............................................................................................................................ 424 27 27.2.1 The MoveNext method .............................................................................................................. 425 28 27.2.2 The Current property.................................................................................................................. 426 29 27.2.3 The Dispose method................................................................................................................... 426 30

27.3 Enumerable objects ........................................................................................................................... 426 31 27.3.1 The GetEnumerator method....................................................................................................... 427 32

27.4 Implementation example ................................................................................................................... 427 33

Annex A. Grammar .................................................................................................................................... 433 34

A.1 Lexical grammar................................................................................................................................. 433 35 A.1.1 Line terminators .......................................................................................................................... 433 36 A.1.2 White space ................................................................................................................................. 433 37 A.1.3 Comments ................................................................................................................................... 434 38 A.1.4 Tokens ......................................................................................................................................... 434 39 A.1.5 Unicode character escape sequences........................................................................................... 434 40 A.1.6 Identifiers .................................................................................................................................... 435 41 A.1.7 Keywords .................................................................................................................................... 436 42 A.1.8 Literals......................................................................................................................................... 436 43 A.1.9 Operators and punctuators........................................................................................................... 438 44 A.1.10 Pre-processing directives........................................................................................................... 438 45

A.2 Syntactic grammar.............................................................................................................................. 440 46 A.2.1 Basic concepts ............................................................................................................................. 440 47 A.2.2 Types ........................................................................................................................................... 440 48 A.2.3 Variables ..................................................................................................................................... 441 49 A.2.4 Expressions ................................................................................................................................. 442 50 A.2.5 Statements ................................................................................................................................... 445 51 A.2.6 Classes......................................................................................................................................... 449 52 A.2.7 Structs.......................................................................................................................................... 454 53

Page 14: C# Language Specification

C# LANGUAGE SPECIFICATION

xiv

A.2.8 Arrays.......................................................................................................................................... 455 1 A.2.9 Interfaces ..................................................................................................................................... 456 2 A.2.10 Enums........................................................................................................................................ 456 3 A.2.11 Delegates ................................................................................................................................... 457 4 A.2.12 Attributes................................................................................................................................... 457 5 A.2.13 Generics..................................................................................................................................... 458 6

A.3 Grammar extensions for unsafe code ................................................................................................. 459 7

Annex B. Portability issues......................................................................................................................... 461 8

B.1 Undefined behavior ............................................................................................................................ 461 9 B.2 Implementation-defined behavior....................................................................................................... 461 10 B.3 Unspecified behavior.......................................................................................................................... 462 11 B.4 Other Issues ........................................................................................................................................ 462 12

Annex C. Naming guidelines ...................................................................................................................... 463 13

C.1 Capitalization styles............................................................................................................................ 463 14 C.1.1 Pascal casing................................................................................................................................ 463 15 C.1.2 Camel casing ............................................................................................................................... 463 16 C.1.3 All uppercase ............................................................................................................................... 463 17 C.1.4 Capitalization summary............................................................................................................... 463 18

C.2 Word choice........................................................................................................................................ 464 19 C.3 Namespaces ........................................................................................................................................ 464 20 C.4 Classes ................................................................................................................................................ 464 21 C.5 Interfaces ............................................................................................................................................ 465 22 C.6 Enums ................................................................................................................................................. 465 23 C.7 Static fields ......................................................................................................................................... 466 24 C.8 Parameters .......................................................................................................................................... 466 25 C.9 Methods .............................................................................................................................................. 466 26 C.10 Properties .......................................................................................................................................... 466 27 C.11 Events ............................................................................................................................................... 467 28 C.12 Case sensitivity ................................................................................................................................. 467 29 C.13 Avoiding type name confusion......................................................................................................... 468 30

Annex D. Standard Library ....................................................................................................................... 469 31

Annex E. Documentation Comments ........................................................................................................ 511 32

E.1 Introduction......................................................................................................................................... 511 33 E.2 Recommended tags ............................................................................................................................. 512 34

E.2.1 <c>............................................................................................................................................... 513 35 E.2.2 <code> ......................................................................................................................................... 513 36 E.2.3 <example> ................................................................................................................................... 513 37 E.2.4 <exception> ................................................................................................................................. 513 38 E.2.5 <list>............................................................................................................................................ 514 39 E.2.6 <para>.......................................................................................................................................... 515 40 E.2.7 <param>....................................................................................................................................... 515 41 E.2.8 <paramref> .................................................................................................................................. 516 42 E.2.9 <permission> ............................................................................................................................... 516 43 E.2.10 <remarks>.................................................................................................................................. 516 44 E.2.11 <returns>.................................................................................................................................... 517 45 E.2.12 <see>.......................................................................................................................................... 517 46 E.2.13 <seealso> ................................................................................................................................... 518 47 E.2.14 <summary>................................................................................................................................ 518 48 E.2.15 <value> ...................................................................................................................................... 518 49

E.3 Processing the documentation file ...................................................................................................... 519 50 E.3.1 ID string format ........................................................................................................................... 519 51 E.3.2 ID string examples....................................................................................................................... 520 52

Page 15: C# Language Specification

Table of Contents

xv

E.4 An example ......................................................................................................................................... 523 1 E.4.1 C# source code............................................................................................................................. 523 2 E.4.2 Resulting XML ............................................................................................................................ 525 3

Annex F. Bibliography................................................................................................................................ 528 4

Page 16: C# Language Specification
Page 17: C# Language Specification

Foreword

xvii

Foreword 1

This third edition cancels and replaces the second edition. Changes from the previous edition include: 2

1. §1 has been made normative. 3

2. §3 Several entries have been moved to the (new) annex, §Annex F, “Bibliography”. 4

3. §9.2.3, “Grammar ambiguities”, is new. 5

4. §9.4.5 Due to the addition of generics, >> is made up of the two tokens > and >. Similarly, >>= is made 6 up of the two tokens > and >=. 7

5. §9.5.7 corrected the production for file-name-character:: and expanded the definition of #line to allow 8 other implementation-defined syntax. 9

6. §9.5.8, “Pragma directives”, is new. 10

7. §10.7.1 Clarifed an existing restriction on name hiding. (This also resulted in some text being deleted 11 from §10.3.) 12

8. §10.7.1.2 Removed the text “The rules governing operator declarations (§17.9) make it impossible for a 13 derived class to declare an operator with the same signature as an operator in a base class. Thus, 14 operators never hide one another.” 15

9. §11.1.1, “The System.ValueType type”, is new; however, it adds no new normative requirements that 16 didn’t already exist. 17

10. §11.1.6 Added text regarding support for denormalized floating-point numbers. 18

11. §11.1.7 (and other subclauses too) Amended to permit alternate representations for the decimal type. 19

12. §12.1.7 Clarified local variable creation in iterative situations. 20

13. §12.3.3.26 Added text regarding definite assignment. 21

14. §14.5.13, “Default value expression” is new. 22

15. §14.5.14, “Anonymous methods”, is new. (There is also new or changed text in §8.17, §12.3.3.27, 23 §13.1, §13.5, §13.6, §14.5, §14.9.8, §25.5.4, §25.6, and §B.3.) 24

16. §14.7.2 Changed the wording for the scale of the result of decimal division. 25

17. §16.7 and contains text for qualified alias members. (There is also new or changed text in §10.8, 26 §14.5.4, and §14.5.13.) 27

18. §17.1.1.3, “Static classes”, is new. (There is also new or changed text in §8.7.12, §10.8, and §14.5.) 28

19. §17.1.4, “Partial declarations”, is new. (There is also new or changed text in §8.7.13, §16.4.1, §17.1, 29 §17.1.1, §17.1.1.1, §17.1.1.2, §17.1.2.1, §17.1.2.2, §17.2, §17.2.2, §18.1, §20.1, §24.2, §25.1, §26.7, and 30 §B.3.) 31

20. §17.6.2 The set and get accessors for properties and indexers can now have different accessibility. 32

21. §21.4, “The System.Enum type” is new; however, it adds no new normative requirements that didn’t 33 already exist. 34

22. §26, “Generics” is new. (There is also new or changed text in §8.16, §10.1, §10.5.3, §10.8, §11, §14.3, 35 §14.5.4, §14.5.11, §15.8.4, §17.1, §17.1.1, §17.2, §18.1, §18.2, §20.1, §20.2, §20.2.1, and §22.1.) 36

23. §27, “Iterators”, is new. (There is also new or changed text in §8.18, §12.3.3.28, §15.14, and §B.3.) 37

24. Annex F, “Bibliography”, is new. 38

Page 18: C# Language Specification
Page 19: C# Language Specification

Introduction

xix

Introduction 1

This International Standard is based on a submission from Hewlett-Packard, Intel, and Microsoft, that 2 describes a language called C#, which was developed within Microsoft. The principal inventors of this 3 language were Anders Hejlsberg, Scott Wiltamuth, and Peter Golde. The first widely distributed 4 implementation of C# was released by Microsoft in July 2000, as part of its .NET Framework initiative. 5

ECMA Technical Committee 39 (TC39) Task Group 2 (TG2) was formed in September 2000, to produce a 6 standard for C#. Another Task Group, TG3, was also formed at that time to produce a standard for a library 7 and execution environment called Common Language Infrastructure (CLI). (CLI is based on a subset of the 8 .NET Framework.) Although Microsoft’s implementation of C# relies on CLI for library and runtime 9 support, other implementations of C# need not, provided they support an alternate way of getting at the 10 minimum CLI features required by this C# standard. 11

As the definition of C# evolved, the goals used in its design were as follows: 12

• C# is intended to be a simple, modern, general-purpose, object-oriented programming language. 13

• The language, and implementations thereof, should provide support for software engineering principles 14 such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, 15 and automatic garbage collection. Software robustness, durability, and programmer productivity are 16 important. 17

• The language is intended for use in developing software components suitable for deployment in 18 distributed environments. 19

• Source code portability is very important, as is programmer portability, especially for those 20 programmers already familiar with C and C++. 21

• Support for internationalization is very important. 22

• C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging 23 from the very large that use sophisticated operating systems, down to the very small having dedicated 24 functions. 25

• Although C# applications are intended to be economical with regards to memory and processing power 26 requirements, the language was not intended to compete directly on performance and size with C or 27 assembly language. 28

The development of this standard started in November 2000. 29

It is expected there will be future revisions to this standard, primarily to add new functionality. 30

Page 20: C# Language Specification
Page 21: C# Language Specification

Chapter 1 Scope

1

1. Scope 1

This International Standard specifies the form and establishes the interpretation of programs written in the 2 C# programming language. It specifies 3

• The representation of C# programs; 4

• The syntax and constraints of the C# language; 5

• The semantic rules for interpreting C# programs; 6

• The restrictions and limits imposed by a conforming implementation of C#. 7

This International Standard does not specify 8

• The mechanism by which C# programs are transformed for use by a data-processing system; 9

• The mechanism by which C# applications are invoked for use by a data-processing system; 10

• The mechanism by which input data are transformed for use by a C# application; 11

• The mechanism by which output data are transformed after being produced by a C# application; 12

• The size or complexity of a program and its data that will exceed the capacity of any specific data-13 processing system or the capacity of a particular processor; 14

• All minimal requirements of a data-processing system that is capable of supporting a conforming 15 implementation. 16

Page 22: C# Language Specification
Page 23: C# Language Specification

Chapter 2 Conformance

3

2. Conformance 1

Conformance is of interest to the following audiences: 2

• Those designing, implementing, or maintaining C# implementations. 3

• Governmental or commercial entities wishing to procure C# implementations. 4

• Testing organizations wishing to provide a C# conformance test suite. 5

• Programmers wishing to port code from one C# implementation to another. 6

• Educators wishing to teach Standard C#. 7

• Authors wanting to write about Standard C#. 8

As such, conformance is most important, and the bulk of this International Standard is aimed at specifying 9 the characteristics that make C# implementations and C# programs conforming ones. 10

11

The text in this International Standard that specifies requirements is considered normative. All other text in 12 this specification is informative; that is, for information purposes only. Unless stated otherwise, all text is 13 normative. Normative text is further broken into required and conditional categories. Conditionally 14 normative text specifies requirements for a feature such that if that feature is provided, its syntax and 15 semantics must be exactly as specified. 16

Undefined behavior is indicated in this International Standard only by the words ‘‘undefined behavior.’’ 17

A strictly conforming program shall use only those features of the language specified in this International 18 Standard as being required. (This means that a strictly conforming program cannot use any conditionally 19 normative feature.) It shall not produce output dependent on any unspecified, undefined, or implementation-20 defined behavior. 21

A conforming implementation of C# must accept any strictly conforming program. 22

A conforming implementation of C# must provide and support all the types, values, objects, properties, 23 methods, and program syntax and semantics described in the normative (but not the conditionally normative) 24 parts in this International Standard. 25

A conforming implementation of C# shall interpret characters in conformance with the Unicode Standard, 26 Version 3.0, and ISO/IEC 10646-1. Conforming implementations must accept Unicode source files encoded 27 with the UTF-8 encoding form. 28

A conforming implementation of C# shall not successfully translate source containing a #error 29 preprocessing directive unless it is part of a group skipped by conditional compilation. 30

A conforming implementation of C# shall produce at least one diagnostic message if the source program 31 violates any rule of syntax, or any negative requirement (defined as a “shall” or “shall not” or “error” or 32 “warning” requirement), unless that requirement is marked with the words “no diagnostic is required”. 33

A conforming implementation of C# is permitted to provide additional types, values, objects, properties, and 34 methods beyond those described in this International Standard, provided they do not alter the behavior of 35 any strictly conforming program. Conforming implementations are required to diagnose programs that use 36 extensions that are ill formed according to this International Standard. Having done so, however; they can 37 compile and execute such programs. (The ability to have extensions implies that a conforming 38 implementation reserves no identifiers other than those explicitly reserved in this International Standard.) 39

Page 24: C# Language Specification

C# LANGUAGE SPECIFICATION

4

A conforming implementation of C# shall be accompanied by a document that defines all implementation-1 defined characteristics, and all extensions. 2

A conforming implementation of C# shall support the class library documented in §Annex D. This library is 3 included by reference in this International Standard. 4

A conforming program is one that is acceptable to a conforming implementation. (Such a program is 5 permitted to contain extensions or conditionally normative features.) 6

Page 25: C# Language Specification

Chapter 3 Normative references

5

3. Normative references 1

The following normative documents contain provisions, which, through reference in this text, constitute 2 provisions of this International Standard. For dated references, subsequent amendments to, or revisions of, 3 any of these publications do not apply. However, parties to agreements based on this International Standard 4 are encouraged to investigate the possibility of applying the most recent editions of the normative documents 5 indicated below. For undated references, the latest edition of the normative document referred to applies. 6 Members of ISO and IEC maintain registers of currently valid International Standards. 7

8

ISO/IEC 23271:2002, Common Language Infrastructure (CLI), Partition IV: Base Class Library (BCL), 9 Extended Numerics Library, and Extended Array Library. 10

ISO 31.11:1992, Quantities and units — Part 11: Mathematical signs and symbols for use in the physical 11 sciences and technology. 12

ISO/IEC 2382.1:1993, Information technology — Vocabulary — Part 1: Fundamental terms. 13

ISO/IEC 10646 (all parts), Information technology — Universal Multiple-Octet Coded Character Set (UCS). 14

IEC 60559:1989, Binary floating-point arithmetic for microprocessor systems (previously designated IEC 15 559:1989). (This standard is widely known by its U.S. national designation, ANSI/IEEE Standard 754-1985, 16 IEEE Standard for Binary Floating-Point Arithmetic.) 17

The Unicode Consortium. The Unicode Standard, Version 3.0, defined by: The Unicode Standard, Version 18 3.0 (Reading, MA, Addison-Wesley, 2000. ISBN 0-201-61633-5), Unicode Annex UAX #15: Unicode 19 Normalization Forms, and Unicode Annex UAX #19: UTF-32. 20

Page 26: C# Language Specification
Page 27: C# Language Specification

Chapter 4 Definitions

7

4. Definitions 1

For the purposes of this International Standard, the following definitions apply. Other terms are defined 2 where they appear in italic type or on the left side of a syntax rule. Terms explicitly defined in this 3 International Standard are not to be presumed to refer implicitly to similar terms defined elsewhere. Terms 4 not defined in this International Standard are to be interpreted according to ISO/IEC 2382.1. Mathematical 5 symbols not defined in this International Standard are to be interpreted according to ISO 31.11. 6

7

Application — refers to an assembly that has an entry point (§10.1). When an application is run, a new 8 application domain is created. Several different instantiations of an application can exist on the same 9 machine at the same time, and each has its own application domain. 10

Application domain — an entity that enables application isolation by acting as a container for application 11 state. An application domain acts as a container and boundary for the types defined in the application and the 12 class libraries it uses. Types loaded into one application domain are distinct from the same type loaded into 13 another application domain, and instances of objects are not directly shared between application domains. 14 For instance, each application domain has its own copy of static variables for these types, and a static 15 constructor for a type is run at most once per application domain. Implementations are free to provide 16 implementation-specific policy or mechanisms for the creation and destruction of application domains. 17

Argument — an expression in the comma-separated list bounded by the parentheses in a method or instance 18 constructor call expression or bounded by the square brackets in an element access expression. It is also 19 known as an actual argument. 20

Assembly — refers to one or more files that are output by the compiler as a result of program compilation. 21 An assembly is a configured set of loadable code modules and other resources that together implement a unit 22 of functionality. An assembly can contain types, the executable code used to implement these types, and 23 references to other assemblies. The physical representation of an assembly is not defined by this 24 specification. Essentially, an assembly is the output of the compiler. 25

Behavior — external appearance or action. 26

Behavior, implementation-defined — unspecified behavior where each implementation documents how 27 the choice is made. 28

Behavior, undefined — behavior, upon use of a nonportable or erroneous construct or of erroneous data, 29 for which this International Standard imposes no requirements. [Possible handling of undefined behavior 30 ranges from ignoring the situation completely with unpredictable results, to behaving during translation or 31 execution in a documented manner characteristic of the environment (with or without the issuance of a 32 diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message)]. 33

Behavior, unspecified — behavior where this International Standard provides two or more possibilities and 34 imposes no further requirements on which is chosen in any instance. 35

Class library — refers to an assembly that can be used by other assemblies. Use of a class library does not 36 cause the creation of a new application domain. Instead, a class library is loaded into the application domain 37 that uses it. For instance, when an application uses a class library, that class library is loaded into the 38 application domain for that application. If an application uses a class library A that itself uses a class 39 library B, then both A and B are loaded into the application domain for the application. 40

Diagnostic message — a message belonging to an implementation-defined subset of the implementation’s 41 output messages. 42

Error, compile-time — an error reported during program translation. 43

Page 28: C# Language Specification

C# LANGUAGE SPECIFICATION

8

Exception — an error condition that is outside the ordinary expected behavior. 1

Implementation — particular set of software (running in a particular translation environment under 2 particular control options) that performs translation of programs for, and supports execution of methods in, a 3 particular execution environment. 4

Namespace — a logical organizational system that provides a way of presenting program elements that are 5 exposed to other programs. 6

Parameter — a variable declared as part of a method, instance constructor, operator, or indexer definition, 7 which acquires a value on entry to that function member. It is also known as a formal parameter. 8

Program — refers to one or more source files that are presented to the compiler. Essentially, a program is 9 the input to the compiler. 10

Program, valid — a C# program constructed according to the syntax rules and diagnosable semantic rules. 11

Program instantiation — the execution of an application. 12

Recommended practice — specification that is strongly recommended as being aligned with the intent of 13 the standard, but that might be impractical for some implementations 14

Source file — an ordered sequence of Unicode characters. Source files typically have a one-to-one 15 correspondence with files in a file system, but this correspondence is not required. 16

Unsafe code — code that is permitted to perform such lower-level operations as declaring and operating on 17 pointers, performing conversions between pointers and integral types, and taking the address of variables. 18 Such operations provide functionality such as permitting interfacing with the underlying operating system, 19 accessing a memory-mapped device, or implementing a time-critical algorithm. 20

Warning, compile-time — an informational message reported during program translation, that is intended 21 to identify a potentially questionable usage of a program element. 22

Page 29: C# Language Specification

Chapter 5 Notational conventions

9

5. Notational conventions 1

Lexical and syntactic grammars for C# are interspersed throughout this specification. The lexical grammar 2 defines how characters can be combined to form tokens (§9.4), the minimal lexical elements of the language. 3 The syntactic grammar defines how tokens can be combined to make valid C# programs. 4

Grammar productions include both non-terminal and terminal symbols. In grammar productions, non-5 terminal symbols are shown in italic type, and terminal symbols are shown in a fixed-width font. Each 6 non-terminal is defined by a set of productions. The first line of a set of productions is the name of the non-7 terminal, followed by one or two colons. One colon is used for a production in the syntactic grammar, two 8 colons for a production in the lexical grammar. Each successive indented line contains the right-hand side 9 for a production that has the non-terminal symbol as the left-hand side. For example: 10

class-modifier: 11 new 12 public 13 protected 14 internal 15 private 16 abstract 17 sealed 18 static 19

defines the class-modifier non-terminal as having seven productions. 20

Alternatives are normally listed on separate lines, as shown above, though in cases where there are many 21 alternatives, the phrase “one of” precedes a list of the options. This is simply shorthand for listing each of 22 the alternatives on a separate line. For example: 23

decimal-digit: one of 24 0 1 2 3 4 5 6 7 8 9 25

is equivalent to: 26

decimal-digit: 27 0 28 1 29 2 30 3 31 4 32 5 33 6 34 7 35 8 36 9 37

A subscripted suffix “opt”, as in identifieropt, is used as shorthand to indicate an optional symbol. The 38 example: 39

for-statement: 40 for ( for-initializeropt ; for-conditionopt ; for-iteratoropt ) embedded-statement 41

is equivalent to: 42

Page 30: C# Language Specification

C# LANGUAGE SPECIFICATION

10

for-statement: 1 for ( ; ; ) embedded-statement 2 for ( for-initializer ; ; ) embedded-statement 3 for ( ; for-condition ; ) embedded-statement 4 for ( ; ; for-iterator ) embedded-statement 5 for ( for-initializer ; for-condition ; ) embedded-statement 6 for ( ; for-condition ; for-iterator ) embedded-statement 7 for ( for-initializer ; ; for-iterator ) embedded-statement 8 for ( for-initializer ; for-condition ; for-iterator ) embedded-statement 9

10

All terminal characters are to be understood as the appropriate Unicode character from the range U+0020 to 11 U+007F, as opposed to any similar-looking characters from other Unicode character ranges. 12

Page 31: C# Language Specification

Chapter 6 Acronyms and abbreviations

11

6. Acronyms and abbreviations 1

This clause is informative. 2

The following acronyms and abbreviations are used throughout this International Standard: 3

4

BCL — Base Class Library, which provides types to represent the built-in data types of the CLI, simple file 5 access, custom attributes, security attributes, string manipulation, formatting, streams, and collections. 6

CLI — Common Language Infrastructure 7

CLS — Common Language Specification 8

IEC — the International Electrotechnical Commission 9

IEEE — the Institute of Electrical and Electronics Engineers 10

ISO — the International Organization for Standardization 11

12

The name C# is pronounced “C Sharp”. 13

The name C# is written as the LATIN CAPITAL LETTER C (U+0043) followed by the NUMBER SIGN # 14 (U+0023). 15

End of informative text. 16

Page 32: C# Language Specification
Page 33: C# Language Specification

Chapter 7 General description

13

7. General description 1

This text is informative. 2

This International Standard is intended to be used by implementers, academics, and application 3 programmers. As such, it contains a considerable amount of explanatory material that, strictly speaking, is 4 not necessary in a formal language specification. 5

This standard is divided into the following subdivisions: 6

1. Front matter (clauses 1–7); 7

2. Language overview (clause 8); 8

3. The language syntax, constraints, and semantics (clauses 9–27); 9

4. Annexes 10

Examples are provided to illustrate possible forms of the constructions described. References are used to 11 refer to related clauses. Notes are provided to give advice or guidance to implementers or programmers. 12 Annexes provide additional information and summarize the information contained in this International 13 Standard. 14

Clauses 1–5, 9–24, the beginning of 25, clauses 26 and 27, and the beginning of annex D form a normative 15 part of this standard; all of clause 25 with the exception of the beginning is conditionally normative; and 16 Foreword, Introduction, clauses 6–8, annexes A, B, C, and most of D, notes, examples, and the index are 17 informative. 18

End of informative text. 19

Informative text is indicated in the following ways: 20

1. Whole or partial clauses or annexes delimited by “This clause/text is informative”and “End of 21 informative text”. 22

2. [Example: The following example … code fragment, possibly with some narrative … end example] 23

3. [Note: narrative … end note] 24

All text not marked as being informative is normative.25

Page 34: C# Language Specification
Page 35: C# Language Specification

Chapter 8 Language overview

15

8. Language overview 1

This clause is informative. 2

C# (pronounced “C Sharp”) is a simple, modern, object oriented, and type-safe programming language. It 3 will immediately be familiar to C and C++ programmers. C# combines the high productivity of Rapid 4 Application Development (RAD) languages and the raw power of C++. 5

The rest of this clause describes the essential features of the language. While later clauses describe rules and 6 exceptions in a detail-oriented and sometimes mathematical manner, this clause strives for clarity and 7 brevity at the expense of completeness. The intent is to provide the reader with an introduction to the 8 language that will facilitate the writing of early programs and the reading of later clauses. 9

8.1 Getting started 10

The canonical “hello, world” program can be written as follows: 11

using System; 12

class Hello 13 { 14 static void Main() { 15 Console.WriteLine("hello, world"); 16 } 17 } 18

The source code for a C# program is typically stored in one or more text files with a file extension of .cs, as 19 in hello.cs. Using a command-line compiler, such a program can be compiled with a command line like 20

csc hello.cs 21

which produces an application named hello.exe. The output produced by this application when it is run 22 is: 23

hello, world 24

Close examination of this program is illuminating: 25

• The using System; directive references a namespace called System that is provided by the Common 26 Language Infrastructure (CLI) class library. This namespace contains the Console class referred to in 27 the Main method. Namespaces provide a hierarchical means of organizing the elements of one or more 28 programs. A using-directive enables unqualified use of the types that are members of the namespace. 29 The “hello, world” program uses Console.WriteLine as shorthand for 30 System.Console.WriteLine. 31

• The Main method is a member of the class Hello. It has the static modifier, and so it is a method on 32 the class Hello rather than on instances of this class. 33

• The entry point for an application—the method that is called to begin execution—is always a static 34 method named Main. 35

• The “hello, world” output is produced using a class library. This standard does not include a class 36 library. Instead, it references the class library provided by CLI. 37

For C and C++ developers, it is interesting to note a few things that do not appear in the “hello, world” 38 program. 39

• The program does not use a global method for Main. Methods and variables are not supported at the 40 global level; such elements are always contained within type declarations (e.g., class and struct 41 declarations). 42

Page 36: C# Language Specification

C# LANGUAGE SPECIFICATION

16

• The program does not use either “::” or “->” operators. The “::” token is used only to separate a 1 namespace alias from a member of the namespace, and the “->” operator is used in only a small fraction 2 of programs (which involve unsafe code). The separator “.” is used in compound names such as 3 Console.WriteLine. 4

• The program does not contain forward declarations. Forward declarations are never needed, as 5 declaration order is not significant. 6

• The program does not use #include to import program text. Dependencies among programs are 7 handled symbolically rather than textually. This approach eliminates barriers between applications 8 written using multiple languages. For example, the Console class need not be written in C#. 9

8.2 Types 10

C# supports two kinds of types: value types and reference types. Value types include simple types (e.g., 11 char, int, and float), enum types, and struct types. Reference types include class types, interface types, 12 delegate types, and array types. 13

Value types differ from reference types in that variables of the value types directly contain their data, 14 whereas variables of the reference types store references to objects. With reference types, it is possible for 15 two variables to reference the same object, and thus possible for operations on one variable to affect the 16 object referenced by the other variable. With value types, the variables each have their own copy of the data, 17 and it is not possible for operations on one to affect the other. 18

The example 19

using System; 20

struct Struct1 21 { 22 public int Value; 23 } 24

class Class1 25 { 26 public int Value = 0; 27 } 28

class Test 29 { 30 static void Main() { 31 Struct1 val1 = new Struct1(); 32 Struct1 val2 = val1; 33 val2.Value = 123; 34

Class1 ref1 = new Class1(); 35 Class1 ref2 = ref1; 36 ref2.Value = 123; 37

Console.WriteLine("Values: {0}, {1}", val1.Value, val2.Value); 38 Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value); 39 } 40 } 41

shows this difference. The output produced is 42

Values: 0, 123 43 Refs: 123, 123 44

The assignment to a field of the local variable val2 does not impact the local variable val1 because both 45 local variables are of a value type (the type Struct1) and each local variable of a value type has its own 46 storage. In contrast, the assignment ref2.Value = 123; affects the object that both ref1 and ref2 47 reference. 48

The lines 49

Console.WriteLine("Values: {0}, {1}", val1.Value, val2.Value); 50 Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value); 51

Page 37: C# Language Specification

Chapter 8 Language overview

17

deserve further comment, as they demonstrate some of the string formatting behavior of 1 Console.WriteLine, which, in fact, takes a variable number of arguments. The first argument is a string, 2 which can contain numbered placeholders like {0} and {1}. Each placeholder refers to a trailing argument 3 with {0} referring to the second argument, {1} referring to the third argument, and so on. Before the output 4 is sent to the console, each placeholder is replaced with the formatted value of its corresponding argument. 5

Developers can define new value types through enum and struct declarations, and can define new reference 6 types via class, interface, and delegate declarations. The example 7

using System; 8

public enum Color 9 { 10 Red, Blue, Green 11 } 12

public struct Point 13 { 14 public int x, y; 15 } 16

public interface IBase 17 { 18 void F(); 19 } 20

public interface IDerived: IBase 21 { 22 void G(); 23 } 24

public class A 25 { 26 protected virtual void H() { 27 Console.WriteLine("A.H"); 28 } 29 } 30

public class B: A, IDerived 31 { 32 public void F() { 33 Console.WriteLine("B.F, implementation of IDerived.F"); 34 } 35

public void G() { 36 Console.WriteLine("B.G, implementation of IDerived.G"); 37 } 38

override protected void H() { 39 Console.WriteLine("B.H, override of A.H"); 40 } 41 } 42

public delegate void EmptyDelegate(); 43

shows an example of each kind of type declaration. Later clauses describe type declarations in detail. 44

8.2.1 Predefined types 45

C# provides a set of predefined types, most of which will be familiar to C and C++ developers. 46

The predefined reference types are object and string. The type object is the ultimate base type of all 47 other types. The type string is used to represent Unicode string values. Values of type string are 48 immutable. 49

The predefined value types include signed and unsigned integral types, floating-point types, and the types 50 bool, char, and decimal. The signed integral types are sbyte, short, int, and long; the unsigned 51 integral types are byte, ushort, uint, and ulong; and the floating-point types are float and double. 52

Page 38: C# Language Specification

C# LANGUAGE SPECIFICATION

18

The bool type is used to represent boolean values: values that are either true or false. The inclusion of bool 1 makes it easier to write self-documenting code, and also helps eliminate the all-too-common C++ coding 2 error in which a developer mistakenly uses “=” when “==” should have been used. In C#, the example 3

int i = …; 4 F(i); 5 if (i = 0) // Bug: the test should be (i == 0) 6 G(); 7

results in a compile-time error because the expression i = 0 is of type int, and if statements require an 8 expression of type bool. 9

The char type is used to represent Unicode code units. A variable of type char represents a single 16-bit 10 Unicode code unit. 11

The decimal type is appropriate for calculations in which rounding errors caused by floating point 12 representations are unacceptable. Common examples include financial calculations such as tax computations 13 and currency conversions. The decimal type provides for at least 28 significant digits. 14

The table below lists the predefined types, and shows how to write literal values for each of them. 15

16

Type Description Example object The ultimate base type of all other types object o = null;

string String type; a string is a sequence of Unicode code units

string s = "hello";

sbyte 8-bit signed integral type sbyte val = 12;

short 16-bit signed integral type short val = 12;

int 32-bit signed integral type int val = 12;

long 64-bit signed integral type long val1 = 12; long val2 = 34L;

byte 8-bit unsigned integral type byte val1 = 12;

ushort 16-bit unsigned integral type ushort val1 = 12;

uint 32-bit unsigned integral type uint val1 = 12; uint val2 = 34U;

ulong 64-bit unsigned integral type ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; ulong val4 = 78UL;

float Single-precision floating point type float val = 1.23F;

double Double-precision floating point type double val1 = 1.23; double val2 = 4.56D;

bool Boolean type; a bool value is either true or false bool val1 = true; bool val2 = false;

char Character type; a char value is a Unicode code unit char val = 'h';

decimal Precise decimal type with at least 28 significant digits decimal val = 1.23M;

17 Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers 18 to the struct System.Int32. As a matter of style, use of the keyword is favored over use of the complete 19 system type name. 20

Predefined value types such as int are treated specially in a few ways but are for the most part treated 21 exactly like other structs. Operator overloading enables developers to define new struct types that behave 22 much like the predefined value types. For instance, a Digit struct can support the same mathematical 23

Page 39: C# Language Specification

Chapter 8 Language overview

19

operations as the predefined integral types, and can define conversions between Digit and predefined 1 types. 2

The predefined types employ operator overloading themselves. For example, the comparison operators == 3 and != have different semantics for different predefined types: 4

• Two expressions of type int are considered equal if they represent the same integer value. 5

• Two expressions of type object are considered equal if both refer to the same object, or if both are 6 null. 7

• Two expressions of type string are considered equal if the string instances have identical lengths and 8 identical characters in each character position, or if both are null. 9

The example 10

using System; 11

class Test 12 { 13 static void Main() { 14 string s = "Test"; 15 string t = string.Copy(s); 16 Console.WriteLine(s == t); 17 Console.WriteLine((object)s == (object)t); 18 } 19 } 20

produces the output 21

True 22 False 23

because the first comparison compares two expressions of type string, and the second comparison 24 compares two expressions of type object. (Note that when the Standard Library produces a string 25 representation of a Boolean value, as is the case with System.WriteLine above, it uses “True” and 26 “False”, while the corresponding C# language Boolean literals are spelled true and false.) 27

8.2.2 Conversions 28

The predefined types also have predefined conversions. For instance, conversions exist between the 29 predefined types int and long. C# differentiates between two kinds of conversions: implicit conversions 30 and explicit conversions. Implicit conversions are supplied for conversions that can safely be performed 31 without careful scrutiny. For instance, the conversion from int to long is an implicit conversion. This 32 conversion always succeeds, and never results in a loss of information. The following example 33

using System; 34

class Test 35 { 36 static void Main() { 37 int intValue = 123; 38 long longValue = intValue; 39 Console.WriteLine("{0}, {1}", intValue, longValue); 40 } 41 } 42

implicitly converts an int to a long. 43

In contrast, explicit conversions are performed with a cast expression. The example 44

using System; 45

Page 40: C# Language Specification

C# LANGUAGE SPECIFICATION

20

class Test 1 { 2 static void Main() { 3 long longValue = Int64.MaxValue; 4 int intValue = (int) longValue; 5 Console.WriteLine("(int) {0} = {1}", longValue, intValue); 6 } 7 } 8

uses an explicit conversion to convert a long to an int. The output is: 9

(int) 9223372036854775807 = -1 10

because an overflow occurs. Cast expressions permit the use of both implicit and explicit conversions. 11

8.2.3 Array types 12

Arrays can be single-dimensional or multi-dimensional. Both “rectangular” and “jagged” arrays are 13 supported. 14

Single-dimensional arrays are the most common type. The example 15

using System; 16

class Test 17 { 18 static void Main() { 19 int[] arr = new int[5]; 20

for (int i = 0; i < arr.Length; i++) 21 arr[i] = i * i; 22

for (int i = 0; i < arr.Length; i++) 23 Console.WriteLine("arr[{0}] = {1}", i, arr[i]); 24 } 25 } 26

creates a single-dimensional array of int values, initializes the array elements, and then prints each of them 27 out. The output produced is: 28

arr[0] = 0 29 arr[1] = 1 30 arr[2] = 4 31 arr[3] = 9 32 arr[4] = 16 33

The type int[] used in the previous example is an array type. Array types are written using a non-array-34 type followed by one or more rank specifiers. The example 35

class Test 36 { 37 static void Main() { 38 int[] a1; // single-dimensional array of int 39 int[,] a2; // 2-dimensional array of int 40 int[,,] a3; // 3-dimensional array of int 41

int[][] j2; // "jagged" array: array of (array of int) 42 int[][][] j3; // array of (array of (array of int)) 43 } 44 } 45

shows a variety of local variable declarations that use array types with int as the element type. 46

Array types are reference types, and so the declaration of an array variable merely sets aside space for the 47 reference to the array. Array instances are actually created via array initializers and array creation 48 expressions. The example 49

Page 41: C# Language Specification

Chapter 8 Language overview

21

class Test 1 { 2 static void Main() { 3 int[] a1 = new int[] {1, 2, 3}; 4 int[,] a2 = new int[,] {{1, 2, 3}, {4, 5, 6}}; 5 int[,,] a3 = new int[10, 20, 30]; 6

int[][] j2 = new int[3][]; 7 j2[0] = new int[] {1, 2, 3}; 8 j2[1] = new int[] {1, 2, 3, 4, 5, 6}; 9 j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; 10 } 11 } 12

shows a variety of array creation expressions. The variables a1, a2 and a3 denote rectangular arrays, and 13 the variable j2 denotes a jagged array. It should be no surprise that these terms are based on the shapes of 14 the arrays. Rectangular arrays always have a rectangular shape. Given the length of each dimension of the 15 array, its rectangular shape is clear. For example, the lengths of a3’s three dimensions are 10, 20, and 30, 16 respectively, and it is easy to see that this array contains 10*20*30 elements. 17

In contrast, the variable j2 denotes a “jagged” array, or an “array of arrays”. Specifically, j2 denotes an 18 array of an array of int, or a single-dimensional array of type int[]. Each of these int[] variables can be 19 initialized individually, and this allows the array to take on a jagged shape. The example gives each of the 20 int[] arrays a different length. Specifically, the length of j2[0] is 3, the length of j2[1] is 6, and the 21 length of j2[2] is 9. 22

[Note: In C++, an array declared as int x[3][5][7] would be considered a three dimensional rectangular 23 array, while in C#, the declaration int[][][] declares a jagged array type. end note] 24

The element type and shape of an array—including whether it is jagged or rectangular, and the number of 25 dimensions it has—are part of its type. On the other hand, the size of the array—as represented by the length 26 of each of its dimensions—is not part of an array’s type. This split is made clear in the language syntax, as 27 the length of each dimension is specified in the array creation expression rather than in the array type. For 28 instance the declaration 29

int[,,] a3 = new int[10, 20, 30]; 30

has an array type of int[,,] and an array creation expression of new int[10, 20, 30]. 31

For local variable and field declarations, a shorthand form is permitted so that it is not necessary to re-state 32 the array type. For instance, the example 33

int[] a1 = new int[] {1, 2, 3}; 34

can be shortened to 35

int[] a1 = {1, 2, 3}; 36

without any change in program semantics. 37

The context in which an array initializer such as {1, 2, 3} is used determines the type of the array being 38 initialized. The example 39

class Test 40 { 41 static void Main() { 42 short[] a = {1, 2, 3}; 43 int[] b = {1, 2, 3}; 44 long[] c = {1, 2, 3}; 45

} 46 } 47

shows that the same array initializer syntax can be used for several different array types. Because context is 48 required to determine the type of an array initializer, it is not possible to use an array initializer in an 49 expression context without explicitly stating the type of the array. 50

Page 42: C# Language Specification

C# LANGUAGE SPECIFICATION

22

8.2.4 Type system unification 1

C# provides a “unified type system”. All types—including value types—derive from the type object. It is 2 possible to call object methods on any value, even values of “primitive” types such as int. The example 3

using System; 4

class Test 5 { 6 static void Main() { 7 Console.WriteLine(3.ToString()); 8 } 9 } 10

calls the object-defined ToString method on an integer literal, resulting in the output “3”. 11

The example 12

class Test 13 { 14 static void Main() { 15 int i = 123; 16 object o = i; // boxing 17 int j = (int) o; // unboxing 18 } 19 } 20

is more interesting. An int value can be converted to object and back again to int. This example shows 21 both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an 22 object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite. 23 When an object box is cast back to its original value type, the value is copied out of the box and into the 24 appropriate storage location. 25

This type system unification provides value types with the benefits of object-ness without introducing 26 unnecessary overhead. For programs that don’t need int values to act like objects, int values are simply 27 32-bit values. For programs that need int values to behave like objects, this capability is available on 28 demand. This ability to treat value types as objects bridges the gap between value types and reference types 29 that exists in most languages. For example, a Stack class can provide Push and Pop methods that take and 30 return object values. 31

public class Stack 32 { 33 public object Pop() {…} 34

public void Push(object o) {…} 35 } 36

Because C# has a unified type system, the Stack class can be used with elements of any type, including 37 value types like int. 38

8.3 Variables and parameters 39

Variables represent storage locations. Every variable has a type that determines what values can be stored in 40 the variable. Local variables are variables that are declared in function members such as methods, 41 properties, and indexers. A local variable is defined by specifying a type name and a declarator that specifies 42 the variable name and an optional initial value, as in: 43

int a; 44 int b = 1; 45

but it is also possible for a local variable declaration to include multiple declarators. The declarations of a 46 and b can be rewritten as: 47

int a, b = 1; 48

A variable shall be assigned before its value can be obtained. The example 49

Page 43: C# Language Specification

Chapter 8 Language overview

23

class Test 1 { 2 static void Main() { 3 int a; 4 int b = 1; 5 int c = a + b; // error, a not yet assigned 6 … 7 } 8 } 9

results in a compile-time error because it attempts to use the variable a before it is assigned a value. The 10 rules governing definite assignment are defined in §12.3. 11

A field (§17.4) is a variable that is associated with a class or struct, or an instance of a class or struct. A field 12 declared with the static modifier defines a static variable, and a field declared without this modifier 13 defines an instance variable. A static field is associated with a type, whereas an instance variable is 14 associated with an instance. The example 15

using Personnel.Data; 16 class Employee 17 { 18 private static DataSet ds; 19

public string Name; 20 public decimal Salary; 21

… 22 } 23

shows an Employee class that has a private static variable and two public instance variables. 24

Formal parameter declarations also define variables. There are four kinds of parameters: value parameters, 25 reference parameters, output parameters, and parameter arrays. 26

A value parameter is used for “in” parameter passing, in which the value of an argument is passed into a 27 method, and modifications of the parameter do not impact the original argument. A value parameter refers to 28 its own variable, one that is distinct from the corresponding argument. This variable is initialized by copying 29 the value of the corresponding argument. The example 30

using System; 31

class Test 32 { 33 static void F(int p) { 34 Console.WriteLine("p = {0}", p); 35 p++; 36 } 37

static void Main() { 38 int a = 1; 39 Console.WriteLine("pre: a = {0}", a); 40 F(a); 41 Console.WriteLine("post: a = {0}", a); 42 } 43 } 44

shows a method F that has a value parameter named p. The example produces the output: 45

pre: a = 1 46 p = 1 47 post: a = 1 48

even though the value parameter p is modified. 49

A reference parameter is used for “by reference” parameter passing, in which the parameter acts as an alias 50 for a caller-provided argument. A reference parameter does not itself define a variable, but rather refers to 51 the variable of the corresponding argument. Modifications of a reference parameter impact the 52 corresponding argument. A reference parameter is declared with a ref modifier. The example 53

using System; 54

Page 44: C# Language Specification

C# LANGUAGE SPECIFICATION

24

class Test 1 { 2 static void Swap(ref int a, ref int b) { 3 int t = a; 4 a = b; 5 b = t; 6 } 7

static void Main() { 8 int x = 1; 9 int y = 2; 10 11 Console.WriteLine("pre: x = {0}, y = {1}", x, y); 12 Swap(ref x, ref y); 13 Console.WriteLine("post: x = {0}, y = {1}", x, y); 14 } 15 } 16

shows a Swap method that has two reference parameters. The output produced is: 17

pre: x = 1, y = 2 18 post: x = 2, y = 1 19

The ref keyword shall be used in both the declaration of the formal parameter and in uses of it. The use of 20 ref at the call site calls special attention to the parameter, so that a developer reading the code will 21 understand that the value of the argument could change as a result of the call. 22

An output parameter is similar to a reference parameter, except that the initial value of the caller-provided 23 argument is unimportant. An output parameter is declared with an out modifier. The example 24

using System; 25

class Test 26 { 27 static void Divide(int a, int b, out int result, out int remainder) { 28 result = a / b; 29 remainder = a % b; 30 } 31

static void Main() { 32 for (int i = 1; i < 10; i++) 33 for (int j = 1; j < 10; j++) { 34 int ans, r; 35 Divide(i, j, out ans, out r); 36 Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r); 37 } 38 } 39 } 40

shows a Divide method that includes two output parameters—one for the result of the division and another 41 for the remainder. 42

For value, reference, and output parameters, there is a one-to-one correspondence between caller-provided 43 arguments and the parameters used to represent them. A parameter array enables a many-to-one 44 relationship: many arguments can be represented by a single parameter array. In other words, parameter 45 arrays enable variable length argument lists. 46

A parameter array is declared with a params modifier. There can be only one parameter array for a given 47 method, and it shall always be the last parameter specified. The type of a parameter array is always a single 48 dimensional array type. A caller can either pass a single argument of this array type, or any number of 49 arguments of the element type of this array type. For instance, the example 50

using System; 51

Page 45: C# Language Specification

Chapter 8 Language overview

25

class Test 1 { 2 static void F(params int[] args) { 3 Console.WriteLine("# of arguments: {0}", args.Length); 4 for (int i = 0; i < args.Length; i++) 5 Console.WriteLine("\targs[{0}] = {1}", i, args[i]); 6 } 7

static void Main() { 8 F(); 9 F(1); 10 F(1, 2); 11 F(1, 2, 3); 12 F(new int[] {1, 2, 3, 4}); 13 } 14 } 15

shows a method F that takes a variable number of int arguments, and several invocations of this method. 16 The output is: 17

# of arguments: 0 18 # of arguments: 1 19 args[0] = 1 20 # of arguments: 2 21 args[0] = 1 22 args[1] = 2 23 # of arguments: 3 24 args[0] = 1 25 args[1] = 2 26 args[2] = 3 27 # of arguments: 4 28 args[0] = 1 29 args[1] = 2 30 args[2] = 3 31 args[3] = 4 32

Most of the examples presented in this introduction use the WriteLine method of the Console class. The 33 argument substitution behavior of this method, as exhibited in the example 34

int a = 1, b = 2; 35 Console.WriteLine("a = {0}, b = {1}", a, b); 36

is accomplished using a parameter array. The WriteLine method provides several overloaded methods for 37 the common cases in which a small number of arguments are passed, and one method that uses a parameter 38 array. 39

namespace System 40 { 41 public class Console 42 { 43 public static void WriteLine(string s) {…} 44 public static void WriteLine(string s, object a) {…} 45 public static void WriteLine(string s, object a, object b) {…} 46 … 47 public static void WriteLine(string s, params object[] args) {…} 48 } 49 } 50

8.4 Automatic memory management 51

Manual memory management requires developers to manage the allocation and de-allocation of blocks of 52 memory. Manual memory management can be both time-consuming and difficult. In C#, automatic memory 53 management is provided so that developers are freed from this burdensome task. In the vast majority of 54 cases, automatic memory management increases code quality and enhances developer productivity without 55 negatively impacting either expressiveness or performance. 56

The example 57

using System; 58

Page 46: C# Language Specification

C# LANGUAGE SPECIFICATION

26

public class Stack 1 { 2 private Node first = null; 3

public bool Empty { 4 get { 5 return (first == null); 6 } 7 } 8

public object Pop() { 9 if (first == null) 10 throw new Exception("Can't Pop from an empty Stack."); 11 else { 12 object temp = first.Value; 13 first = first.Next; 14 return temp; 15 } 16 } 17

public void Push(object o) { 18 first = new Node(o, first); 19 } 20

class Node 21 { 22 public Node Next; 23 public object Value; 24 public Node(object value): this(value, null) {} 25 public Node(object value, Node next) { 26 Next = next; 27 Value = value; 28 } 29 } 30 } 31

shows a Stack class implemented as a linked list of Node instances. Node instances are created in the Push 32 method and are garbage collected when no longer needed. A Node instance becomes eligible for garbage 33 collection when it is no longer possible for any code to access it. For instance, when an item is removed 34 from the Stack, the associated Node instance becomes eligible for garbage collection. 35

The example 36

class Test 37 { 38 static void Main() { 39 Stack s = new Stack(); 40 for (int i = 0; i < 10; i++) 41 s.Push(i); 42 s = null; 43 } 44 } 45

shows code that uses the Stack class. A Stack is created and initialized with 10 elements, and then 46 assigned the value null. Once the variable s is assigned null, the Stack and the associated 10 Node 47 instances become eligible for garbage collection. The garbage collector is permitted to clean up immediately, 48 but is not required to do so. 49

The garbage collector underlying C# might work by moving objects around in memory, but this motion is 50 invisible to most C# developers. For developers who are generally content with automatic memory 51 management but sometimes need fine-grained control or that extra bit of performance, C# provides the 52 ability to write “unsafe” code. Such code can deal directly with pointer types and object addresses, however, 53 C# requires the programmer to fix objects to temporarily prevent the garbage collector from moving them. 54

This “unsafe” code feature is in fact a “safe” feature from the perspective of both developers and users. 55 Unsafe code shall be clearly marked in the code with the modifier unsafe, so developers can't possibly use 56 unsafe language features accidentally, and the compiler and the execution engine work together to ensure 57

Page 47: C# Language Specification

Chapter 8 Language overview

27

that unsafe code cannot masquerade as safe code. These restrictions limit the use of unsafe code to situations 1 in which the code is trusted. 2

The example 3

using System; 4

class Test 5 { 6 static void WriteLocations(byte[] arr) { 7 unsafe { 8 fixed (byte* pArray = arr) { 9 byte* pElem = pArray; 10 for (int i = 0; i < arr.Length; i++) { 11 byte value = *pElem; 12 Console.WriteLine("arr[{0}] at 0x{1:X} is {2}", 13 i, (uint)pElem, value); 14 pElem++; 15 } 16 } 17 } 18 } 19

static void Main() { 20 byte[] arr = new byte[] {1, 2, 3, 4, 5}; 21 WriteLocations(arr); 22 } 23 } 24

shows an unsafe block in a method named WriteLocations that fixes an array instance and uses pointer 25 manipulation to iterate over the elements. The index, value, and location of each array element are written to 26 the console. One possible example of output is: 27

arr[0] at 0x8E0360 is 1 28 arr[1] at 0x8E0361 is 2 29 arr[2] at 0x8E0362 is 3 30 arr[3] at 0x8E0363 is 4 31 arr[4] at 0x8E0364 is 5 32

but, of course, the exact memory locations can be different in different executions of the application. 33

8.5 Expressions 34

C# includes unary operators, binary operators, and one ternary operator. The following table summarizes the 35 operators, listing them in order of precedence from highest to lowest: 36

37

Page 48: C# Language Specification

C# LANGUAGE SPECIFICATION

28

Subclause Category Operators

14.5 Primary x.y f(x) a[x] x++ x-- new

typeof checked unchecked

14.5.13 Unary + - ! ~ ++x --x (T)x

14.7 Multiplicative * / %

14.7 Additive + -

14.8 Shift << >>

14.9 Relational and type-testing

< > <= >= is as

14.9 Equality == !=

14.10 Logical AND &

14.10 Logical XOR ^

14.10 Logical OR |

14.11 Conditional AND

&&

14.11 Conditional OR ||

14.12 Conditional ?:

14.13 Assignment = *= /= %= += -= <<= >>= &= ^= |=

1 When an expression contains multiple operators, the precedence of the operators controls the order in which 2 the individual operators are evaluated. For example, the expression x + y * z is evaluated as 3 x + (y * z) because the * operator has higher precedence than the + operator. 4

When an operand occurs between two operators with the same precedence, the associativity of the operators 5 controls the order in which the operations are performed: 6

• Except for the assignment operators, all binary operators are left-associative, meaning that operations 7 are performed from left to right. For example, x + y + z is evaluated as (x + y) + z. 8

• The assignment operators and the conditional operator (?:) are right-associative, meaning that 9 operations are performed from right to left. For example, x = y = z is evaluated as x = (y = z). 10

Precedence and associativity can be controlled using parentheses. For example, x + y * z first multiplies 11 y by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z. 12

8.6 Statements 13

C# borrows most of its statements directly from C and C++, though there are some noteworthy additions and 14 modifications. The table below lists the kinds of statements that can be used, and provides an example for 15 each. 16 17

Page 49: C# Language Specification

Chapter 8 Language overview

29

Statement Example

Statement lists and block statements

static void Main() { F(); G(); { H(); I(); } }

Labeled statements and goto statements

static void Main(string[] args) { if (args.Length == 0) goto done; Console.WriteLine(args.Length); done: Console.WriteLine("Done"); }

Local constant declarations static void Main() { const float pi = 3.14f; const int r = 123; Console.WriteLine(pi * r * r); }

Local variable declarations static void Main() { int a; int b = 2, c = 3; a = 1; Console.WriteLine(a + b + c); }

Expression statements static int F(int a, int b) { return a + b; }

static void Main() { F(1, 2); // Expression statement }

if statements static void Main(string[] args) { if (args.Length == 0) Console.WriteLine("No args"); else Console.WriteLine("Args"); }

switch statements static void Main(string[] args) { switch (args.Length) { case 0: Console.WriteLine("No args"); break; case 1: Console.WriteLine("One arg "); break; default: int n = args.Length; Console.WriteLine("{0} args", n); break; } }

while statements static void Main(string[] args) { int i = 0; while (i < args.Length) { Console.WriteLine(args[i]); i++; } }

Page 50: C# Language Specification

C# LANGUAGE SPECIFICATION

30

do statements static void Main() { string s; do { s = Console.ReadLine(); } while (s != "Exit"); }

for statements static void Main(string[] args) { for (int i = 0; i < args.Length; i++) Console.WriteLine(args[i]); }

foreach statements static void Main(string[] args) { foreach (string s in args) Console.WriteLine(s); }

break statements static void Main(string[] args) { int i = 0; while (true) { if (i == args.Length) break; Console.WriteLine(args[i++]); } }

continue statements static void Main(string[] args) { int i = 0; while (true) { Console.WriteLine(args[i++]); if (i < args.Length) continue; break; } }

return statements static int F(int a, int b) { return a + b; }

static void Main() { Console.WriteLine(F(1, 2)); return; }

yield statements static IEnumerable<int> FromTo(int a, int b) { if (a > b) yield break; for ( ; ; a++) { yield return a; if (a == b) break; } }

throw statements and try statements

static int F(int a, int b) { if (b == 0) throw new Exception("Divide by zero"); return a / b; }

static void Main() { try { Console.WriteLine(F(5, 0)); } catch(Exception e) { Console.WriteLine("Error"); } }

Page 51: C# Language Specification

Chapter 8 Language overview

31

checked and unchecked statements

static void Main() { int x = Int32.MaxValue; Console.WriteLine(x + 1); // Overflow checked { Console.WriteLine(x + 1); // Exception } unchecked { Console.WriteLine(x + 1); // Overflow } }

lock statements static void Main() { A a = …; lock(a) { a.P = a.P + 1; } }

using statements static void Main() { using (Resource r = new Resource()) { r.F(); } }

1

8.7 Classes 2

Class declarations define new reference types. A class can inherit from another class, and can implement 3 interfaces. Generic class declarations (§26.1) have one or more type parameters. 4

Class members can include constants, fields, methods, properties, events, indexers, operators, instance 5 constructors, destructors, static constructors, and nested type declarations. Each member has an associated 6 accessibility (§10.5), which controls the regions of program text that are able to access the member. There 7 are five possible forms of accessibility. These are summarized in the table below. 8

9

Form Intuitive meaning public Access not limited protected Access limited to the containing class or types derived from the containing class internal Access limited to this program protected internal

Access limited to this program or types derived from the containing class

private Access limited to the containing type 10 The example 11

using System; 12

class MyClass 13 { 14 public const int MyConst = 12; 15

public int MyField = 34; 16

public static int MyStaticField = 34; 17

public void MyMethod(){ 18 Console.WriteLine("MyClass.MyMethod"); 19 } 20

public int MyProperty { 21 get { 22 return MyField; 23 } 24

Page 52: C# Language Specification

C# LANGUAGE SPECIFICATION

32

set { 1 MyField = value; 2 } 3 } 4

public event EventHandler MyEvent; 5

public int this[int index] { 6 get { 7 return 0; 8 } 9

set { 10 Console.WriteLine("this[{0}] = {1}", index, value); 11 } 12 } 13

public static MyClass operator+(MyClass a, MyClass b) { 14 return new MyClass(a.MyField + b.MyField); 15 } 16

public MyClass() { 17 Console.WriteLine("Instance constructor"); 18 } 19

public MyClass(int value) { 20 MyField = value; 21 Console.WriteLine("Instance constructor"); 22 } 23

~MyClass() { 24 Console.WriteLine("Destructor"); 25 } 26

static MyClass() { 27 MyStaticField *= 2; 28 Console.WriteLine("Static constructor"); 29 } 30

internal class MyNestedClass 31 {} 32 } 33

shows a class that contains each kind of member. The example 34

class Test 35 { 36 static void Main() { 37 // Instance constructor usage 38 MyClass a = new MyClass(); 39 MyClass b = new MyClass(123); 40

// Constant usage 41 Console.WriteLine("MyConst = {0}", MyClass.MyConst); 42

// Field usage 43 a.MyField++; 44 Console.WriteLine("a.MyField = {0}", a.MyField); 45

// Method usage 46 a.MyMethod(); 47

// Property usage 48 a.MyProperty++; 49 Console.WriteLine("a.MyProperty = {0}", a.MyProperty); 50

// Indexer usage 51 a[3] = a[1] = a[2]; 52 Console.WriteLine("a[3] = {0}", a[3]); 53

// Event usage 54 a.MyEvent += new EventHandler(MyHandler); 55

// Overloaded operator usage 56 MyClass c = a + b; 57

Page 53: C# Language Specification

Chapter 8 Language overview

33

// Nested type usage 1 MyClass.MyNestedClass d = new MyClass.MyNestedClass(); 2 } 3

static void MyHandler(object sender, EventArgs e) { 4 Console.WriteLine("Test.MyHandler"); 5 } 6 } 7

shows uses of these members. 8

8.7.1 Constants 9

A constant is a class member that represents a constant value: a value that can be computed at compile-time. 10 Constants are permitted to depend on other constants within the same program as long as there are no 11 circular dependencies. The rules governing constant expressions are defined in §14.15. The example 12

class Constants 13 { 14 public const int A = 1; 15 public const int B = A + 1; 16 } 17

shows a class named Constants that has two public constants. 18

Even though constants are considered static members, a constant declaration neither requires nor allows the 19 modifier static. Constants can be accessed through the class, as in 20

using System; 21

class Test 22 { 23 static void Main() { 24 Console.WriteLine("{0}, {1}", Constants.A, Constants.B); 25 } 26 } 27

which prints out the values of Constants.A and Constants.B, respectively. 28

8.7.2 Fields 29

A field is a member that represents a variable associated with an object or class. The example 30

class Color 31 { 32 internal ushort redPart; 33 internal ushort bluePart; 34 internal ushort greenPart; 35

public Color(ushort red, ushort blue, ushort green) { 36 redPart = red; 37 bluePart = blue; 38 greenPart = green; 39 } 40

public static Color Red = new Color(0xFF, 0, 0); 41 public static Color Blue = new Color(0, 0xFF, 0); 42 public static Color Green = new Color(0, 0, 0xFF); 43 public static Color White = new Color(0xFF, 0xFF, 0xFF); 44 } 45

shows a Color class that has internal instance fields named redPart, bluePart, and greenPart, and 46 static fields named Red, Blue, Green, and White 47

The use of static fields in this manner is not ideal. The fields are initialized at some point before they are 48 used, but after this initialization there is nothing to stop a client from changing them. Such a modification 49 could cause unpredictable errors in other programs that use Color and assume that the values do not 50 change. Readonly fields can be used to prevent such problems. Assignments to a readonly field can only 51 occur as part of the declaration, or in an instance constructor or static constructor in the same class. A static 52 readonly field can be assigned in a static constructor, and a non-static readonly field can be assigned in an 53

Page 54: C# Language Specification

C# LANGUAGE SPECIFICATION

34

instance constructor. Thus, the Color class can be enhanced by adding the modifier readonly to the static 1 fields: 2

class Color 3 { 4 internal ushort redPart; 5 internal ushort bluePart; 6 internal ushort greenPart; 7

public Color(ushort red, ushort blue, ushort green) { 8 redPart = red; 9 bluePart = blue; 10 greenPart = green; 11 } 12

public static readonly Color Red = new Color(0xFF, 0, 0); 13 public static readonly Color Blue = new Color(0, 0xFF, 0); 14 public static readonly Color Green = new Color(0, 0, 0xFF); 15 public static readonly Color White = new Color(0xFF, 0xFF, 0xFF); 16 } 17

8.7.3 Methods 18

A method is a member that implements a computation or action that can be performed by an object or class. 19 Methods have a (possibly empty) list of formal parameters, a return value (unless the method’s return-type is 20 void), and are either static or non-static. Static methods are accessed through the class. Non-static methods, 21 which are also called instance methods, are accessed through instances of the class. A generic method 22 (§26.6) has a list of one or more type parameters. The example 23

using System; 24

public class Stack 25 { 26 public static Stack Clone(Stack s) {…} 27

public static Stack Flip(Stack s) {…} 28

public object Pop() {…} 29

public void Push(object o) {…} 30

public void PushMultiple<T>(T[] a) {…} 31

public override string ToString() {…} 32 … 33 } 34

class Test 35 { 36 static void Main() { 37 Stack s = new Stack(); 38 for (int i = 1; i < 10; i++) 39 s.Push(i); 40

Stack flipped = Stack.Flip(s); 41

Stack cloned = Stack.Clone(s); 42

Console.WriteLine("Original stack: " + s.ToString()); 43 Console.WriteLine("Flipped stack: " + flipped.ToString()); 44 Console.WriteLine("Cloned stack: " + cloned.ToString()); 45 } 46 } 47

shows a Stack that has several static methods (Clone and Flip) and several instance methods (Pop, Push, 48 and ToString) and a generic method (PushMultiple<T>). 49

Methods can be overloaded, which means that multiple methods can have the same name so long as they 50 have unique signatures. The signature of a method consists of the name of the method and the number, 51 modifiers, and types of its formal parameters, and the number of generic type parameters. The signature of a 52 method does not include the return type or the names of the formal parameters or type parameters. The 53 example 54

Page 55: C# Language Specification

Chapter 8 Language overview

35

using System; 1

class Test 2 { 3 static void F() { 4 Console.WriteLine("F()"); 5 } 6

static void F(object o) { 7 Console.WriteLine("F(object)"); 8 } 9

static void F(int value) { 10 Console.WriteLine("F(int)"); 11 } 12

static void F(ref int value) { 13 Console.WriteLine("F(ref int)"); 14 } 15

static void F(int a, int b) { 16 Console.WriteLine("F(int, int)"); 17 } 18

static void F(int[] values) { 19 Console.WriteLine("F(int[])"); 20 } 21

static void F<T>(T t) { 22 Console.WriteLine("F<T>(T)"); 23 } 24

static void Main() { 25 F(); 26 F(1); 27 int i = 10; 28 F(ref i); 29 F((object)1); 30 F(1, 2); 31 F(new int[] {1, 2, 3}); 32 F("Hello"); 33 F<string>("World"); 34 } 35 } 36

shows a class with a number of methods called F. The output produced is 37

F() 38 F(int) 39 F(ref int) 40 F(object) 41 F(int, int) 42 F(int[]) 43 F<T>(T) 44 F<T>(T) 45

8.7.4 Properties 46

A property is a member that provides access to a characteristic of an object or a class. Examples of 47 properties include the length of a string, the size of a font, the caption of a window, the name of a customer, 48 and so on. Properties are a natural extension of fields. Both are named members with associated types, and 49 the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote 50 storage locations. Instead, properties have accessors that specify the statements to be executed when their 51 values are read or written. 52

Properties are defined with property declarations. The first part of a property declaration looks quite similar 53 to a field declaration. The second part includes a get accessor and/or a set accessor. In the example below, 54 the Button class defines a Caption property. 55

Page 56: C# Language Specification

C# LANGUAGE SPECIFICATION

36

public class Button 1 { 2 private string caption; 3

public string Caption { 4 get { 5 return caption; 6 } 7

set { 8 caption = value; 9 Repaint(); 10 } 11 } 12 … 13 } 14

Properties that can be both read and written, such as Caption, include both get and set accessors. The get 15 accessor is called when the property’s value is read; the set accessor is called when the property’s value is 16 written. In a set accessor, the new value for the property is made available via an implicit parameter named 17 value. 18

The declaration of properties is relatively straightforward, but the real value of properties is seen when they 19 are used. For example, the Caption property can be read and written in the same way that fields can be read 20 and written: 21

Button b = new Button(); 22 b.Caption = "ABC"; // set; causes repaint 23 string s = b.Caption; // get 24 b.Caption += "DEF"; // get & set; causes repaint 25

8.7.5 Events 26

An event is a member that enables an object or class to provide notifications. A class defines an event by 27 providing an event declaration (which resembles a field declaration, though with an added event keyword) 28 and an optional set of event accessors. The type of this declaration shall be a delegate type. 29

An instance of a delegate type encapsulates one or more callable entities. For instance methods, a callable 30 entity consists of an instance and a method on that instance. For static methods, a callable entity consists of 31 just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all of that 32 delegate instance’s methods with that set of arguments. 33

In the example 34

public delegate void EventHandler(object sender, System.EventArgs e); 35

public class Button 36 { 37 public event EventHandler Click; 38 public void Reset() { 39 Click = null; 40 } 41 } 42

the Button class defines a Click event of type EventHandler. Inside the Button class, the Click 43 member is exactly like a private field of type EventHandler. However, outside the Button class, the 44 Click member can only be used on the left-hand side of the += and –= operators. The += operator adds a 45 handler for the event, and the -= operator removes a handler for the event. The example 46

using System; 47

public class Form1 48 { 49 public Form1() { 50 // Add Button1_Click as an event handler for Button1’s Click event 51 Button1.Click += new EventHandler(Button1_Click); 52 } 53

Button Button1 = new Button(); 54

Page 57: C# Language Specification

Chapter 8 Language overview

37

void Button1_Click(object sender, EventArgs e) { 1 Console.WriteLine("Button1 was clicked!"); 2 } 3

public void Disconnect() { 4 Button1.Click -= new EventHandler(Button1_Click); 5 } 6 } 7

shows a Form1 class that adds Button1_Click as an event handler for Button1’s Click event. In the 8 Disconnect method, that event handler is removed. 9

For a simple event declaration such as 10

public event EventHandler Click; 11

the compiler automatically provides the implementation underlying the += and -= operators. 12

An implementer who wants more control can get it by explicitly providing add and remove accessors. For 13 example, the Button class could be rewritten as follows: 14

public class Button 15 { 16 private EventHandler handler; 17 public event EventHandler Click { 18 add { handler += value; } 19 20

remove { handler -= value; } 21 } 22 } 23

This change has no effect on client code, but allows the Button class more implementation flexibility. For 24 example, the event handler for Click need not be represented by a field. 25

8.7.6 Operators 26

An operator is a member that defines the meaning of an expression operator that can be applied to instances 27 of the class. There are three kinds of operators that can be defined: unary operators, binary operators, and 28 conversion operators. 29

The following example defines a Digit type that represents decimal digits—integral values between 0 30 and 9. 31

using System; 32

public struct Digit 33 { 34 byte value; 35

public Digit(int value) { 36 if (value < 0 || value > 9) throw new ArgumentException(); 37 this.value = (byte)value; 38 } 39

public static implicit operator byte(Digit d) { 40 return d.value; 41 } 42

public static explicit operator Digit(int value) { 43 return new Digit(value); 44 } 45

public static Digit operator+(Digit a, Digit b) { 46 return new Digit(a.value + b.value); 47 } 48

public static Digit operator-(Digit a, Digit b) { 49 return new Digit(a.value - b.value); 50 } 51

Page 58: C# Language Specification

C# LANGUAGE SPECIFICATION

38

public static bool operator==(Digit a, Digit b) { 1 return a.value == b.value; 2 } 3

public static bool operator!=(Digit a, Digit b) { 4 return a.value != b.value; 5 } 6

public override bool Equals(object value) { 7 if (value == null) return false; 8 if (GetType() == value.GetType()) return this == (Digit)value; 9 return false; 10 } 11

public override int GetHashCode() { 12 return value.GetHashCode(); 13 } 14

public override string ToString() { 15 return value.ToString(); 16 } 17 } 18

class Test 19 { 20 static void Main() { 21 Digit a = (Digit) 5; 22 Digit b = (Digit) 3; 23 Digit plus = a + b; 24 Digit minus = a - b; 25 bool equals = (a == b); 26 Console.WriteLine("{0} + {1} = {2}", a, b, plus); 27 Console.WriteLine("{0} - {1} = {2}", a, b, minus); 28 Console.WriteLine("{0} == {1} = {2}", a, b, equals); 29 } 30 } 31

The Digit type defines the following operators: 32

• An implicit conversion operator from Digit to byte. 33

• An explicit conversion operator from int to Digit. 34

• An addition operator that adds two Digit values and returns a Digit value. 35

• A subtraction operator that subtracts one Digit value from another, and returns a Digit value. 36

• The equality (==) and inequality (!=) operators, which compare two Digit values. 37

8.7.7 Indexers 38

An indexer is a member that enables an object to be indexed in the same way as an array. Whereas 39 properties enable field-like access, indexers enable array-like access. 40

As an example, consider the Stack class presented earlier. The designer of this class might want to expose 41 array-like access so that it is possible to inspect or alter the items on the stack without performing 42 unnecessary Push and Pop operations. That is, class Stack is implemented as a linked list, but it also 43 provides the convenience of array access. 44

Indexer declarations are similar to property declarations, with the main differences being that indexers are 45 nameless (the “name” used in the declaration is this, since this is being indexed) and that indexers 46 include indexing parameters. The indexing parameters are provided between square brackets. The example 47

using System; 48

Page 59: C# Language Specification

Chapter 8 Language overview

39

public class Stack 1 { 2 private Node GetNode(int index) { 3 Node temp = first; 4 while (true) { 5 if (temp == null || index < 0) 6 throw new Exception("Index out of range."); 7 if (index == 0) 8 return temp; 9 temp = temp.Next; 10 index--; 11 } 12 } 13

public object this[int index] { 14 get { 15 return GetNode(index).Value; 16 } 17

set { 18 GetNode(index).Value = value; 19 } 20 } 21

… 22 } 23

class Test 24 { 25 static void Main() { 26 Stack s = new Stack(); 27

s.Push(1); 28 s.Push(2); 29 s.Push(3); 30

s[0] = 33; // Changes the top item from 3 to 33 31 s[1] = 22; // Changes the middle item from 2 to 22 32 s[2] = 11; // Changes the bottom item from 1 to 11 33 } 34 } 35

shows an indexer for the Stack class. 36

8.7.8 Instance constructors 37

An instance constructor is a member that implements the actions required to initialize an instance of a class. 38

The example 39

using System; 40

class Point 41 { 42 public double x, y; 43

public Point() { 44 this.x = 0; 45 this.y = 0; 46 } 47

public Point(double x, double y) { 48 this.x = x; 49 this.y = y; 50 } 51

public static double Distance(Point a, Point b) { 52 double xdiff = a.x - b.x; 53 double ydiff = a.y - b.y; 54 return Math.Sqrt(xdiff * xdiff + ydiff * ydiff); 55 } 56

Page 60: C# Language Specification

C# LANGUAGE SPECIFICATION

40

public override string ToString() { 1 return string.Format("({0}, {1})", x, y); 2 } 3 } 4

class Test 5 { 6 static void Main() { 7 Point a = new Point(); 8 Point b = new Point(3, 4); 9 double d = Point.Distance(a, b); 10 Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d); 11 } 12 } 13

shows a Point class that provides two public instance constructors, one of which takes no arguments, while 14 the other takes two double arguments. 15

If no instance constructor is supplied for a class, then one having no parameters is automatically provided, 16 which simply invokes the parameterless constructor of the direct base class. 17

8.7.9 Destructors 18

A destructor is a member that implements the actions required to destruct an instance of a class. Destructors 19 cannot have parameters, they cannot have accessibility modifiers, and they cannot be called explicitly. The 20 destructor for an instance is called automatically during garbage collection. 21

The example 22

using System; 23

class Point 24 { 25 public double x, y; 26

public Point(double x, double y) { 27 this.x = x; 28 this.y = y; 29 } 30

~Point() { 31 Console.WriteLine("Destructed {0}", this); 32 } 33

public override string ToString() { 34 return string.Format("({0}, {1})", x, y); 35 } 36 } 37

shows a Point class with a destructor. 38

8.7.10 Static constructors 39

A static constructor is a member that implements the actions required to initialize a class. Static constructors 40 cannot have parameters, they cannot have accessibility modifiers, and they cannot be called explicitly. The 41 static constructor for a class is called automatically. 42

The example 43

using Personnel.Data; 44 class Employee 45 { 46 private static DataSet ds; 47

static Employee() { 48 ds = new DataSet(…); 49 } 50

Page 61: C# Language Specification

Chapter 8 Language overview

41

public string Name; 1 public decimal Salary; 2 … 3 } 4

shows an Employee class with a static constructor that initializes a static field. 5

8.7.11 Inheritance 6

Classes support single inheritance, and the type object is the ultimate base class for all classes. 7

The classes shown in earlier examples all implicitly derive from object. The example 8

using System; 9

class A 10 { 11 public void F() { Console.WriteLine("A.F"); } 12 } 13

shows a class A that implicitly derives from object. The example 14

class B: A 15 { 16 public void G() { Console.WriteLine("B.G"); } 17 } 18

class Test 19 { 20 static void Main() { 21 B b = new B(); 22 b.F(); // Inherited from A 23 b.G(); // Introduced in B 24 25

A a = b; // Treat a B as an A 26 a.F(); 27 } 28 } 29

shows a class B that derives from A. The class B inherits A’s F method, and introduces a G method of its own. 30

Methods, properties, and indexers can be virtual, which means that their implementation can be overridden 31 in derived classes. The example 32

using System; 33

class A 34 { 35 public virtual void F() { Console.WriteLine("A.F"); } 36 } 37

class B: A 38 { 39 public override void F() { 40 base.F(); 41 Console.WriteLine("B.F"); 42 } 43 } 44

class Test 45 { 46 static void Main() { 47 B b = new B(); 48 b.F(); 49

A a = b; 50 a.F(); 51 } 52 } 53

shows a class A with a virtual method F, and a class B that overrides F. The overriding method in B contains 54 a call, base.F(), which calls the overridden method in A. 55

Page 62: C# Language Specification

C# LANGUAGE SPECIFICATION

42

A class can indicate that it is incomplete, and is intended only as a base class for other classes, by including 1 the modifier abstract. Such a class is called an abstract class. An abstract class can specify abstract 2 members—members that a non-abstract derived class shall implement. The example 3

using System; 4

abstract class A 5 { 6 public abstract void F(); 7 } 8

class B: A 9 { 10 public override void F() { Console.WriteLine("B.F"); } 11 } 12

class Test 13 { 14 static void Main() { 15 B b = new B(); 16 b.F(); 17

A a = b; 18 a.F(); 19 } 20 } 21

introduces an abstract method F in the abstract class A. The non-abstract class B provides an implementation 22 for this method. 23

8.7.12 Static classes 24

Classes that are not intended to be instantiated, and which contain only static members should be declared as 25 static classes. Examples of such classes are System.Console and System.Environment. Static classes 26 are implicitly sealed and have no instance constructors. Static classes can be used only with the typeof 27 operator and to access elements of the class. In particular, a static class cannot be used as the type of a 28 variable or be used as a type argument. 29

public static class Months 30 { 31 static Months() { … } 32 private static readonly string[] monthName = { … } 33 public static string GetMonthName(int mm) { … } 34 private static readonly int[,] daysInMonth = { … } 35 public static int GetDaysInMonth(bool isLeapYear, int mm) { … } 36 public static bool IsLeapYear(int yy) { … } 37 } 38

8.7.13 Partial type declarations 39

In certain situations, the declaration of a type may grow so large that keeping it in a single source file 40 becomes impractical or difficult. In such cases, it is often desirable to split that class declaration into 41 multiple source files, with each source file focussing on one or more semi-independent concerns. 42

Another common situation occurs when code is generated from a program rather than written by a person. In 43 rich frameworks and development environments, it is often most efficient to have parts of a project’s source 44 code generated automatically from visual form designers, database schemas, RPC descriptions, etc. While 45 these kinds of tools produce huge productivity gains, they suffer from problems when we wish to customize 46 the output, possibly by adding members to generated classes. If we directly modify the output of the code 47 generator, then those changes will be lost if the code generator needs to be run again. By placing the 48 customized additions in a different source file, lost modifications can be greatly reduced or eliminated. 49

Partial type declarations allow greater flexibility in these situations by allowing the definition of a class, 50 struct, or interface to be split into as many different pieces as needed. For example, when the following 51 source files are compiled together: 52

Page 63: C# Language Specification

Chapter 8 Language overview

43

// machine-generated code in file #1 1 partial class Widget 2 { 3 private int[] counts; 4 public string ToString() { 5 ... 6 } 7 } 8

// programmer-generated code in file #2 9 partial class Widget 10 { 11 private int value; 12 private void Helper() { 13 // ... 14 } 15

public int Process(object obj) { 16 ... 17 } 18 } 19

Widget’s members are the union of all the members in all its parts. 20

8.8 Structs 21

The list of similarities between classes and structs is long—structs can implement interfaces, and can have 22 the same kinds of members as classes. Structs differ from classes in several important ways, however: 23 structs are value types rather than reference types, and inheritance is not supported for structs. Struct values 24 are stored “on the stack” or “in-line”. Careful programmers can sometimes enhance performance through 25 judicious use of structs. 26

For example, the use of a struct rather than a class for a Point can make a large difference in the number of 27 memory allocations performed at run time. The program below creates and initializes an array of 100 points. 28 With Point implemented as a class, 101 separate objects are instantiated—one for the array and one each 29 for the 100 elements. 30

class Point 31 { 32 public int x, y; 33

public Point(int x, int y) { 34 this.x = x; 35 this.y = y; 36 } 37 } 38

class Test 39 { 40 static void Main() { 41 Point[] points = new Point[100]; 42 for (int i = 0; i < 100; i++) 43 points[i] = new Point(i, i*i); 44 } 45 } 46

If Point is instead implemented as a struct, as in 47

struct Point 48 { 49 public int x, y; 50

public Point(int x, int y) { 51 this.x = x; 52 this.y = y; 53 } 54 } 55

only one object is instantiated—the one for the array. The Point instances are allocated in-line within the 56 array. This optimization can be misused. Using structs instead of classes can also make an application run 57

Page 64: C# Language Specification

C# LANGUAGE SPECIFICATION

44

slower or take up more memory, as passing a struct instance by value causes a copy of that struct to be 1 created. 2

8.9 Interfaces 3

An interface defines a contract. A class or struct that implements an interface shall adhere to its contract. 4 Interfaces can contain methods, properties, events, and indexers as members. 5

The example 6

interface IExample 7 { 8 string this[int index] { get; set; } 9 event EventHandler E; 10 void F(int value); 11 string P { get; set; } 12 } 13

public delegate void EventHandler(object sender, EventArgs e); 14

shows an interface that contains an indexer, an event E, a method F, and a property P. 15

Interfaces can employ multiple inheritance. In the example 16

interface IControl 17 { 18 void Paint(); 19 } 20

interface ITextBox: IControl 21 { 22 void SetText(string text); 23 } 24

interface IListBox: IControl 25 { 26 void SetItems(string[] items); 27 } 28

interface IComboBox: ITextBox, IListBox {} 29

the interface IComboBox inherits from both ITextBox and IListBox. 30

Classes and structs can implement multiple interfaces. In the example 31

interface IDataBound 32 { 33 void Bind(Binder b); 34 } 35

public class EditBox: Control, IControl, IDataBound 36 { 37 public void Paint() {…} 38 public void Bind(Binder b) {…} 39 } 40

the class EditBox derives from the class Control and implements both IControl and IDataBound. 41

In the previous example, the Paint method from the IControl interface and the Bind method from 42 IDataBound interface are implemented using public members on the EditBox class. C# provides an 43 alternative way of implementing these methods that allows the implementing class to avoid having these 44 members be public. Interface members can be implemented using a qualified name. For example, the 45 EditBox class could instead be implemented by providing IControl.Paint and IDataBound.Bind 46 methods. 47

public class EditBox: IControl, IDataBound 48 { 49 void IControl.Paint() {…} 50 void IDataBound.Bind(Binder b) {…} 51 } 52

Page 65: C# Language Specification

Chapter 8 Language overview

45

Interface members implemented in this way are called explicit interface members because each member 1 explicitly designates the interface member being implemented. Explicit interface members can only be 2 called via the interface. For example, the EditBox’s implementation of the Paint method can be called 3 only by casting to the IControl interface. 4

class Test 5 { 6 static void Main() { 7 EditBox editbox = new EditBox(); 8 editbox.Paint(); // error: no such method 9 IControl control = editbox; 10 control.Paint(); // calls EditBox’s Paint implementation 11 } 12 } 13

8.10 Delegates 14

Delegates enable scenarios that some other languages have addressed with function pointers. However, 15 unlike function pointers, delegates are object-oriented and type-safe. 16

A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instance 17 encapsulates one or more methods, each of which is referred to as a callable entity. For instance methods, a 18 callable entity consists of an instance and a method on that instance. For static methods, a callable entity 19 consists of just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all 20 of that delegate instance’s methods with that set of arguments. 21

An interesting and useful property of a delegate instance is that it does not know or care about the classes of 22 the methods it encapsulates; all that matters is that those methods be consistent (§22.1) with the delegate’s 23 type. This makes delegates perfectly suited for “anonymous” invocation. This is a powerful capability. 24

There are three steps in defining and using delegates: declaration, instantiation, and invocation. Delegates 25 are declared using delegate declaration syntax. The example 26

delegate void SimpleDelegate(); 27

declares a delegate named SimpleDelegate that takes no arguments and returns no result. 28

The example 29

class Test 30 { 31 static void F() { 32 System.Console.WriteLine("Test.F"); 33 } 34

static void Main() { 35 SimpleDelegate d = new SimpleDelegate(F); 36 d(); 37 } 38 } 39

creates a SimpleDelegate instance and then immediately calls it. 40

There is not much point in instantiating a delegate for a method and then immediately calling that method 41 via the delegate, as it would be simpler to call the method directly. Delegates really show their usefulness 42 when their anonymity is used. The example 43

void MultiCall(SimpleDelegate d, int count) { 44 for (int i = 0; i < count; i++) { 45 d(); 46 } 47 } 48

shows a MultiCall method that repeatedly calls a SimpleDelegate. The MultiCall method doesn’t 49 know or care about the type of the target method for the SimpleDelegate, what accessibility that method 50 has, or whether or not that method is static. All that matters is that the target method is consistent (§22.1) 51 with SimpleDelegate. 52

Page 66: C# Language Specification

C# LANGUAGE SPECIFICATION

46

8.11 Enums 1

An enum type declaration defines a type name for a related group of symbolic constants. Enums are used for 2 “multiple choice” scenarios, in which a runtime decision is made from a fixed number of choices that are 3 known at compile-time. 4

The example 5

enum Color 6 { 7 Red, 8 Blue, 9 Green 10 } 11

class Shape 12 { 13 public void Fill(Color color) { 14 switch(color) { 15 case Color.Red: 16 … 17 break; 18

case Color.Blue: 19 … 20 break; 21

case Color.Green: 22 … 23 break; 24

default: 25 break; 26 } 27 } 28 } 29

shows a Color enum and a method that uses this enum. The signature of the Fill method makes it clear 30 that the shape can be filled with one of the given colors. 31

The use of enums is superior to the use of integer constants—as is common in languages without enums—32 because the use of enums makes the code more readable and self-documenting. The self-documenting nature 33 of the code also makes it possible for the development tool to assist with code writing and other “designer” 34 activities. For example, the use of Color rather than int for a parameter type enables smart code editors to 35 suggest Color values. 36

8.12 Namespaces and assemblies 37

The programs presented so far have stood on their own except for dependence on a few system-provided 38 classes such as System.Console. It is far more common, however, for real-world applications to consist of 39 several different pieces, each compiled separately. For example, a corporate application might depend on 40 several different components, including some developed internally and some purchased from independent 41 software vendors. 42

Namespaces and assemblies enable this component-based system. Namespaces provide a logical 43 organizational system. Namespaces are used both as an “internal” organization system for a program, and as 44 an “external” organization system—a way of presenting program elements that are exposed to other 45 programs. 46

Assemblies are used for physical packaging and deployment. An assembly can contain types, the executable 47 code used to implement these types, and references to other assemblies. 48

To demonstrate the use of namespaces and assemblies, this subclause revisits the “hello, world” program 49 presented earlier, and splits it into two pieces: a class library that provides messages and a console 50 application that displays them. 51

The class library will contain a single class named HelloMessage. The example 52

Page 67: C# Language Specification

Chapter 8 Language overview

47

// HelloLibrary.cs 1

namespace CSharp.Introduction 2 { 3 public class HelloMessage 4 { 5 public string Message { 6 get { 7 return "hello, world"; 8 } 9 } 10 } 11 } 12

shows the HelloMessage class in a namespace named CSharp.Introduction. The HelloMessage 13 class provides a read-only property named Message. Namespaces can nest, and the declaration 14

namespace CSharp.Introduction 15 {…} 16

is shorthand for two levels of namespace nesting: 17

namespace CSharp 18 { 19 namespace Introduction 20 {…} 21 } 22

The next step in the componentization of “hello, world” is to write a console application that uses the 23 HelloMessage class. The fully qualified name (§10.8.1) for the class—24 CSharp.Introduction.HelloMessage—could be used, but this name is quite long and unwieldy. An 25 easier way is to use a using namespace directive, which makes it possible to use all of the types in a 26 namespace without qualification. The example 27

// HelloApp.cs 28

using CSharp.Introduction; 29

class HelloApp 30 { 31 static void Main() { 32 HelloMessage m = new HelloMessage(); 33 System.Console.WriteLine(m.Message); 34 } 35 } 36

shows a using namespace directive that refers to the CSharp.Introduction namespace. The occurrences 37 of HelloMessage are shorthand for CSharp.Introduction.HelloMessage. 38

C# also enables the definition and use of aliases. A using alias directive defines an alias for a type or 39 namespace. Such aliases can be useful in situation in which name collisions occur between two class 40 libraries, or when a small number of types from a much larger namespace are being used. The example 41

using MessageSource = CSharp.Introduction.HelloMessage; 42

shows a using alias directive that defines MessageSource as an alias for the HelloMessage class. 43

The code we have written can be compiled into a class library containing the class HelloMessage and an 44 application containing the class HelloApp. The details of this compilation step might differ based on the 45 compiler or tool being used. A command-line compiler might enable compilation of a class library and an 46 application that uses that library with the following command-line invocations: 47

csc /target:library HelloLibrary.cs 48

csc /reference:HelloLibrary.dll HelloApp.cs 49

which produce a class library named HelloLibrary.dll and an application named HelloApp.exe. 50

Page 68: C# Language Specification

C# LANGUAGE SPECIFICATION

48

8.13 Versioning 1

Versioning is the process of evolving a component over time in a compatible manner. A new version of a 2 component is source compatible with a previous version if code that depends on the previous version can, 3 when recompiled, work with the new version. In contrast, a new version of a component is binary 4 compatible if an application that depended on the old version can, without recompilation, work with the new 5 version. 6

Most languages do not support binary compatibility at all, and many do little to facilitate source 7 compatibility. In fact, some languages contain flaws that make it impossible, in general, to evolve a class 8 over time without breaking at least some client code. 9

As an example, consider the situation of a base class author who ships a class named Base. In the first 10 version, Base contains no method F. A component named Derived derives from Base, and introduces 11 an F. This Derived class, along with the class Base on which it depends, is released to customers, who 12 deploy to numerous clients and servers. 13

// Author A 14 namespace A 15 { 16 public class Base // version 1 17 { 18 } 19 } 20

// Author B 21 namespace B 22 { 23 class Derived: A.Base 24 { 25 public virtual void F() { 26 System.Console.WriteLine("Derived.F"); 27 } 28 } 29 } 30

So far, so good, but now the versioning trouble begins. The author of Base produces a new version, giving it 31 its own method F. 32

// Author A 33 namespace A 34 { 35 public class Base // version 2 36 { 37 public virtual void F() { // added in version 2 38 System.Console.WriteLine("Base.F"); 39 } 40 } 41 } 42

This new version of Base should be both source and binary compatible with the initial version. (If it weren’t 43 possible to simply add a method then a base class could never evolve.) Unfortunately, the new F in Base 44 makes the meaning of Derived’s F unclear. Did Derived mean to override Base’s F? This seems unlikely, 45 since when Derived was compiled, Base did not even have an F! Further, if Derived’s F does override 46 Base’s F, then it shall adhere to the contract specified by Base—a contract that was unspecified when 47 Derived was written. In some cases, this is impossible. For example, Base’s F might require that overrides 48 of it always call the base. Derived’s F could not possibly adhere to such a contract. 49

C# addresses this versioning problem by requiring developers to state their intent clearly. In the original 50 code example, the code was clear, since Base did not even have an F. Clearly, Derived’s F is intended as a 51 new method rather than an override of a base method, since no base method named F exists. 52

If Base adds an F and ships a new version, then the intent of a binary version of Derived is still clear—53 Derived’s F is semantically unrelated, and should not be treated as an override. 54

Page 69: C# Language Specification

Chapter 8 Language overview

49

However, when Derived is recompiled, the meaning is unclear—the author of Derived might intend its F 1 to override Base’s F, or to hide it. Since the intent is unclear, the compiler produces a warning, and by 2 default makes Derived’s F hide Base’s F. This course of action duplicates the semantics for the case in 3 which Derived is not recompiled. The warning that is generated alerts Derived’s author to the presence of 4 the F method in Base. 5

If Derived’s F is semantically unrelated to Base’s F, then Derived’s author can express this intent—and, 6 in effect, turn off the warning—by using the new keyword in the declaration of F. 7

// Author A 8 namespace A 9 { 10 public class Base // version 2 11 { 12 public virtual void F() { // added in version 2 13 System.Console.WriteLine("Base.F"); 14 } 15 } 16 } 17

// Author B 18 namespace B 19 { 20 class Derived: A.Base // version 2a: new 21 { 22 new public virtual void F() { 23 System.Console.WriteLine("Derived.F"); 24 } 25 } 26 } 27

On the other hand, Derived’s author might investigate further, and decide that Derived’s F should 28 override Base’s F. This intent can be specified by using the override keyword, as shown below. 29

// Author A 30 namespace A 31 { 32 public class Base // version 2 33 { 34 public virtual void F() { // added in version 2 35 System.Console.WriteLine("Base.F"); 36 } 37 } 38 } 39

// Author B 40 namespace B 41 { 42 class Derived: A.Base // version 2b: override 43 { 44 public override void F() { 45 base.F(); 46 System.Console.WriteLine("Derived.F"); 47 } 48 } 49 } 50

The author of Derived has one other option, and that is to change the name of F, thus completely avoiding 51 the name collision. Although this change would break source and binary compatibility for Derived, the 52 importance of this compatibility varies depending on the scenario. If Derived is not exposed to other 53 programs, then changing the name of F is likely a good idea, as it would improve the readability of the 54 program—there would no longer be any confusion about the meaning of F. 55

8.14 Extern Aliases 56

By default types from all referenced assemblies and the current program are placed into a single namespace 57 hierarchy. With only a single namespace hierarchy, it is not possible to reference types with the same fully 58

Page 70: C# Language Specification

C# LANGUAGE SPECIFICATION

50

qualified name from different assemblies, a situation that arises when types are independently given the 1 same name, or when a program needs to reference several versions of the same assembly. Extern aliases 2 make it possible to create and reference separate namespace hierarchies in such situations. 3

Consider the following two assemblies: 4

Assembly a1.dll: 5

namespace N 6 { 7 public class A {} 8

public class B {} 9 } 10

Assembly a2.dll: 11

namespace N 12 { 13 public class B {} 14

public class C {} 15 } 16

and the following program: 17

class Test 18 { 19 N.A a; // Ok 20 N.B b; // Error 21 N.C c; // Ok 22 } 23

A command-line compiler might allow compilation of this program with a command-line something like 24 this: 25

csc /r:a1.dll /r:a2.dll test.cs 26

where the types contained in a1.dll and a2.dll are all placed in the global namespace hierarchy, and an 27 error occurs because the type N.B exists in both assemblies. With extern aliases, it becomes possible to place 28 the types contained in a1.dll and a2.dll into separate namespace hierarchies. 29

The following program declares and uses two extern aliases, X and Y, each of which represent the root of a 30 distinct namespace hierarchy created from the types contained in one or more assemblies. 31

extern alias X; 32 extern alias Y; 33

class Test 34 { 35 X::N.A a; 36 X::N.B b1; 37 Y::N.B b2; 38 Y::N.C c; 39 } 40

The program declares the existence of the extern aliases X and Y, but the actual definitions of the aliases are 41 external to the program. A command line compiler can enable the definition of the extern aliases X and Y 42 such that the extern alias X is the root of a namespace hierarchy formed by the types in a1.dll and Y is the 43 root of a namespace hierarchy formed by the types in a2.dll. A compiler might enable the above example 44 with a command-line like: 45

csc /r:X=a1.dll /r:Y=a2.dll test.cs 46

The identically named N.B classes can now be referenced as X.N.B and Y.N.B, or, using the namespace 47 alias qualifier, X::N.B and Y::N.B. An error occurs if a program declares an extern alias for which no 48 external definition is provided. 49

An extern alias can include multiple assemblies, and a particular assembly can be included in multiple extern 50 aliases. For example, given the assembly 51

Page 71: C# Language Specification

Chapter 8 Language overview

51

Assembly a3.dll: 1

namespace N 2 { 3 public class D {} 4

public class E {} 5 } 6

a command line like 7

csc /r:X=a1.dll /r:X=a3.dll /r:Y=a2.dll /r:Y=a3.dll test.cs 8

might define the extern alias X to be the root of a namespace hierarchy formed by the types in a1.dll and 9 a3.dll and Y to be the root of a namespace hierarchy formed by the types in a2.dll and a3.dll. Because 10 of this definition, it is possible to refer to the class N.D in a3.dll as both X::N.D and Y::N.D. 11

An assembly can be placed in the global namespace hierarchy even if it is also included in one or more 12 extern aliases. For example, the command line 13

csc /r:a1.dll /r:X=a1.dll /r:Y=a2.dll test.cs 14

places the assembly a1.dll in both the global namespace hierarchy and the namespace hierarchy rooted by 15 the extern alias X. Consequently, the class N.A can be referred to as N.A or X::N.A. 16

It is possible to ensure that a lookup always starts at the root of the global namespace hierarchy by using the 17 identifier global with the namespace alias qualifier, such as global::System.IO.Stream. 18

A using directive may reference an extern alias that was defined in the same immediately enclosing 19 namespace declaration or compilation unit. For example: 20

extern alias X; 21

using X::N; 22

class Test 23 { 24 A a; // X::N.A 25 B b; // X::N.B 26 } 27

8.15 Attributes 28

C# is an imperative language, but like all imperative languages it does have some declarative elements. For 29 example, the accessibility of a method in a class is specified by declaring it public, protected, 30 internal, protected internal, or private. C# generalizes this capability, so that programmers can 31 invent new kinds of declarative information, attach this declarative information to various program entities, 32 and retrieve this declarative information at run-time. Programs specify this additional declarative 33 information by defining and using attributes (§24). 34

For instance, a framework might define a HelpAttribute attribute that can be placed on program elements 35 such as classes and methods, enabling developers to provide a mapping from program elements to 36 documentation for them. The example 37

using System; 38

[AttributeUsage(AttributeTargets.All)] 39 public class HelpAttribute: Attribute 40 { 41 public HelpAttribute(string url) { 42 this.url = url; 43 } 44

public string Topic = null; 45

private string url; 46

public string Url { 47 get { return url; } 48 } 49 } 50

Page 72: C# Language Specification

C# LANGUAGE SPECIFICATION

52

defines an attribute class named HelpAttribute, or Help for short, that has one positional parameter 1 (string url) and one named parameter (string Topic). Positional parameters are defined by the 2 formal parameters for public instance constructors of the attribute class, and named parameters are defined 3 by public non-static read-write fields and properties of the attribute class. 4

The example 5

[Help("http://www.mycompany.com/…/Class1.htm")] 6 public class Class1 7 { 8 [Help("http://www.mycompany.com/…/Class1.htm", Topic = "F")] 9 public void F() {} 10 } 11

shows several uses of the attribute Help. 12

Attribute information for a given program element can be retrieved at run-time by using reflection support. 13 The example 14

using System; 15

class Test 16 { 17 static void Main() { 18 Type type = typeof(Class1); 19 object[] arr = type.GetCustomAttributes( 20 typeof(HelpAttribute), true); 21 if (arr.Length == 0) 22 Console.WriteLine("Class1 has no Help attribute."); 23 else { 24 HelpAttribute ha = (HelpAttribute) arr[0]; 25 Console.WriteLine("Url = {0}, Topic = {1}", ha.Url, ha.Topic); 26 } 27 } 28 } 29

checks to see if Class1 has a Help attribute, and writes out the associated Url and Topic values if the 30 attribute is present. 31

8.16 Generics 32

C# allows classes, structs, interfaces, and methods to be parameterized by the types of data they store and 33 manipulate. This feature is really a set of features known collectively as generics. C# generics will 34 immediately be familiar to users of generics in Eiffel or Ada, or to users of templates in C++. 35

Many common classes and structs can be parameterized by the types of data being stored and manipulated—36 these are called generic class declarations and generic struct declarations, respectively. Similarly, many 37 interfaces define contracts that can be parameterized by the types of data they handle—these are called 38 generic interface declarations. In order to implement “generic algorithms,” methods can also be 39 parameterized by type; such methods are known as generic methods. 40

8.16.1 Why generics? 41

Without generics, programmers can store data of any type in variables of the base type object. To 42 illustrate, let’s create a simple Stack type with two actions, “Push” and “Pop”. The Stack class stores its 43 data in an array of object, and the Push and Pop methods use the object type to accept and return data, 44 respectively: 45

public class Stack 46 { 47 private object[] items = new object[100]; 48 public void Push(object data) {…} 49 public object Pop() {…} 50 } 51

We can then push a value of any type, such as a Customer type, for example, onto the stack. However, 52 when we wanted to retrieve that value, we would need to explicitly cast the result of the Pop method, an 53

Page 73: C# Language Specification

Chapter 8 Language overview

53

object, into a Customer type, which is tedious to write, and carries a performance penalty for run-time 1 type checking: 2

Stack s = new Stack(); 3 s.Push(new Customer()); 4 Customer c = (Customer)s.Pop(); 5

If we pass to the Push method a value type, such as an int, it will automatically be boxed. Similarly, if we 6 want to retrieve an int from the stack, we would need to explicitly unbox the object type we obtain from 7 the Pop method: 8

Stack s = new Stack(); 9 s.Push(3); 10 int i = (int)s.Pop(); 11

Such boxing and unboxing operations can impact performance. 12

Furthermore, in the implementation shown, it is not possible to enforce the kind of data placed in the stack. 13 Indeed, we could create a stack and push a Customer type onto it. However, later, we could use the same 14 stack and try to pop data off of it and cast it into an incompatible type: 15

Stack s = new Stack(); 16 s.Push(new Customer()); 17 Employee e = (Employee)s.Pop(); // runtime error 18

While the code above is an improper use of the Stack class we presumably intended to implement, and 19 should be a compile-time error, it is actually valid code. However, at run-time, the application will fail 20 because we have performed an invalid cast operation. 21

8.16.2 Creating and consuming generics 22

Generics provide a facility for creating high-performance data structures that are specialized by the compiler 23 and/or execution engine based on the types that they use. These so-called generic type declarations are 24 created so that their internal algorithms remain the same, yet the types of their external interface and internal 25 data can vary based on user preference. 26

In order to minimize the learning curve for developers, generics are used in much the same way as C++ 27 templates. Programmers can create classes and structures just as they normally have, and by using the angle 28 bracket notation (< and >) they can specify type parameters. When the generic class declaration is used, each 29 type parameter shall be replaced by a type argument that the user of the class supplies. 30

In the example below, we create a Stack generic class declaration where we specify a type parameter, 31 called ItemType, declared in angle brackets after the declaration. Rather than forcing conversions to and 32 from object, instances of the generic Stack class will accept the type for which they are created and store 33 data of that type without conversion. The type parameter ItemType acts as a placeholder until an actual 34 type is specified at use. Note that ItemType is used as the element type for the internal items array, the type 35 for the parameter to the Push method, and the return type for the Pop method: 36

public class Stack<ItemType> 37 { 38 private ItemType[] items = new ItemType[100]; 39 public void Push(ItemType data) {…} 40 public ItemType Pop() {…} 41 } 42

When we use the generic class declaration Stack, as in the short example below, we can specify the actual 43 type to be used by the generic class. In this case, we instruct the Stack to use an int type by specifying it 44 as a type argument using the angle brackets after the name: 45

Stack<int> s = new Stack<int>(); 46 s.Push(3); 47 int x = s.Pop(); 48

In so doing, we have created a new constructed type, Stack<int>, for which every ItemType inside the 49 declaration of Stack is replaced with the supplied type argument int. Indeed, when we create our new 50 instance of Stack<int>, the native storage of the items array is now an int[] rather than object[], 51

Page 74: C# Language Specification

C# LANGUAGE SPECIFICATION

54

providing substantial storage efficiency. Additionally, we have eliminated the boxing penalty associated 1 with pushing an int onto the stack. Further, when we pop an item off the stack, we no longer need to 2 explicitly cast it to the appropriate type because this particular kind of Stack class natively stores an int in 3 its data structure. 4

If we wanted to store items other than an int into a Stack, we would have to create a different constructed 5 type from Stack, specifying a new type argument. Suppose we had a simple Customer type and we 6 wanted to use a Stack to store it. To do so, we simply use the Customer class as the type argument to 7 Stack and easily reuse our code: 8

Stack<Customer> s = new Stack<Customer>(); 9 s.Push(new Customer()); 10 Customer c = s.Pop(); 11

Of course, once we’ve created a Stack with a Customer type as its type argument, we are now limited to 12 storing only Customer objects (or objects of a class derived from Customer). Generics provide strong 13 typing, meaning we can no longer improperly store an integer into the stack, like so: 14

Stack<Customer> s = new Stack<Customer>(); 15 s.Push(new Customer()); 16 s.Push(3); // compile-time error 17 Customer c = s.Pop(); // no cast required 18

8.16.3 Multiple type parameters 19

Generic type declarations can have any number of type parameters. In our Stack example, we used only 20 one type parameter. Suppose we created a simple Dictionary generic class declaration that stored values 21 alongside keys. We could define a generic version of a Dictionary by declaring two type parameters, 22 separated by commas within the angle brackets of the declaration: 23

public class Dictionary<KeyType, ElementType> 24 { 25 public void Add(KeyType key, ElementType val) {…} 26 public ElementType this[KeyType key] {…} 27 } 28

When we use Dictionary, we need to supply two type arguments within the angle brackets. Then when 29 we call the Add function or use the indexer, the compiler checks that we supplied the right types: 30

Dictionary<string, Customer> dict = new Dictionary<string, Customer>(); 31 dict.Add("Peter", new Customer()); 32 Customer c = dict["Peter"]; 33

8.16.4 Constraints 34

In many cases, we will do more than store data based on a given type parameter. Often, we will also want to 35 use members of the type parameter to execute statements within our generic type declaration. 36

For example, suppose in the Add method of our Dictionary we wanted to compare items using the 37 CompareTo method of the supplied key, like so: 38

public class Dictionary<KeyType, ElementType> 39 { 40 public void Add(KeyType key, ElementType val) 41 { 42 … 43

if (key.CompareTo(x) < 0) {…} // compile-time error 44 … 45 } 46 } 47

Unfortunately, at compile-time the type parameter KeyType is, as expected, generic. As written, the 48 compiler will assume that only the operations available to object, such as ToString, are available on the 49 variable key of type KeyType. As a result, the compiler will display an error because the CompareTo 50 method would not be found. However, we can cast the key variable to a type that does contain a 51 CompareTo method, such as an IComparable interface, allowing the program to compile: 52

Page 75: C# Language Specification

Chapter 8 Language overview

55

public class Dictionary<KeyType, ElementType> 1 { 2 public void Add(KeyType key, ElementType val) 3 { 4 … 5 if (((IComparable)key).CompareTo(x) < 0) {…} 6 … 7 } 8 } 9

However, if we now construct a type from Dictionary and supply a type argument which does not 10 implement IComparable, we will encounter a run-time error, specifically an InvalidCastException. 11 Since one of the objectives of generics is to provide strong typing and to reduce the need for casts, a more 12 elegant solution is needed. 13

We can supply an optional list of constraints for each type parameter. A constraint indicates a requirement 14 that a type shall fulfill in order to be accepted as a type argument. (For example, it might have to implement 15 a given interface or be derived from a given base class.) A constraint is declared using the word where, 16 followed by a type parameter and colon (:), followed by a comma-separated list of constraints, which can 17 include a class type, interface types, other type parameters, the reference type constraint “class”, the value 18 type constraint “struct”, and the constructor constraint “new()”. 19

In order to satisfy our need to use the CompareTo method inside Dictionary, we can impose a constraint 20 on KeyType, requiring any type passed as the first argument to Dictionary to implement IComparable, 21 like so: 22

public class Dictionary<KeyType, ElementType> where KeyType: IComparable 23 { 24 public void Add(KeyType key, ElementType val) 25 { 26 … 27 if (key.CompareTo(x) < 0) {…} 28 … 29 } 30 } 31

When compiled, this code will now be checked to ensure that each time we construct a Dictionary type 32 we are passing a first type argument that implements IComparable. Further, we no longer have to 33 explicitly cast variable key to IComparable before calling the CompareTo method. 34

Constraints are most useful when they are used in the context of defining a framework, i.e. a collection of 35 related classes, where it is advantageous to ensure that a number of types support some common signatures 36 and/or base types. Constraints can be used to help define “generic algorithms” that plug together 37 functionality provided by different types. This can also be achieved by subclassing and runtime 38 polymorphism, but static, constrained polymorphism can, in many cases, result in more efficient code, more 39 flexible specifications of generic algorithms, and more errors being caught at compile-time rather than run-40 time. However, constraints need to be used with care and taste. Types that do not implement the constraints 41 will not easily be usable in conjunction with generic code. 42

For any given type parameter, we can specify any number of interfaces and type parameters as constraints, 43 but no more than one class. Each constrained type parameter has a separate where clause. In the example 44 below, the KeyType type parameter has two interface constraints, while the ElementType type parameter 45 has one class constraint: 46

public class Dictionary<KeyType, ElementType > 47 where KeyType: IComparable, IEnumerable 48 where ElementType: Customer 49 { 50 public void Add(KeyType key, ElementType val) 51 { 52 … 53

Page 76: C# Language Specification

C# LANGUAGE SPECIFICATION

56

if (key.CompareTo(x) < 0) {…} 1 … 2 } 3 } 4

8.16.5 Generic methods 5

In some cases, a type parameter is not needed for an entire class, but only when calling a particular method. 6 Often, this occurs when creating a method that takes a generic type as a parameter. For example, when using 7 the Stack described earlier, we might often find ourselves pushing multiple values in a row onto a stack, 8 and decide to write a method to do so in a single call. If we are only using a single kind of Stack, say 9 Stack<int>, writing such a method is easy: 10

static void PushMultiple(Stack<int> s, params int[] values) { 11 foreach (int v in values) { 12 s.Push(v); 13 } 14 } 15

We can use this method to push multiple int values onto a Stack<int>: 16

Stack<int> s = new Stack<int>(); 17 PushMultiple(s, 1, 2, 3, 4); 18

However, the method above only works with one particular constructed type: Stack<int>. While we can 19 easily write similar code for other constructed Stack types, we would like to write a single method that can 20 work with any Stack, no matter what type argument was used. 21

We do this by writing a generic method. Like a generic class declaration, a generic method is written with 22 type parameters enclosed in angle brackets. With a generic method, the type parameters are written 23 immediately after the method name, and can be used within the parameter list, return type, and body of the 24 method. A generic PushMultiple method would look like this: 25

static void PushMultiple<ItemType>(Stack<ItemType> s, 26 params ItemType[] values) 27 { 28 foreach (ItemType v in values) { 29 s.Push(v); 30 } 31 } 32

Using this generic method, we can now push multiple items onto a Stack of any kind. Furthermore, the 33 compiler type checking will ensure that the pushed items have the correct type for the kind of Stack being 34 used. When calling a generic method, we place type arguments to the method in angle brackets. The generic 35 PushMultiple method can be called this way: 36

Stack<int> s = new Stack<int>(); 37 PushMultiple<int>(s, 1, 2, 3, 4); 38

This generic PushMultiple method is much better than the previous version, since it works on any kind of 39 Stack. However, it appears to be less convenient to call, since the desired ItemType shall be supplied as a 40 type argument to the method. In many cases, however, the compiler can deduce the correct type argument 41 from the other arguments passed to the method, using a process called type inferencing. In the example 42 above, since the first regular argument is of type Stack<int>, and the subsequent arguments are of type 43 int, the compiler can reason that the type parameter shall also be int. Thus, the generic PushMultiple 44 method can be called without specifying the type parameter: 45

Stack<int> s = new Stack<int>(); 46 PushMultiple(s, 1, 2, 3, 4); 47

8.17 Anonymous methods 48

In C# code, a callback method is often invoked strictly through a delegate and not invoked directly. The 49 purpose of such a method is obscured by the necessary separation of the method declaration from the 50 delegate instantiation. In contrast, the body of an anonymous method is written “in-line” where the delegate 51 is used, conveniently tying the method source code to the delegate instance. Besides this convenience, 52

Page 77: C# Language Specification

Chapter 8 Language overview

57

anonymous methods have shared access to the local state of the containing function member. To achieve the 1 same state sharing using named methods requires “lifting” local variables into fields of some object, further 2 obscuring the source code. 3

An anonymous method is declared using an anonymous-method-expression: 4

anonymous-method-expression: 5 delegate anonymous-method-signatureopt block 6

The optional anonymous-method-signature defines the names and types of the formal parameters for the 7 anonymous method. If the anonymous-method-signature is omitted, the block does not use any formal 8 parameters. The block defines the body of the anonymous method. 9

An anonymous-method-expression is classified as a special kind of value that references the anonymous 10 method. This value has no instrinsic type, but is implicitly convertible to any delegate type that has 11 parameter types and a return type compatible with the anonymous method. If an anonymous method is 12 declared without a signature, any delegate parameter types that do not include an out parameter are 13 compatible with the anonymous method. If an anonymous method is declared with a signature, only delegate 14 parameter types that exactly match in type and order are compatible. The return type of a delegate is 15 compatible with an anonymous method if the expressions associated with all return statements in the 16 anonymous method can be implicitly converted to the return type of the delegate. A void delegate return 17 type is compatible with an anonymous method that has no return statements, or only has return 18 statements with no expression. 19

Local variables and value parameters (including this) whose scope contains an anonymous method 20 declaration are called outer variables of the anonymous method. 21

In the absence of anonymous methods, the lifetime of a local variable or value parameter ends when 22 execution of its scope ends, as described in §12.1.7. However, an anonymous method can access an outer 23 variable instance after execution has left the scope of the outer variable. In this case, the lifetime of the outer 24 variable extends until all referencing anonymous method delegates are eligible for garbage collection, as 25 described in §10.9. 26

An anonymous method cannot access ref or out parameters of an outer scope. The reason for this is that 27 the caller of a function member allocates the storage for such parameters, so their lifetimes cannot be 28 extended arbitrarily by the called function member. Consequently, since the this value of an instance 29 method of a struct is equivalent to a ref parameter, an anonymous method in a struct is not allowed to 30 access this. 31

Semantically, an anonymous method contained in a class or struct T is considered to be a method of T. If T is 32 a class type, the anonymous method is considered to be an instance method or static method according to 33 whether the containing function member is instance or static, respectively. In contrast, if T is a struct type, 34 the anonymous method is always considered to be static and, as indicated above, cannot access this. 35

The following code defines an Action delegate type and a Walk method. The Walk method invokes an 36 action sequentially on nodes in a linked list until either the action returns false or the end of the list is 37 encountered. 38

delegate bool Action(Node n); 39

static void Walk(Node n, Action a) { 40 while (n != null && a(n)) n = n.Next; 41 } 42

The following invocation of Walk employs an anonymous method to display the names of the nodes in a 43 list: 44

Walk(list, 45 delegate(Node n) { 46 Console.WriteLine(n.Name); 47 return true; 48 } 49 ); 50

Page 78: C# Language Specification

C# LANGUAGE SPECIFICATION

58

This code could easily be implemented using a named method, but doing so would require separating the 1 method declaration from the invocation, obscuring the purpose of the method and the result of the 2 invocation: 3

Walk(list, new Action(DisplayNodeName)); 4 … 5

bool DisplayNodeName(Node n) { 6 Console.WriteLine(n.Name); 7 return true; 8 } 9

The following code uses an outer local variable c to display the ordinal position of each node: 10

int c = 0; 11

Walk(list, 12 delegate(Node n) { 13 Console.WriteLine("{0}: {1}", ++c, n.Name); 14 return true; 15 } 16 ); 17

Console.WriteLine("Processed {0} nodes", c); 18

Implementing this example using a named method would require “lifting” the outer local variable c into a 19 field, which would further obscure the code, would introduce additional overhead in the type, and could 20 introduce concurrency issues. 21

The following code uses an outer local variable c and an instance field max to restrict the number of items 22 displayed: 23

class A 24 { 25 int max; 26 … 27

void F(Node list) { 28 int c = 0; 29

Walk(list, 30 delegate(Node n) { 31 if (c >= max) { 32 Console.WriteLine("... display truncated"); 33 return false; 34 } 35 Console.WriteLine("{0}: {1}", ++c, n.name); 36 return true; 37 } 38 ); 39

Console.WriteLine("Processed {0} nodes", c); 40 } 41 } 42

Since F is an instance method of class A, the anonymous method is considered to be an instance method 43 of A, and the field max is accessed through the anonymous method’s this variable (which is the same as 44 F’s this variable). That is, max is a field, not an outer variable, and the anonymous method can access max 45 just as any other instance method does. 46

Implementing this example in a safe way (considering the possibility of concurrency) without an anonymous 47 method requires a significant amount of work and decreases clarity: 48

class A 49 { 50 int max; 51 … 52

void F(node list) { 53 NodeNameDisplayer nnd = new NodeNameDisplayer(this); 54 nnd.c = 0; 55

Page 79: C# Language Specification

Chapter 8 Language overview

59

Walk(list, new Action(nnd.DisplayNodeName)); 1

Console.WriteLine("Processed {0} nodes", nnd.c); 2 } 3

private class NodeNameDisplayer 4 { 5 A outer; 6 internal int c; 7

NodeNameDisplayer(A outer) { 8 this.outer = outer; 9 } 10

bool DisplayNodeName(Node n) { 11 if (c >= outer.max) { 12 Console.WriteLine("... display truncated"); 13 return false; 14 } 15 Console.WriteLine("{0}: {1}", ++c, n.Name); 16 return true; 17 } 18 } 19 } 20

8.18 Iterators 21

The foreach statement is used to iterate over the elements of an enumerable collection. In order to be 22 enumerable, a collection shall have a parameterless GetEnumerator method that returns an enumerator. 23 Generally, enumerators are difficult to implement, but the task is significantly simplified with iterators. 24

An iterator is a statement block that yields an ordered sequence of values. An iterator is distinguished from a 25 normal statement block by the presence of one or more yield statements: 26

• The yield return statement produces the next value of the iteration. 27

• The yield break statement indicates that the iteration is complete. 28

An iterator can be used as the body of a function member as long as the return type of the function member 29 is one of the enumerator interfaces or one of the enumerable interfaces: 30

• The enumerator interfaces are System.Collections.IEnumerator and types constructed from 31 System.Collections.Generic.IEnumerator<T>. 32

• The enumerable interfaces are System.Collections.IEnumerable and types constructed from 33 System.Collections.Generic.IEnumerable<T>. 34

It is important to understand that an iterator is not a kind of member, but is a means of implementing a 35 function member. A member implemented via an iterator can be overridden or overloaded by other members 36 which may or may not be implemented with iterators. 37

The following Stack<T> class implements its GetEnumerator method using an iterator, which 38 enumerates the elements of the stack in top-to-bottom order. 39

using System.Collections.Generic; 40

public class Stack<T>: IEnumerable<T> 41 { 42 T[] items; 43 int count; 44

public void Push(T data) {…} 45

public T Pop() {…} 46

public IEnumerator<T> GetEnumerator() { 47 for (int i = count – 1; i >= 0; --i) { 48 yield return items[i]; 49 } 50 } 51 } 52

Page 80: C# Language Specification

C# LANGUAGE SPECIFICATION

60

The presence of the GetEnumerator method makes Stack<T> an enumerable type, allowing instances of 1 Stack<T> to be used in a foreach statement. The following example pushes the values 0 through 9 onto 2 an integer stack and then uses a foreach loop to display the values in top-to-bottom order. 3

using System; 4

class Test 5 { 6 static void Main() { 7 Stack<int> s = new Stack<int>(); 8 for (int i = 0; i < 10; i++) s.Push(i); 9 foreach (int i in s) Console.Write("{0} ", i); 10 Console.WriteLine(); 11 } 12 } 13

The output of the example is: 14

9 8 7 6 5 4 3 2 1 0 15

The foreach statement implicitly calls a collection’s parameterless GetEnumerator method to obtain an 16 enumerator. There can only be one such parameterless GetEnumerator method defined by a collection, yet 17 it is often appropriate to have multiple ways of enumerating, and ways of controlling the enumeration 18 through parameters. In such cases, a collection can use iterators to implement properties or methods that 19 return one of the enumerable interfaces. For example, Stack<T> might introduce two new properties, 20 TopToBottom and BottomToTop, of type IEnumerable<T>: 21

using System.Collections.Generic; 22

public class Stack<T>: IEnumerable<T> 23 { 24 T[] items; 25 int count; 26

public void Push(T data) {…} 27

public T Pop() {…} 28

public IEnumerator<T> GetEnumerator() { 29 for (int i = count – 1; i >= 0; --i) { 30 yield return items[i]; 31 } 32 } 33

public IEnumerable<T> TopToBottom { 34 get { 35 return this; 36 } 37 } 38

public IEnumerable<T> BottomToTop { 39 get { 40 for (int i = 0; i < count; i++) { 41 yield return items[i]; 42 } 43 } 44 } 45 } 46

The get accessor for the TopToBottom property just returns this since the stack itself is an enumerable. 47 The BottomToTop property returns an enumerable implemented with an iterator. The following example 48 shows how the properties can be used to enumerate stack elements in either order: 49

using System; 50

class Test 51 { 52 static void Main() { 53 Stack<int> s = new Stack<int>(); 54 for (int i = 0; i < 10; i++) s.Push(i); 55

Page 81: C# Language Specification

Chapter 8 Language overview

61

foreach (int i in s.TopToBottom) Console.Write("{0} ", i); 1 Console.WriteLine(); 2

foreach (int i in s.BottomToTop) Console.Write("{0} ", i); 3 Console.WriteLine(); 4 } 5 } 6

Of course, these properties can be used outside of a foreach statement as well. The following example 7 passes the results of invoking the properties to a separate Print method. The example also shows an iterator 8 used as the body of a FromToBy method that takes parameters: 9

using System; 10 using System.Collections.Generic; 11

class Test 12 { 13 static void Print(IEnumerable<int> collection) { 14 foreach (int i in collection) Console.Write("{0} ", i); 15 Console.WriteLine(); 16 } 17

static IEnumerable<int> FromToBy(int from, int to, int by) { 18 for (int i = from; i <= to; i += by) { 19 yield return i; 20 } 21 } 22

static void Main() { 23 Stack<int> s = new Stack<int>(); 24 for (int i = 0; i < 10; i++) s.Push(i); 25 Print(s.TopToBottom); 26 Print(s.BottomToTop); 27 Print(FromToBy(10, 20, 2)); 28 } 29 } 30

The output of the example is: 31

9 8 7 6 5 4 3 2 1 0 32 0 1 2 3 4 5 6 7 8 9 33 10 12 14 16 18 20 34

The generic and non-generic enumerable interfaces contain a single member, a GetEnumerator method 35 that takes no arguments and returns an enumerator interface. An enumerable acts as an enumerator factory. 36 Properly implemented enumerables generate independent enumerators each time their GetEnumerator 37 method is called. Assuming the internal state of the enumerable has not changed between two calls to 38 GetEnumerator, the two enumerators returned should produce the same set of values in the same order. 39 This should hold even if the lifetimes of the enumerators overlap as in the following code sample: 40

using System; 41 using System.Collections.Generic; 42

class Test 43 { 44 static IEnumerable<int> FromTo(int from, int to) { 45 while (from <= to) yield return from++; 46 } 47

static void Main() { 48 IEnumerable<int> e = FromTo(1, 10); 49 foreach (int x in e) { 50 foreach (int y in e) { 51 Console.Write("{0,3} ", x * y); 52 } 53 Console.WriteLine(); 54 } 55 } 56 } 57

Page 82: C# Language Specification

C# LANGUAGE SPECIFICATION

62

The code above prints a simple multiplication table of the integers 1 through 10. Note that the FromTo 1 method is invoked only once to generate the enumerable e. However, e.GetEnumerator() is invoked 2 multiple times (by the foreach statements) to generate multiple equivalent enumerators. These enumerators 3 all encapsulate the iterator code specified in the declaration of FromTo. Note that the iterator code modifies 4 the from parameter. Nevertheless, the enumerators act independently because each enumerator is given its 5 own copy of the from and to parameters. The sharing of transient state between enumerators is one of 6 several common subtle flaws that should be avoided when implementing enumerables and enumerators. 7 Iterators are designed to help avoid these problems and to implement robust enumerables and enumerators in 8 a simple intuitive way. 9

End of informative text. 10

Page 83: C# Language Specification

Chapter 9 Lexical structure

63

9. Lexical structure 1

9.1 Programs 2

A C# program consists of one or more source files, known formally as compilation units (§16.1). A source 3 file is an ordered sequence of Unicode characters. Source files typically have a one-to-one correspondence 4 with files in a file system, but this correspondence is not required. 5

Conceptually speaking, a program is compiled using three steps: 6

1. Transformation, which converts a file from a particular character repertoire and encoding scheme into a 7 sequence of Unicode characters. 8

2. Lexical analysis, which translates a stream of Unicode input characters into a stream of tokens. 9

3. Syntactic analysis, which translates the stream of tokens into executable code. 10

Conforming implementations shall accept Unicode source files encoded with the UTF-8 encoding form 11 (as defined by the Unicode standard), and transform them into a sequence of Unicode characters. 12 Implementations can choose to accept and transform additional character encoding schemes (such as 13 UTF-16, UTF-32, or non-Unicode character mappings). 14

[Note: It is beyond the scope of this standard to define how a file using a character representation other 15 than Unicode might be transformed into a sequence of Unicode characters. During such transformation, 16 however, it is recommended that the usual line-separating character (or sequence) in the other character 17 set be translated to the two-character sequence consisting of the Unicode carriage-return character 18 followed by Unicode line-feed character. For the most part this transformation will have no visible 19 effects; however, it will affect the interpretation of verbatim string literal tokens (§9.4.4.5). The purpose 20 of this recommendation is to allow a verbatim string literal to produce the same character sequence 21 when its source file is moved between systems that support differing non-Unicode character sets, in 22 particular, those using differing character sequences for line-separation. end note] 23

9.2 Grammars 24

This specification presents the syntax of the C# programming language using two grammars. The lexical 25 grammar (§9.2.1) defines how Unicode characters are combined to form line terminators, white space, 26 comments, tokens, and pre-processing directives. The syntactic grammar (§9.2.2) defines how the tokens 27 resulting from the lexical grammar are combined to form C# programs. 28

9.2.1 Lexical grammar 29

The lexical grammar of C# is presented in §9.2.3, §9.4, and §9.5. The terminal symbols of the lexical 30 grammar are the characters of the Unicode character set, and the lexical grammar specifies how characters 31 are combined to form tokens (§9.4), white space (§9.3.3), comments (§9.3.2), and pre-processing directives 32 (§9.5). 33

Every source file in a C# program shall conform to the input production of the lexical grammar (§9.2.3). 34

9.2.2 Syntactic grammar 35

The syntactic grammar of C# is presented in the clauses, subclauses, and appendices that follow this 36 subclause. The terminal symbols of the syntactic grammar are the tokens defined by the lexical grammar, 37 and the syntactic grammar specifies how tokens are combined to form C# programs. 38

Every source file in a C# program shall conform to the compilation-unit production (§16.1) of the syntactic 39 grammar. 40

Page 84: C# Language Specification

C# LANGUAGE SPECIFICATION

64

9.2.3 Grammar ambiguities 1

The productions for simple-name (§14.5.2) and member-access (§14.5.4) can give rise to ambiguities in the 2 grammar for expressions. [Example: The statement: 3

F(G<A, B>(7)); 4

could be interpreted as a call to F with two arguments, G < A and B > (7). Alternatively, it could be 5 interpreted as a call to F with one argument, which is a call to a generic method G with two type arguments 6 and one regular argument. end example] 7

If a sequence of tokens can be parsed (in context) as a simple-name (§14.5.2), member-access (§14.5.4), or 8 pointer-member-access (§25.5.2) ending with a type-argument-list (§26.5.1), the token immediately 9 following the closing > token is examined. If it is one of 10

( ) ] : ; , . ? == != 11

then the type-argument-list is retained as part of the simple-name, member-access or pointer-member-access 12 and any other possible parse of the sequence of tokens is discarded. Otherwise, the type-argument-list is not 13 considered to be part of the simple-name, member-access or pointer-member-access, even if there is no other 14 possible parse of the sequence of tokens. [Note: These rules are not applied when parsing a type-argument-15 list in a namespace-or-type-name (§10.8). end note] [Example: The statement: 16

F(G<A, B>(7)); 17

will, according to this rule, be interpreted as a call to F with one argument, which is a call to a generic 18 method G with two type arguments and one regular argument. The statements 19

F(G<A, B>7); 20 F(G<A, B>>7); 21

will each be interpreted as a call to F with two arguments. The statement 22

x = F<A> + y; 23

will be interpreted as a less-than operater, greater-than operator and unary-plus operator, as if the statement 24 had been written x = (F < A) > (+y), instead of as a simple-name with a type-argument-list followed 25 by a binary-plus operator. In the statement 26

x = y is C<T> + z; 27

the tokens C<T> are interpreted as a namespace-or-type-name with a type-argument-list. end example] 28

9.3 Lexical analysis 29

The input production defines the lexical structure of a C# source file. Each source file in a C# program shall 30 conform to this lexical grammar production. 31

input:: 32 input-sectionopt 33

input-section:: 34 input-section-part 35 input-section input-section-part 36

input-section-part:: 37 input-elementsopt new-line 38 pp-directive 39

input-elements:: 40 input-element 41 input-elements input-element 42

input-element:: 43 whitespace 44 comment 45 token 46

Page 85: C# Language Specification

Chapter 9 Lexical structure

65

Five basic elements make up the lexical structure of a C# source file: Line terminators (§9.3.1), white space 1 (§9.3.3), comments (§9.3.2), tokens (§9.4), and pre-processing directives (§9.5). Of these basic elements, 2 only tokens are significant in the syntactic grammar of a C# program (§9.2.2), except in the case of a > token 3 being combined with another token to form a single operator (§9.4.5). 4

The lexical processing of a C# source file consists of reducing the file into a sequence of tokens which 5 becomes the input to the syntactic analysis. Line terminators, white space, and comments can serve to 6 separate tokens, and pre-processing directives can cause sections of the source file to be skipped, but 7 otherwise these lexical elements have no impact on the syntactic structure of a C# program. 8

When several lexical grammar productions match a sequence of characters in a source file, the lexical 9 processing always forms the longest possible lexical element. [Example: The character sequence // is 10 processed as the beginning of a single-line comment because that lexical element is longer than a single / 11 token. end example] 12

9.3.1 Line terminators 13

Line terminators divide the characters of a C# source file into lines. 14

new-line:: 15 Carriage return character (U+000D) 16 Line feed character (U+000A) 17 Carriage return character (U+000D) followed by line feed character (U+000A) 18 Next line character (U+2085) 19 Line separator character (U+2028) 20 Paragraph separator character (U+2029) 21

For compatibility with source code editing tools that add end-of-file markers, and to enable a source file to 22 be viewed as a sequence of properly terminated lines, the following transformations are applied, in order, to 23 every source file in a C# program: 24

• If the last character of the source file is a Control-Z character (U+001A), this character is deleted. 25

• A carriage-return character (U+000D) is added to the end of the source file if that source file is non-26 empty and if the last character of the source file is not a carriage return (U+000D), a line feed (U+000A), 27 a next line character (U+2085), a line separator (U+2028), or a paragraph separator (U+2029). [Note: 28 The additional carriage-return allows a program to end in a pp-directive (§9.5) that does not have a 29 terminating new-line. end note] 30

9.3.2 Comments 31

Two forms of comments are supported: delimited comments and single-line comments. 32

A delimited comment begins with the characters /* and ends with the characters */. Delimited comments 33 can occupy a portion of a line, a single line, or multiple lines. [Example: The example 34

/* Hello, world program 35 This program writes “hello, world” to the console 36 */ 37 class Hello 38 { 39 static void Main() { 40 System.Console.WriteLine("hello, world"); 41 } 42 } 43

includes a delimited comment. end example] 44

A single-line comment begins with the characters // and extends to the end of the line. [Example: The 45 example 46

Page 86: C# Language Specification

C# LANGUAGE SPECIFICATION

66

// Hello, world program 1 // This program writes “hello, world” to the console 2 // 3 class Hello // any name will do for this class 4 { 5 static void Main() { // this method must be named "Main" 6 System.Console.WriteLine("hello, world"); 7 } 8 } 9

shows several single-line comments. end example] 10

comment:: 11 single-line-comment 12 delimited-comment 13

single-line-comment:: 14 // input-charactersopt 15

input-characters:: 16 input-character 17 input-characters input-character 18

input-character:: 19 Any Unicode character except a new-line-character 20

new-line-character:: 21 Carriage return character (U+000D) 22 Line feed character (U+000A) 23 Next line character (U+0085) 24 Line separator character (U+2028) 25 Paragraph separator character (U+2029) 26

delimited-comment:: 27 /* delimited-comment-textopt asterisks / 28

delimited-comment-text:: 29 delimited-comment-section 30 delimited-comment-text delimited-comment-section 31

delimited-comment-section:: 32 not-asterisk 33 asterisks not-slash 34

asterisks:: 35 * 36 asterisks * 37

not-asterisk:: 38 Any Unicode character except * 39

not-slash:: 40 Any Unicode character except / 41

Comments do not nest. The character sequences /* and */ have no special meaning within a single-line 42 comment, and the character sequences // and /* have no special meaning within a delimited comment. 43

Comments are not processed within character and string literals. 44

9.3.3 White space 45

White space is defined as any character with Unicode class Zs (which includes the space character) as well 46 as the horizontal tab character, the vertical tab character, and the form feed character. 47

Page 87: C# Language Specification

Chapter 9 Lexical structure

67

whitespace:: 1 whitespace-characters 2

whitespace-characters:: 3 whitespace-character 4 whitespace-characters whitespace-character 5

whitespace-character:: 6 Any character with Unicode class Zs 7 Horizontal tab character (U+0009) 8 Vertical tab character (U+000B) 9 Form feed character (U+000C) 10

9.4 Tokens 11

There are several kinds of tokens: identifiers, keywords, literals, operators, and punctuators. White space 12 and comments are not tokens, though they act as separators for tokens. 13

token:: 14 identifier 15 keyword 16 integer-literal 17 real-literal 18 character-literal 19 string-literal 20 operator-or-punctuator 21

9.4.1 Unicode escape sequences 22

A Unicode escape sequence represents a Unicode character. Unicode escape sequences are processed in 23 identifiers (§9.4.2), regular string literals (§9.4.4.5), and character literals (§9.4.4.4). A Unicode character 24 escape is not processed in any other location (for example, to form an operator, punctuator, or keyword). 25

unicode-escape-sequence:: 26 \u hex-digit hex-digit hex-digit hex-digit 27 \U hex-digit hex-digit hex-digit hex-digit hex-digit hex-digit hex-digit hex-digit 28

A Unicode escape sequence represents the single Unicode character formed by the hexadecimal number 29 following the “\u” or “\U” characters. Since C# uses a 16-bit encoding of Unicode characters in characters 30 and string values, a Unicode code point in the range U+10000 to U+10FFFF is represented using two 31 Unicode surrogate code units. Unicode code points above 0x10FFFF are invalid and are not supported. 32

Multiple translations are not performed. For instance, the string literal “\u005Cu005C” is equivalent to 33 “\u005C” rather than “\”. [Note: The Unicode value \u005C is the character “\”. end note] 34

[Example: The example 35

class Class1 36 { 37 static void Test(bool \u0066) { 38 char c = '\u0066'; 39 if (\u0066) 40 System.Console.WriteLine(c.ToString()); 41 } 42 } 43

shows several uses of \u0066, which is the escape sequence for the letter “f”. The program is equivalent to 44

Page 88: C# Language Specification

C# LANGUAGE SPECIFICATION

68

class Class1 1 { 2 static void Test(bool f) { 3 char c = 'f'; 4 if (f) 5 System.Console.WriteLine(c.ToString()); 6 } 7 } 8

end example] 9

9.4.2 Identifiers 10

The rules for identifiers given in this subclause correspond exactly to those recommended by the Unicode 11 Standard Annex 15 except that underscore is allowed as an initial character (as is traditional in the 12 C programming language), Unicode escape sequences are permitted in identifiers, and the “@” character is 13 allowed as a prefix to enable keywords to be used as identifiers. 14

identifier:: 15 available-identifier 16 @ identifier-or-keyword 17

available-identifier:: 18 An identifier-or-keyword that is not a keyword 19

identifier-or-keyword:: 20 identifier-start-character identifier-part-charactersopt 21

identifier-start-character:: 22 letter-character 23 _ (the underscore character U+005F) 24

identifier-part-characters:: 25 identifier-part-character 26 identifier-part-characters identifier-part-character 27

identifier-part-character:: 28 letter-character 29 decimal-digit-character 30 connecting-character 31 combining-character 32 formatting-character 33

letter-character:: 34 A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl 35 A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl 36

combining-character:: 37 A Unicode character of classes Mn or Mc 38 A unicode-escape-sequence representing a character of classes Mn or Mc 39

decimal-digit-character:: 40 A Unicode character of the class Nd 41 A unicode-escape-sequence representing a character of the class Nd 42

connecting-character:: 43 A Unicode character of the class Pc 44 A unicode-escape-sequence representing a character of the class Pc 45

formatting-character:: 46 A Unicode character of the class Cf 47 A unicode-escape-sequence representing a character of the class Cf 48

Page 89: C# Language Specification

Chapter 9 Lexical structure

69

[Note: For information on the Unicode character classes mentioned above, see The Unicode Standard, 1 Verson 3.0, §4.5. end note] 2

[Example: Examples of valid identifiers include “identifier1”, “_identifier2”, and “@if”. end 3 example] 4

An identifier in a conforming program shall be in the canonical format defined by Unicode Normalization 5 Form C, as defined by Unicode Standard Annex 15. The behavior when encountering an identifier not in 6 Normalization Form C is implementation-defined; however, a diagnostic is not required. 7

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other 8 programming languages. The character @ is not actually part of the identifier, so the identifier might be seen 9 in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a 10 verbatim identifier. [Note: Use of the @ prefix for identifiers that are not keywords is permitted, but strongly 11 discouraged as a matter of style. end note] 12

[Example: The example: 13

class @class 14 { 15 public static void @static(bool @bool) { 16 if (@bool) 17 System.Console.WriteLine("true"); 18 else 19 System.Console.WriteLine("false"); 20 } 21 } 22

class Class1 23 { 24 static void M() { 25 cl\u0061ss.st\u0061tic(true); 26 } 27 } 28

defines a class named “class” with a static method named “static” that takes a parameter named 29 “bool”. Note that since Unicode escapes are not permitted in keywords, the token “cl\u0061ss” is an 30 identifier, and is the same identifier as “@class”. end example] 31

Two identifiers are considered the same if they are identical after the following transformations are applied, 32 in order: 33

• The prefix “@”, if used, is removed. 34

• Each unicode-escape-sequence is transformed into its corresponding Unicode character. 35

• Any formatting-characters are removed. 36

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the 37 implementation; however, no diagnostic is required if such an identifier is defined. [Note: For example, an 38 implementation might provide extended keywords that begin with two underscores. end note] 39

9.4.3 Keywords 40

A keyword is an identifier-like sequence of characters that is reserved, and cannot be used as an identifier 41 except when prefaced by the @ character. 42

Page 90: C# Language Specification

C# LANGUAGE SPECIFICATION

70

keyword:: one of 1 abstract as base bool break 2 byte case catch char checked 3 class const continue decimal default 4 delegate do double else enum 5 event explicit extern false finally 6 fixed float for foreach goto 7 if implicit in int interface 8 internal is lock long namespace 9 new null object operator out 10 override params private protected public 11 readonly ref return sbyte sealed 12 short sizeof stackalloc static string 13 struct switch this throw true 14 try typeof uint ulong unchecked 15 unsafe ushort using virtual void 16 volatile while 17

The following identifiers have special meaning in the syntactic grammar, but they are not keywords: add 18 (§17.7), get (§17.6.2), global (§16.7), partial (§17.1.4), remove (§17.7), set (§17.6.2), value 19 (§17.6.2, §17.7.2), where (§26.7), yield (§15.14), and alias (§16.3). For convenience and clarity, these 20 identifiers appear as terminals in the syntactic grammar; however, they are identifiers. [Note: As a result, 21 unlike keywords, these identifiers can be written with a @ prefix and can contain unicode-escape-sequences. 22 end note] 23

9.4.4 Literals 24

A literal is a source code representation of a value. 25

literal:: 26 boolean-literal 27 integer-literal 28 real-literal 29 character-literal 30 string-literal 31 null-literal 32

9.4.4.1 Boolean literals 33

There are two boolean literal values: true and false. 34

boolean-literal:: 35 true 36 false 37

The type of a boolean-literal is bool. 38

9.4.4.2 Integer literals 39

Integer literals are used to write values of types int, uint, long, and ulong. Integer literals have two 40 possible forms: decimal and hexadecimal. 41

integer-literal:: 42 decimal-integer-literal 43 hexadecimal-integer-literal 44

decimal-integer-literal:: 45 decimal-digits integer-type-suffixopt 46

Page 91: C# Language Specification

Chapter 9 Lexical structure

71

decimal-digits:: 1 decimal-digit 2 decimal-digits decimal-digit 3

decimal-digit:: one of 4 0 1 2 3 4 5 6 7 8 9 5

integer-type-suffix:: one of 6 U u L l UL Ul uL ul LU Lu lU lu 7

hexadecimal-integer-literal:: 8 0x hex-digits integer-type-suffixopt 9 0X hex-digits integer-type-suffixopt 10

hex-digits:: 11 hex-digit 12 hex-digits hex-digit 13

hex-digit:: one of 14 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f 15

The type of an integer literal is determined as follows: 16

• If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, 17 long, ulong. 18

• If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: 19 uint, ulong. 20

• If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: 21 long, ulong. 22

• If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong. 23

If the value represented by an integer literal is outside the range of the ulong type, a compile-time error 24 occurs. 25

[Note: As a matter of style, it is suggested that “L” be used instead of “l” when writing literals of type long, 26 since it is easy to confuse the letter “l” with the digit “1”. end note] 27

To permit the smallest possible int and long values to be written as decimal integer literals, the following 28 two rules exist: 29

• When a decimal-integer-literal with the value 2147483648 (231) and no integer-type-suffix appears as 30 the token immediately following a unary minus operator token (§14.6.2), the result (of both tokens) is a 31 constant of type int with the value −2147483648 (−231). In all other situations, such a decimal-integer-32 literal is of type uint. 33

• When a decimal-integer-literal with the value 9223372036854775808 (263) and no integer-type-suffix or 34 the integer-type-suffix L or l appears as the token immediately following a unary minus operator token 35 (§14.6.2), the result (of both tokens) is a constant of type long with the value −9223372036854775808 36 (−263). In all other situations, such a decimal-integer-literal is of type ulong. 37

9.4.4.3 Real literals 38

Real literals are used to write values of types float, double, and decimal. 39

real-literal:: 40 decimal-digits . decimal-digits exponent-partopt real-type-suffixopt 41 . decimal-digits exponent-partopt real-type-suffixopt 42 decimal-digits exponent-part real-type-suffixopt 43 decimal-digits real-type-suffix 44

Page 92: C# Language Specification

C# LANGUAGE SPECIFICATION

72

exponent-part:: 1 e signopt decimal-digits 2 E signopt decimal-digits 3

sign:: one of 4 + - 5

real-type-suffix:: one of 6 F f D d M m 7

If no real-type-suffix is specified, the type of the real literal is double. Otherwise, the real-type-suffix 8 determines the type of the real literal, as follows: 9

• A real literal suffixed by F or f is of type float. [Example: The literals 1f, 1.5f, 1e10f, and 10 123.456F are all of type float. end example] 11

• A real literal suffixed by D or d is of type double. [Example: The literals 1d, 1.5d, 1e10d, and 12 123.456D are all of type double. end example] 13

• A real literal suffixed by M or m is of type decimal. [Example: The literals 1m, 1.5m, 1e10m, and 14 123.456M are all of type decimal. end example] This literal is converted to a decimal value by 15 taking the exact value, and, if necessary, rounding to the nearest representable value using banker's 16 rounding (§11.1.7). Any scale apparent in the literal is preserved unless the value is rounded. [Note: 17 Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3. 18 end note] 19

If the specified literal is too large to be represented in the indicated type, a compile-time error occurs. [Note: 20 In particular, a real-literal will never produce a floating-point infinity. A non-zero real-literal may, 21 however, be rounded to zero. end note] 22

The value of a real literal having type float or double is determined by using the IEC 60559 “round to 23 nearest” mode. 24

9.4.4.4 Character literals 25

A character literal represents a single character, and usually consists of a character in quotes, as in 'a'. 26

character-literal:: 27 ' character ' 28

character:: 29 single-character 30 simple-escape-sequence 31 hexadecimal-escape-sequence 32 unicode-escape-sequence 33

single-character:: 34 Any character except ' (U+0027), \ (U+005C), and new-line-character 35

simple-escape-sequence:: one of 36 \' \" \\ \0 \a \b \f \n \r \t \v 37

hexadecimal-escape-sequence:: 38 \x hex-digit hex-digitopt hex-digitopt hex-digitopt 39

[Note: A character that follows a backslash character (\) in a character shall be one of the following 40 characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs. end note] 41

A hexadecimal escape sequence represents a single Unicode character, with the value formed by the 42 hexadecimal number following “\x”. 43

If the value represented by a character literal is greater than U+FFFF, a compile-time error occurs. 44

A Unicode character escape sequence (§9.4.1) in a character literal shall be in the range U+0000 to U+FFFF. 45

Page 93: C# Language Specification

Chapter 9 Lexical structure

73

A simple escape sequence represents a Unicode character encoding, as described in the table below. 1

2

Escape sequence

Character name

Unicode code point

\' Single quote 0x0027

\" Double quote 0x0022

\\ Backslash 0x005C

\0 Null 0x0000

\a Alert 0x0007

\b Backspace 0x0008

\f Form feed 0x000C

\n New line 0x000A

\r Carriage return 0x000D

\t Horizontal tab 0x0009

\v Vertical tab 0x000B

3 The type of a character-literal is char. 4

9.4.4.5 String literals 5

C# supports two forms of string literals: regular string literals and verbatim string literals. A regular string 6 literal consists of zero or more characters enclosed in double quotes, as in "hello, world", and can 7 include both simple escape sequences (such as \t for the tab character), and hexadecimal and Unicode 8 escape sequences. 9

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more 10 characters, and a closing double-quote character. [Example: A simple example is @"hello, world". end 11 example] In a verbatim string literal, the characters between the delimiters are interpreted verbatim, with the 12 only exception being a quote-escape-sequence. In particular, simple escape sequences, and hexadecimal and 13 Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal can span 14 multiple lines. 15

string-literal:: 16 regular-string-literal 17 verbatim-string-literal 18

regular-string-literal:: 19 " regular-string-literal-charactersopt " 20

regular-string-literal-characters:: 21 regular-string-literal-character 22 regular-string-literal-characters regular-string-literal-character 23

regular-string-literal-character:: 24 single-regular-string-literal-character 25 simple-escape-sequence 26 hexadecimal-escape-sequence 27 unicode-escape-sequence 28

single-regular-string-literal-character:: 29 Any character except " (U+0022), \ (U+005C), and new-line-character 30

Page 94: C# Language Specification

C# LANGUAGE SPECIFICATION

74

verbatim-string-literal:: 1 @" verbatim-string-literal-charactersopt " 2

verbatim-string-literal-characters:: 3 verbatim-string-literal-character 4 verbatim-string-literal-characters verbatim-string-literal-character 5

verbatim-string-literal-character:: 6 single-verbatim-string-literal-character 7 quote-escape-sequence 8

single-verbatim-string-literal-character:: 9 Any character except " 10

quote-escape-sequence:: 11 "" 12

[Note: A character that follows a backslash character (\) in a regular-string-literal-character shall be one of 13 the following characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs. end 14 note] 15

[Example: The example 16

string a = "Happy birthday, Joel"; // Happy birthday, Joel 17 string b = @"Happy birthday, Joel"; // Happy birthday, Joel 18

string c = "hello \t world"; // hello world 19 string d = @"hello \t world"; // hello \t world 20

string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me 21 string f = @"Joe said ""Hello"" to me"; // Joe said "Hello" to me 22

string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt 23 string h = @"\\server\share\file.txt"; // \\server\share\file.txt 24

string i = "one\r\ntwo\r\nthree"; 25 string j = @"one 26 two 27 three"; 28

shows a variety of string literals. The last string literal, j, is a verbatim string literal that spans multiple lines. 29 The characters between the quotation marks, including white space such as new line characters, are 30 preserved verbatim. end example] 31

[Note: Since a hexadecimal escape sequence can have a variable number of hex digits, the string literal 32 "\x123" contains a single character with hex value 123. To create a string containing the character with hex 33 value 12 followed by the character 3, one could write "\x00123" or "\x12" + "3" instead. end note] 34

The type of a string-literal is string. 35

Each string literal does not necessarily result in a new string instance. When two or more string literals that 36 are equivalent according to the string equality operator (§14.9.7), appear in the same assembly, these string 37 literals refer to the same string instance. [Example: For instance, the output produced by 38

class Test 39 { 40 static void Main() { 41 object a = "hello"; 42 object b = "hello"; 43 System.Console.WriteLine(a == b); 44 } 45 } 46

is True because the two literals refer to the same string instance. end example] 47

9.4.4.6 The null literal 48

null-literal:: 49 null 50

Page 95: C# Language Specification

Chapter 9 Lexical structure

75

The type of a null-literal is the null type. 1

9.4.5 Operators and punctuators 2

There are several kinds of operators and punctuators. Operators are used in expressions to describe 3 operations involving one or more operands. [Example: The expression a + b uses the + operator to add the 4 two operands a and b. end example] Punctuators are for grouping and separating. 5

operator-or-punctuator:: one of 6 { } [ ] ( ) . , : ; 7 + - * / % & | ^ ! ~ 8 = < > ? ++ -- && || == -> 9 != <= >= += -= *= /= %= &= |= 10 ^= << <<= :: 11

right-shift: 12 > > 13

right-shift-assignment: 14 > >= 15

right-shift is made up of the two tokens > and >. Similarly, right-shift-assignment is made up of the two 16 tokens > and >=. Unlike other productions in the syntactic grammar, no characters of any kind (not even 17 whitespace) are allowed between the two tokens in each of these productions. [Note: Prior to the addition of 18 generics to C#, >> and >>= were both single tokens. However, the syntax for generics uses the < and > 19 characters to delimit type parameters and type arguments. It is often desirable to use nested constructed 20 types, such as List<Dictionary<string, int>>. Rather than requiring the programmer to separate the 21 > and > by a space, the definition of the two operator-or-punctuators was changed. end note] 22

9.5 Pre-processing directives 23

The pre-processing directives provide the ability to conditionally skip sections of source files, to report error 24 and warning conditions, and to delineate distinct regions of source code. [Note: The term “pre-processing 25 directives” is used only for consistency with the C and C++ programming languages. In C#, there is no 26 separate pre-processing step; pre-processing directives are processed as part of the lexical analysis phase. 27 end note] 28

pp-directive:: 29 pp-declaration 30 pp-conditional 31 pp-line 32 pp-diagnostic 33 pp-region 34 pp-pragma 35

The following pre-processing directives are available: 36

• #define and #undef, which are used to define and undefine, respectively, conditional compilation 37 symbols (§9.5.3). 38

• #if, #elif, #else, and #endif, which are used to conditionally skip sections of source code (§9.5.1). 39

• #line, which is used to control line numbers emitted for errors and warnings (§9.5.7). 40

• #error and #warning, which are used to issue errors and warnings, respectively (§9.5.5). 41

• #region and #endregion, which are used to explicitly mark sections of source code (§9.5.6). 42

• #pragma, which is used to provide contextual information to a compiler (§9.5.8). 43

Page 96: C# Language Specification

C# LANGUAGE SPECIFICATION

76

A pre-processing directive always occupies a separate line of source code and always begins with a 1 # character and a pre-processing directive name. White space can occur before the # character and between 2 the # character and the directive name. 3

A source line containing a #define, #undef, #if, #elif, #else, #endif, or #line directive can end 4 with a single-line comment. Delimited comments (the /* */ style of comments) are not permitted on source 5 lines containing pre-processing directives. 6

Pre-processing directives are not tokens and are not part of the syntactic grammar of C#. However, pre-7 processing directives can be used to include or exclude sequences of tokens and can in that way affect the 8 meaning of a C# program. [Example: When compiled, the program 9

#define A 10 #undef B 11

class C 12 { 13 #if A 14 void F() {} 15 #else 16 void G() {} 17 #endif 18

#if B 19 void H() {} 20 #else 21 void I() {} 22 #endif 23 } 24

results in the exact same sequence of tokens as the program 25

class C 26 { 27 void F() {} 28 void I() {} 29 } 30

Thus, whereas lexically, the two programs are quite different, syntactically, they are identical. end example] 31

9.5.1 Conditional compilation symbols 32

The conditional compilation functionality provided by the #if, #elif, #else, and #endif directives is 33 controlled through pre-processing expressions (§9.5.2) and conditional compilation symbols. 34

conditional-symbol:: 35 Any identifier-or-keyword except true or false 36

A conditional compilation symbol has two possible states: defined or undefined. At the beginning of the 37 lexical processing of a source file, a conditional compilation symbol is undefined unless it has been 38 explicitly defined by an external mechanism (such as a command-line compiler option). When a #define 39 directive is processed, the conditional compilation symbol named in that directive becomes defined in that 40 source file. The symbol remains defined until an #undef directive for that same symbol is processed, or 41 until the end of the source file is reached. An implication of this is that #define and #undef directives in 42 one source file have no effect on other source files in the same program. 43

The name space for conditional compilation symbols is distinct and separate from all other named entities in 44 a C# program. Conditional compilation symbols can only be referenced in #define and #undef directives 45 and in pre-processing expressions. 46

9.5.2 Pre-processing expressions 47

Pre-processing expressions can occur in #if and #elif directives. The operators !, ==, !=, && and || are 48 permitted in pre-processing expressions, and parentheses can be used for grouping. 49

Page 97: C# Language Specification

Chapter 9 Lexical structure

77

pp-expression:: 1 whitespaceopt pp-or-expression whitespaceopt 2

pp-or-expression:: 3 pp-and-expression 4 pp-or-expression whitespaceopt || whitespaceopt pp-and-expression 5

pp-and-expression:: 6 pp-equality-expression 7 pp-and-expression whitespaceopt && whitespaceopt pp-equality-expression 8

pp-equality-expression:: 9 pp-unary-expression 10 pp-equality-expression whitespaceopt == whitespaceopt pp-unary-expression 11 pp-equality-expression whitespaceopt != whitespaceopt pp-unary-expression 12

pp-unary-expression:: 13 pp-primary-expression 14 ! whitespaceopt pp-unary-expression 15

pp-primary-expression:: 16 true 17 false 18 conditional-symbol 19 ( whitespaceopt pp-expression whitespaceopt ) 20

When referenced in a pre-processing expression, a defined conditional compilation symbol has the boolean 21 value true, and an undefined conditional compilation symbol has the boolean value false. 22

Evaluation of a pre-processing expression always yields a boolean value. The rules of evaluation for a pre-23 processing expression are the same as those for a constant expression (§14.15), except that the only user-24 defined entities that can be referenced are conditional compilation symbols. 25

9.5.3 Declaration directives 26

The declaration directives are used to define or undefine conditional compilation symbols. 27

pp-declaration:: 28 whitespaceopt # whitespaceopt define whitespace conditional-symbol pp-new-line 29 whitespaceopt # whitespaceopt undef whitespace conditional-symbol pp-new-line 30

pp-new-line:: 31 whitespaceopt single-line-commentopt new-line 32

The processing of a #define directive causes the given conditional compilation symbol to become defined, 33 starting with the source line that follows the directive. Likewise, the processing of an #undef directive 34 causes the given conditional compilation symbol to become undefined, starting with the source line that 35 follows the directive. 36

Any #define and #undef directives in a source file shall occur before the first token (§9.4) in the source 37 file; otherwise a compile-time error occurs. In intuitive terms, #define and #undef directives shall 38 precede any “real code” in the source file. 39

[Example: The example: 40

#define Enterprise 41

#if Professional || Enterprise 42 #define Advanced 43 #endif 44

Page 98: C# Language Specification

C# LANGUAGE SPECIFICATION

78

namespace Megacorp.Data 1 { 2 #if Advanced 3 class PivotTable {...} 4 #endif 5 } 6

is valid because the #define directives precede the first token (the namespace keyword) in the source file. 7

end example] 8

[Example: The following example results in a compile-time error because a #define follows real code: 9

#define A 10 namespace N 11 { 12 #define B 13 #if B 14 class Class1 {} 15 #endif 16 } 17

end example] 18

A #define can define a conditional compilation symbol that is already defined, without there being any 19 intervening #undef for that symbol. [Example: The example below defines a conditional compilation 20 symbol A and then defines it again. 21

#define A 22 #define A 23

For compilers that allow conditional compilation symbols to be defined as compilation options, an 24 alternative way for such redefinition to occur is to define the symbol as a compiler option as well as in the 25 source. end example] 26

A #undef can “undefine” a conditional compilation symbol that is not defined. [Example: The example 27 below defines a conditional compilation symbol A and then undefines it twice; although the second #undef 28 has no effect, it is still valid. 29

#define A 30 #undef A 31 #undef A 32

end example] 33

9.5.4 Conditional compilation directives 34

The conditional compilation directives are used to conditionally include or exclude portions of a source file. 35

pp-conditional:: 36 pp-if-section pp-elif-sectionsopt pp-else-sectionopt pp-endif 37

pp-if-section:: 38 whitespaceopt # whitespaceopt if whitespace pp-expression pp-new-line 39 conditional-sectionopt 40

pp-elif-sections:: 41 pp-elif-section 42 pp-elif-sections pp-elif-section 43

pp-elif-section:: 44 whitespaceopt # whitespaceopt elif whitespace pp-expression pp-new-line 45 conditional-sectionopt 46

pp-else-section:: 47 whitespaceopt # whitespaceopt else pp-new-line conditional-sectionopt 48

Page 99: C# Language Specification

Chapter 9 Lexical structure

79

pp-endif:: 1 whitespaceopt # whitespaceopt endif pp-new-line 2

conditional-section:: 3 input-section 4 skipped-section 5

skipped-section:: 6 skipped-section-part 7 skipped-section skipped-section-part 8

skipped-section-part:: 9 whitespaceopt skipped-charactersopt new-line 10 pp-directive 11

skipped-characters:: 12 not-number-sign input-charactersopt 13

not-number-sign:: 14 Any input-character except # 15

[Note: As indicated by the syntax, conditional compilation directives shall be written as sets consisting of, in 16 order, an #if directive, zero or more #elif directives, zero or one #else directive, and an #endif 17 directive. Between the directives are conditional sections of source code. Each section is controlled by the 18 immediately preceding directive. A conditional section can itself contain nested conditional compilation 19 directives provided these directives form complete sets. end note] 20

A pp-conditional selects at most one of the contained conditional-sections for normal lexical processing: 21

• The pp-expressions of the #if and #elif directives are evaluated in order until one yields true. If an 22 expression yields true, the conditional-section of the corresponding directive is selected. 23

• If all pp-expressions yield false, and if an #else directive is present, the conditional-section of the 24 #else directive is selected. 25

• Otherwise, no conditional-section is selected. 26

The selected conditional-section, if any, is processed as a normal input-section: the source code contained in 27 the section shall adhere to the lexical grammar; tokens are generated from the source code in the section; and 28 pre-processing directives in the section have the prescribed effects. 29

The remaining conditional-sections, if any, are processed as skipped-sections: except for pre-processing 30 directives, the source code in the section need not adhere to the lexical grammar; no tokens are generated 31 from the source code in the section; and pre-processing directives in the section shall be lexically correct but 32 are not otherwise processed. Within a conditional-section that is being processed as a skipped-section, any 33 nested conditional-sections (contained in nested #if...#endif and #region...#endregion constructs) are 34 also processed as skipped-sections. 35

[Example: The following example illustrates how conditional compilation directives can nest: 36

#define Debug // Debugging on 37 #undef Trace // Tracing off 38

class PurchaseTransaction 39 { 40 void Commit() { 41 #if Debug 42 CheckConsistency(); 43 #if Trace 44 WriteToLog(this.ToString()); 45 #endif 46 #endif 47 CommitHelper(); 48 } 49 … 50 } 51

Page 100: C# Language Specification

C# LANGUAGE SPECIFICATION

80

Except for pre-processing directives, skipped source code is not subject to lexical analysis. For example, the 1 following is valid despite the unterminated comment in the #else section: 2

#define Debug // Debugging on 3

class PurchaseTransaction 4 { 5 void Commit() { 6 #if Debug 7 CheckConsistency(); 8 #else 9 /* Do something else 10 #endif 11 } 12 … 13 } 14

Note, however, that pre-processing directives are required to be lexically correct even in skipped sections of 15 source code. 16

Pre-processing directives are not processed when they appear inside multi-line input elements. For example, 17 the program: 18

class Hello 19 { 20 static void Main() { 21 System.Console.WriteLine(@"hello, 22 #if Debug 23 world 24 #else 25 Nebraska 26 #endif 27 "); 28 } 29 } 30

results in the output: 31

hello, 32 #if Debug 33 world 34 #else 35 Nebraska 36 #endif 37

In peculiar cases, the set of pre-processing directives that is processed might depend on the evaluation of the 38 pp-expression. The example: 39

#if X 40 /* 41 #else 42 /* */ class Q { } 43 #endif 44

always produces the same token stream (class Q { }), regardless of whether or not X is defined. If X is 45 defined, the only processed directives are #if and #endif, due to the multi-line comment. If X is 46 undefined, then three directives (#if, #else, #endif) are part of the directive set. end example] 47

9.5.5 Diagnostic directives 48

The diagnostic directives are used to explicitly generate error and warning messages that are reported in the 49 same way as other compile-time errors and warnings. 50

pp-diagnostic:: 51 whitespaceopt # whitespaceopt error pp-message 52 whitespaceopt # whitespaceopt warning pp-message 53

Page 101: C# Language Specification

Chapter 9 Lexical structure

81

pp-message:: 1 new-line 2 whitespace input-charactersopt new-line 3

[Example: The example 4

#warning Code review needed before check-in 5

#if Debug && Retail 6 #error A build can't be both debug and retail 7 #endif 8

class Test {…} 9

always produces a warning (“Code review needed before check-in”), and produces a compile-time 10 error if the pre-processing identifiers Debug and Retail are both defined. Note that a pp-message can 11 contain arbitrary text; specifically, it need not contain well-formed tokens, as shown by the single quote in 12 the word can't. end example] 13

9.5.6 Region control 14

The region directives are used to explicitly mark regions of source code. 15

pp-region:: 16 pp-start-region conditional-sectionopt pp-end-region 17

pp-start-region:: 18 whitespaceopt # whitespaceopt region pp-message 19

pp-end-region:: 20 whitespaceopt # whitespaceopt endregion pp-message 21

No semantic meaning is attached to a region; regions are intended for use by the programmer or by 22 automated tools to mark a section of source code. The message specified in a #region or #endregion 23 directive likewise has no semantic meaning; it merely serves to identify the region. Matching #region and 24 #endregion directives can have different pp-messages. 25

The lexical processing of a region: 26

#region 27 ... 28 #endregion 29

corresponds exactly to the lexical processing of a conditional compilation directive of the form: 30

#if true 31 ... 32 #endif 33

9.5.7 Line directives 34

Line directives can be used to alter the line numbers and source file names that are reported by the compiler 35 in output such as warnings and errors. 36

[Note: Line directives are most commonly used in meta-programming tools that generate C# source code 37 from some other text input. end note] 38

pp-line:: 39 whitespaceopt # whitespaceopt line whitespace line-indicator pp-new-line 40

line-indicator:: 41 decimal-digits whitespace file-name 42 decimal-digits 43 identifier-or-keyword 44

file-name:: 45 " file-name-characters " 46

Page 102: C# Language Specification

C# LANGUAGE SPECIFICATION

82

file-name-characters:: 1 file-name-character 2 file-name-characters file-name-character 3

file-name-character:: 4 Any character except " (U+0022), and new-line-character 5

When no #line directives are present, the compiler reports true line numbers and source file names in its 6 output. When processing a #line directive that includes a line-indicator that is not identifier-or-keyword, 7 the compiler treats the line after the directive as having the given line number (and file name, if specified). 8

A #line directive in which the line-indicator is an identifier-or-keyword whose value equals default 9 (using equality as specified in §9.4.2) reverses the effect of all preceding #line directives. The compiler 10 reports true line information for subsequent lines, precisely as if no #line directives had been processed. 11

The purpose of a line-indicator with an identifier-or-keyword whose value does not equal default is 12 implementation-defined. An implementation that does not recognize such an identifier-or-keyword in a line-13 indicator shall issue a warning. 14

[Note: Note that a file-name differs from a regular string literal in that escape characters are not processed; 15 the ‘\’ character simply designates an ordinary back-slash character within a file-name. end note] 16

9.5.8 Pragma directives 17

The #pragma directive is a preprocessing directive used to specify contextual information to a compiler. 18 [Note: For example, a compiler might provide #pragma directives that 19

• Enable or disable particular warning messages when compiling subsequent code. 20

• Specify which optimizations to apply to subsequent code. 21

• Specify information to be used by a debugger. 22

end note] 23

pp-pragma: 24 whitespaceopt # whitespaceopt pragma pp-pragma-text 25

pp-pragma-text: 26 new-line 27 whitespace input-charactersopt new-line 28

The input-characters in the pp-pragma-text are interpreted by the compiler in an implementation-defined 29 manner. The information supplied in a #pragma directive shall not change program semantics. A #pragma 30 directive shall only change compiler behavior which is outside the scope of this language specification. If 31 the compiler cannot interpret the input-characters, the compiler can produce a warning; however, it shall not 32 produce a compile-time error. 33

[Note: pp-pragma-text can contain arbitrary text; specifically, it need not contain well-formed tokens. end 34 note] 35

Page 103: C# Language Specification

Chapter 10 Basic concepts

83

10. Basic concepts 1

10.1 Application startup 2

Application startup occurs when the execution environment calls a designated method, which is referred to 3 as the application's entry point. This entry point method is always named Main, and shall have one of the 4 following signatures: 5

static void Main() {…} 6 static void Main(string[] args) {…} 7 static int Main() {…} 8 static int Main(string[] args) {…} 9

As shown, the entry point can optionally return an int value. This return value is used in application 10 termination (§10.2). 11

The entry point can optionally have one formal parameter, and this formal parameter can have any name. If 12 such a parameter is declared, it shall obey the following constraints: 13

• The implementation shall ensure that the value of this parameter is not null. 14

• Let args be the name of the parameter. If the length of the array designated by args is greater than 15 zero, the array members args[0] through args[args.Length-1], inclusive, shall refer to strings, 16 called application parameters, which are given implementation-defined values by the host environment 17 prior to application startup. The intent is to supply to the application information determined prior to 18 application startup from elsewhere in the hosted environment. If the host environment is not capable of 19 supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the 20 strings are received in lowercase. [Note: On systems supporting a command line, application parameters 21 correspond to what are generally known as command-line arguments. end note] 22

Since C# supports method overloading, a class or struct can contain multiple definitions of some method, 23 provided each has a different signature. However, within a single program, no class or struct shall contain 24 more than one method called Main whose definition qualifies it to be used as an application entry point. 25 Other overloaded versions of Main are permitted, however, provided they have more than one parameter, or 26 their only parameter is other than type string[]. 27

An application can be made up of multiple classes or structs. It is possible for more than one of these classes 28 or structs to contain a method called Main whose definition qualifies it to be used as an application entry 29 point. In such cases, one of these Main methods shall be chosen as the entry point so that application startup 30 can occur. This choice of an entry point is beyond the scope of this specification—no mechanism for 31 specifying or determining an entry point is provided. 32

In C#, every method shall be defined as a member of a class or struct. Ordinarily, the declared accessibility 33 (§10.5.1) of a method is determined by the access modifiers (§17.2.3) specified in its declaration, and 34 similarly the declared accessibility of a type is determined by the access modifiers specified in its 35 declaration. In order for a given method of a given type to be callable, both the type and the member shall be 36 accessible. However, the application entry point is a special case. Specifically, the execution environment 37 can access the application's entry point regardless of its declared accessibility and regardless of the declared 38 accessibility of its enclosing type declarations. 39

The entry point method shall not be defined in a generic class declaration (§26.1) or a generic struct 40 declaration (§26.2). 41

In all other respects, entry point methods behave like those that are not entry points. 42

Page 104: C# Language Specification

C# LANGUAGE SPECIFICATION

84

10.2 Application termination 1

Application termination returns control to the execution environment. 2

If the return type of the application’s entry point method is int, the value returned serves as the 3 application's termination status code. The purpose of this code is to allow communication of success or 4 failure to the execution environment. 5

If the return type of the entry point method is void, reaching the right brace (}) which terminates that 6 method, or executing a return statement that has no expression, results in a termination status code of 0. 7

Prior to an application’s termination, destructors for all of its objects that have not yet been garbage 8 collected are called, unless such cleanup has been suppressed (by a call to the library method 9 GC.SuppressFinalize, for example). 10

10.3 Declarations 11

Declarations in a C# program define the constituent elements of the program. C# programs are organized 12 using namespace declarations (§16), which can contain type declarations and nested namespace declarations. 13 Type declarations (§16.6) are used to define classes (§17), structs (§18), interfaces (§20), enums (§21), and 14 delegates (§22). The kinds of members permitted in a type declaration depend on the form of the type 15 declaration. For instance, class declarations can contain declarations for constants (§17.3), fields (§17.4), 16 methods (§17.5), properties (§17.6), events (§17.7), indexers (§17.8), operators (§17.9), instance 17 constructors (§17.10), destructors (§17.12), static constructors (§17.11), and nested types. 18

A declaration defines a name in the declaration space to which the declaration belongs. It is a compile-time 19 error to have two or more declarations that introduce members with the same name in a declaration space, 20 except in the following cases 21

• Two or more namespace declarations with the same name are allowed in the same declaration space. 22 Such namespace declarations are aggregated to form a single logical namespace and share a single 23 declaration space. 24

• Declarations in separate programs but in the same namespace declaration space are allowed to share the 25 same name. 26

• Two or more methods with the same name but distinct signatures are allowed in the same declaration 27 space (§10.6). 28

• Two or more type declarations with the same name but distinct numbers of type parameters are allowed 29 in the same declaration space (§10.8.2). 30

• Two or more type declarations with the partial modifier in the same declaration space may share the 31 same name, same number of type parameters and same classification (class, struct or interface). 32 In this case, the type declarations contribute to a single type and are themselves aggregated to form a 33 single declaration space (§17.1.4). 34

• A namespace declaration and one or more type declarations in the same declaration space may share the 35 same name as long as the type declarations all have at least one type parameter (§10.8.2). 36

It is never possible for a type declaration space to contain different kinds of members with the same name. 37 [Example: A type declaration space can never contain a field and a method by the same name. end example] 38

There are several different types of declaration spaces, as described in the following. 39

• Within all source files of a program, namespace-member-declarations with no enclosing namespace-40 declaration are members of a single combined declaration space called the global declaration space. 41

• Within all source files of a program, namespace-member-declarations within namespace-declarations 42 that have the same fully qualified namespace name are members of a single combined declaration space. 43

Page 105: C# Language Specification

Chapter 10 Basic concepts

85

• Each compilation-unit and namespace-body has an alias declaration space. Each extern-alias-directive 1 and using-alias-directive of the compilation-unit or namespace-body contributes a member to the alias 2 declaration space (§16.4.1). 3

• Each non-partial class, struct, or interface declaration creates a new declaration space. Each partial class, 4 struct, or interface declaration contributes to a declaration space shared by all matching parts in the same 5 program (§17.1.4). Names are introduced into this declaration space through the type-parameter-list and 6 class-member-declarations, struct-member-declarations, or interface-member-declarations. Except for 7 overloaded instance constructor declarations and static constructor declarations, a class or struct member 8 declaration cannot introduce a member by the same name as the class or struct. A class, struct, or 9 interface permits the declaration of overloaded methods and indexers. Furthermore, a class or struct 10 permits the declaration of overloaded instance constructors, operators, and types. [Example: A class, 11 struct, or interface can contain multiple method declarations with the same name, provided these method 12 declarations differ in their signature (§10.6). A class or struct can contain multiple nested types with the 13 same name provided the types differ in the number of type parameters. end example] Base classes do not 14 contribute to the declaration space of a class, and base interfaces do not contribute to the declaration 15 space of an interface. Thus, a derived class or interface is allowed to declare a member with the same 16 name as an inherited member. 17

• Each enumeration declaration creates a new declaration space. Names are introduced into this 18 declaration space through enum-member-declarations. 19

• Each block or switch-block creates a declaration space for local variables and local constants called the 20 local variable declaration space. Names are introduced into this declaration space through local-21 variable-declarations and local-constant-declarations. If a block is the body of an instance constructor, 22 method, or operator declaration, or a get or set accessor for an indexer declaration, the parameters 23 declared in such a declaration are members of the block’s local variable declaration space. If a block is 24 the body of a generic method, the type parameters declared in such a declaration are members of the 25 block’s local variable declaration space. It is an error for two members of a local variable declaration 26 space to have the same name. It is an error for the local variable declaration space of a block and the 27 local variable declaration space of a nested block to contain elements with the same name. [Note: Thus, 28 within a nested block it is not possible to declare a local variable or constant with the same name as a 29 local variable or constant in an enclosing block. It is possible for two nested blocks to contain elements 30 with the same name as long as neither block contains the other. end note] 31

• Each block or switch-block creates a separate declaration space for labels called the label declaration 32 space of the block. Names are introduced into this declaration space through labeled-statements, and the 33 names are referenced through goto-statements. It is an error for the label declaration space of a block 34 and the label declaration space of a nested block to contain elements with the same name. Thus, within a 35 nested block it is not possible to declare a label with the same name as a label in an enclosing block. 36 [Note: It is possible for two nested blocks to contain elements with the same name as long as neither 37 block contains the other. end note] 38

The textual order in which names are declared is generally of no significance. In particular, textual order is 39 not significant for the declaration and use of namespaces, constants, methods, properties, events, indexers, 40 operators, instance constructors, destructors, static constructors, and types. Declaration order is significant in 41 the following ways: 42

• Declaration order for field declarations and local variable declarations determines the order in which 43 their initializers (if any) are executed. When there are field declarations in multiple partial type 44 declarations for the same type, the order of the parts is unspecified. However, within each part the field 45 initializers are executed in order. 46

• Local variables and local constants shall be defined before they are used (§10.7). 47

• Declaration order for enum member declarations (§21.3) is significant when constant-expression values 48 are omitted. 49

Page 106: C# Language Specification

C# LANGUAGE SPECIFICATION

86

[Example: The declaration space of a namespace is “open ended”, and two namespace declarations with the 1 same fully qualified name contribute to the same declaration space. For example 2

namespace Megacorp.Data 3 { 4 class Customer 5 { 6 … 7 } 8 } 9

namespace Megacorp.Data 10 { 11 class Order 12 { 13 … 14 } 15 } 16

The two namespace declarations above contribute to the same declaration space, in this case declaring two 17 classes with the fully qualified names Megacorp.Data.Customer and Megacorp.Data.Order. Because 18 the two declarations contribute to the same declaration space, it would have caused a compile-time error if 19 each contained a declaration of a class with the same name. 20

All declarations of a partial type contribute to the same declaration space: 21

partial class C 22 { 23 int F; 24 partial struct N1 {…} 25 partial class N2 {…} 26 partial class N3 {…} 27 } 28

partial class C 29 { 30 void F() {…} // Error: conflicts with field F 31 partial class N1 {…} // Error: conflicts with struct N1 32 class N2 {…} // Error: conflicts with other N2 33 partial class N3 {…} // Ok 34 } 35

The two partial declarations for C combine to form a single declaration space and a single type. The field 36 named F in the first part conflicts with the method named F in the second part. The struct named N1 in the 37 first part conflicts with the class named N1 in the second part. The non-partial class N2 in the second part 38 conflicts with the partial class N2 in the first part. The two partial declarations for N3 combine to form a 39 single type C.N3. end example] 40

[Note: As specified above, the declaration space of a block cannot share names with the declaration spaces 41 of any nested blocks. Thus, in the following example, the F and G methods result in a compile-time error 42 because the name i is declared in the outer block and cannot be redeclared in the inner block. However, the 43 H and I methods are valid since the two i’s are declared in separate non-nested blocks. 44

class A 45 { 46 void F() { 47 int i = 0; 48 if (true) { 49 int i = 1; 50 } 51 } 52

void G() { 53 if (true) { 54 int i = 0; 55 } 56 int i = 1; 57 } 58

Page 107: C# Language Specification

Chapter 10 Basic concepts

87

void H() { 1 if (true) { 2 int i = 0; 3 } 4 if (true) { 5 int i = 1; 6 } 7 } 8

void I() { 9 for (int i = 0; i < 10; i++) 10 H(); 11 for (int i = 0; i < 10; i++) 12 H(); 13 } 14 } 15

end note] 16

10.4 Members 17

Namespaces and types have members. [Note: The members of an entity are generally available through the 18 use of a qualified name that starts with a reference to the entity, followed by a “.” token, followed by the 19 name of the member. end note] 20

Members of a type are either declared in the type or inherited from the base class of the type. When a type 21 inherits from a base class, all members of the base class, except instance constructors, destructors, and static 22 constructors become members of the derived type. The declared accessibility of a base class member does 23 not control whether the member is inherited—inheritance extends to any member that isn’t an instance 24 constructor, static constructor, or destructor. However, an inherited member might not be accessible in a 25 derived type, either because of its declared accessibility (§10.5.1) or because it is hidden by a declaration in 26 the type itself (§10.7.1.2). 27

10.4.1 Namespace members 28

Namespaces and types that have no enclosing namespace are members of the global namespace. This 29 corresponds directly to the names declared in the global declaration space. 30

Namespaces and types declared within a namespace are members of that namespace. This corresponds 31 directly to the names declared in the declaration space of the namespace. 32

Namespaces have no access restrictions. It is not possible to declare private, protected, or internal 33 namespaces, and namespace names are always publicly accessible. 34

10.4.2 Struct members 35

The members of a struct are the members declared in the struct and the members inherited from the struct’s 36 direct base class System.ValueType and the indirect base class object. 37

The members of a simple type are the members of the struct type aliased by the simple type (§11.1.4). 38

10.4.3 Enumeration members 39

The members of an enumeration are the constants declared in the enumeration and the members inherited 40 from the enumeration’s direct base class System.Enum and the indirect base classes System.ValueType 41 and object. 42

10.4.4 Class members 43

The members of a class are the members declared in the class and the members inherited from the base class 44 (except for class object which has no base class). The members inherited from the base class include the 45 constants, fields, methods, properties, events, indexers, operators, and types of the base class, but not the 46 instance constructors, destructors, and static constructors of the base class. Base class members are inherited 47 without regard to their accessibility. 48

Page 108: C# Language Specification

C# LANGUAGE SPECIFICATION

88

A class declaration can contain declarations of constants, fields, methods, properties, events, indexers, 1 operators, instance constructors, destructors, static constructors, and types. 2

The members of object and string correspond directly to the members of the class types they alias: 3

• The members of object are the members of the System.Object class. 4

• The members of string are the members of the System.String class. 5

10.4.5 Interface members 6

The members of an interface are the members declared in the interface and in all base interfaces of the 7 interface. [Note: The members in class object are not, strictly speaking, members of any interface (§20.2). 8 However, the members in class object are available via member lookup in any interface type (§14.3). end 9 note] 10

10.4.6 Array members 11

The members of an array are the members inherited from class System.Array. 12

10.4.7 Delegate members 13

The members of a delegate are the members inherited from class System.Delegate. 14

10.5 Member access 15

Declarations of members allow control over member access. The accessibility of a member is established by 16 the declared accessibility (§10.5.1) of the member combined with the accessibility of the immediately 17 containing type, if any. 18

When access to a particular member is allowed, the member is said to be accessible. Conversely, when 19 access to a particular member is disallowed, the member is said to be inaccessible. Access to a member is 20 permitted when the textual location in which the access takes place is included in the accessibility domain 21 (§10.5.2) of the member. 22

10.5.1 Declared accessibility 23

The declared accessibility of a member can be one of the following: 24

• Public, which is selected by including a public modifier in the member declaration. The intuitive 25 meaning of public is “access not limited”. 26

• Protected, which is selected by including a protected modifier in the member declaration. The 27 intuitive meaning of protected is “access limited to the containing class or types derived from the 28 containing class”. 29

• Internal, which is selected by including an internal modifier in the member declaration. The intuitive 30 meaning of internal is “access limited to this program”. 31

• Protected internal, which is selected by including both a protected and an internal modifier in the 32 member declaration. The intuitive meaning of protected internal is “access limited to this program 33 or types derived from the containing class”. 34

• Private, which is selected by including a private modifier in the member declaration. The intuitive 35 meaning of private is “access limited to the containing type”. 36

Depending on the context in which a member declaration takes place, only certain types of declared 37 accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, 38 the context in which the declaration takes place determines the default declared accessibility. 39

• Namespaces implicitly have public declared accessibility. No access modifiers are allowed on 40 namespace declarations. 41

Page 109: C# Language Specification

Chapter 10 Basic concepts

89

• Types declared in compilation units or namespaces can have public or internal declared 1 accessibility and default to internal declared accessibility. 2

• Class members can have any of the five kinds of declared accessibility and default to private declared 3 accessibility. [Note: A type declared as a member of a class can have any of the five kinds of declared 4 accessibility, whereas a type declared as a member of a namespace can have only public or internal 5 declared accessibility. end note] 6

• Struct members can have public, internal, or private declared accessibility and default to 7 private declared accessibility because structs are implicitly sealed. Struct members introduced in a 8 struct (that is, not inherited by that struct) cannot have protected or protected internal declared 9 accessibility. [Note: A type declared as a member of a struct can have public, internal, or private 10 declared accessibility, whereas a type declared as a member of a namespace can have only public or 11 internal declared accessibility. end note] 12

• Interface members implicitly have public declared accessibility. No access modifiers are allowed on 13 interface member declarations. 14

• Enumeration members implicitly have public declared accessibility. No access modifiers are allowed 15 on enumeration member declarations. 16

10.5.2 Accessibility domains 17

The accessibility domain of a member consists of the (possibly disjoint) sections of program text in which 18 access to the member is permitted. For purposes of defining the accessibility domain of a member, a member 19 is said to be top-level if it is not declared within a type, and a member is said to be nested if it is declared 20 within another type. Furthermore, the text of a program is defined as all source text contained in all source 21 files of that program, and the source text of a type is defined as all source text contained between the 22 opening and closing “{” and “}” tokens in the class-body, struct-body, interface-body, or enum-body of all 23 declarations for the type (including, possibly, multiple partial declarations and all types that are nested 24 within the type). 25

The accessibility domain of a predefined type (such as object, int, or double) is unlimited. 26

The accessibility domain of a top-level type T that is declared in a program P is defined as follows: 27

• If the declared accessibility of T is public, the accessibility domain of T is the program text of P and 28 any program that references P. 29

• If the declared accessibility of T is internal, the accessibility domain of T is the program text of P. 30

[Note: From these definitions it follows that the accessibility domain of a top-level type is always at least the 31 program text of the program in which that type is declared. end note] 32

The accessibility domain of a nested member M declared in a type T within a program P, is defined as 33 follows (noting that M itself might possibly be a type): 34

• If the declared accessibility of M is public, the accessibility domain of M is the accessibility domain 35 of T. 36

• If the declared accessibility of M is protected internal, let D be the union of the program text of P 37 and the program text of any type derived from T, which is declared outside P. The accessibility domain 38 of M is the intersection of the accessibility domain of T with D. 39

• If the declared accessibility of M is protected, let D be the union of the program text of T and the 40 program text of any type derived from T. The accessibility domain of M is the intersection of the 41 accessibility domain of T with D. 42

• If the declared accessibility of M is internal, the accessibility domain of M is the intersection of the 43 accessibility domain of T with the program text of P. 44

• If the declared accessibility of M is private, the accessibility domain of M is the program text of T. 45

Page 110: C# Language Specification

C# LANGUAGE SPECIFICATION

90

[Note: From these definitions it follows that the accessibility domain of a nested member is always at least 1 the program text of the type in which the member is declared. Furthermore, it follows that the accessibility 2 domain of a member is never more inclusive than the accessibility domain of the type in which the member 3 is declared. end note] 4

[Note: In intuitive terms, when a type or member M is accessed, the following steps are evaluated to ensure 5 that the access is permitted: 6

• First, if M is declared within a type (as opposed to a compilation unit or a namespace), a compile-time 7 error occurs if that type is not accessible. 8

• Then, if M is public, the access is permitted. 9

• Otherwise, if M is protected internal, the access is permitted if it occurs within the program in 10 which M is declared, or if it occurs within a class derived from the class in which M is declared and takes 11 place through the derived class type (§10.5.3). 12

• Otherwise, if M is protected, the access is permitted if it occurs within the class in which M is declared, 13 or if it occurs within a class derived from the class in which M is declared and takes place through the 14 derived class type (§10.5.3). 15

• Otherwise, if M is internal, the access is permitted if it occurs within the program in which M is 16 declared. 17

• Otherwise, if M is private, the access is permitted if it occurs within the type in which M is declared. 18

• Otherwise, the type or member is inaccessible, and a compile-time error occurs. 19

end note] 20

[Example: In the following code 21

public class A 22 { 23 public static int X; 24 internal static int Y; 25 private static int Z; 26 } 27

internal class B 28 { 29 public static int X; 30 internal static int Y; 31 private static int Z; 32

public class C 33 { 34 public static int X; 35 internal static int Y; 36 private static int Z; 37 } 38

private class D 39 { 40 public static int X; 41 internal static int Y; 42 private static int Z; 43 } 44 } 45

the classes and members have the following accessibility domains: 46

• The accessibility domain of A and A.X is unlimited. 47

• The accessibility domain of A.Y, B, B.X, B.Y, B.C, B.C.X, and B.C.Y is the program text of the 48 containing program. 49

• The accessibility domain of A.Z is the program text of A. 50

Page 111: C# Language Specification

Chapter 10 Basic concepts

91

• The accessibility domain of B.Z and B.D is the program text of B, including the program text of B.C 1 and B.D. 2

• The accessibility domain of B.C.Z is the program text of B.C. 3

• The accessibility domain of B.D.X and B.D.Y is the program text of B, including the program text of 4 B.C and B.D. 5

• The accessibility domain of B.D.Z is the program text of B.D. 6

As the example illustrates, the accessibility domain of a member is never larger than that of a containing 7 type. For example, even though all X members have public declared accessibility, all but A.X have 8 accessibility domains that are constrained by a containing type. end example] 9

As described in §10.4, all members of a base class, except for instance constructors, destructors, and 10 static constructors are inherited by derived types. This includes even private members of a base class. 11 However, the accessibility domain of a private member includes only the program text of the type in 12 which the member is declared. [Example: In the following code 13

class A 14 { 15 int x; 16 static void F(B b) { 17 b.x = 1; // Ok 18 } 19 } 20

class B: A 21 { 22 static void F(B b) { 23 b.x = 1; // Error, x not accessible 24 } 25 } 26

the B class inherits the private member x from the A class. Because the member is private, it is only 27 accessible within the class-body of A. Thus, the access to b.x succeeds in the A.F method, but fails in the 28 B.F method. end example] 29

10.5.3 Protected access for instance members 30

When a protected instance member is accessed outside the program text of the class in which it is 31 declared, and when a protected internal instance member is accessed outside the program text of the 32 program in which it is declared, the access is required to take place through an instance of the derived class 33 type in which the access occurs. Let B be a base class that declares a protected instance member M, and let D 34 be a class that derives from B. Within the class-body of D, access to M can take one of the following forms: 35

• An unqualified type-name or primary-expression of the form M. 36

• A primary-expression of the form E.M, provided the type of E is D or a class derived from D. 37

• A primary-expression of the form base.M. 38

In addition to these forms of access, a derived class can access a protected instance constructor of a base 39 class in a constructor-initializer (§17.10.1). 40

[Example: In the following code 41

public class A 42 { 43 protected int x; 44

static void F(A a, B b) { 45 a.x = 1; // Ok 46 b.x = 1; // Ok 47 } 48 } 49

Page 112: C# Language Specification

C# LANGUAGE SPECIFICATION

92

public class B: A 1 { 2 static void F(A a, B b) { 3 a.x = 1; // Error, must access through instance of B 4 b.x = 1; // Ok 5 } 6 } 7

within A, it is possible to access x through instances of both A and B, since in either case the access takes 8 place through an instance of A or a class derived from A. However, within B, it is not possible to access x 9 through an instance of A, since A does not derive from B. end example] 10

In the context of generics (§26.1.6), the rules for accessing protected and protected internal 11 instance members are augmented by the following: 12

• Within a generic class G, access to an inherited protected instance member M using a primary-expression 13 of the form E.M is permitted if the type of E is a class type constructed from G or a class type derived 14 from a class type constructed from G. 15

10.5.4 Accessibility constraints 16

Several constructs in the C# language require a type to be at least as accessible as a member or another type. 17 A type T is said to be at least as accessible as a member or type M if the accessibility domain of T is a 18 superset of the accessibility domain of M. In other words, T is at least as accessible as M if T is accessible in 19 all contexts in which M is accessible. 20

The following accessibility constraints exist: 21

• The direct base class of a class type shall be at least as accessible as the class type itself. 22

• The explicit base interfaces of an interface type shall be at least as accessible as the interface type itself. 23

• The return type and parameter types of a delegate type shall be at least as accessible as the delegate type 24 itself. 25

• The type of a constant shall be at least as accessible as the constant itself. 26

• The type of a field shall be at least as accessible as the field itself. 27

• The return type and parameter types of a method shall be at least as accessible as the method itself. 28

• The type of a property shall be at least as accessible as the property itself. 29

• The type of an event shall be at least as accessible as the event itself. 30

• The type and parameter types of an indexer shall be at least as accessible as the indexer itself. 31

• The return type and parameter types of an operator shall be at least as accessible as the operator itself. 32

• The parameter types of an instance constructor shall be at least as accessible as the instance constructor 33 itself. 34

[Example: In the following code 35

class A {…} 36

public class B: A {…} 37

the B class results in a compile-time error because A is not at least as accessible as B. end example] 38

[Example: Likewise, in the following code 39

class A {…} 40

public class B 41 { 42 A F() {…} 43

internal A G() {…} 44

Page 113: C# Language Specification

Chapter 10 Basic concepts

93

public A H() {…} 1 } 2

the H method in B results in a compile-time error because the return type A is not at least as accessible as the 3 method. end example] 4

10.6 Signatures and overloading 5

Methods, instance constructors, indexers, and operators are characterized by their signatures: 6

• The signature of a method consists of the name of the method, the number of type parameters, and the 7 type and kind (value, reference, or output) of each of its formal parameters, considered in the order left 8 to right. The signature of a method specifically does not include the return type, parameter names or 9 type parameter names, nor does it include the params modifier that can be specified for the right-most 10 parameter. When a parameter type includes a type parameter of the method, the ordinal position of the 11 type parameter is used for type equivalence, not the name of the type parameter. 12

• The signature of an instance constructor consists of the type and kind (value, reference, or output) of 13 each of its formal parameters, considered in the order left to right. The signature of an instance 14 constructor specifically does not include the parameter names or the params modifier that can be 15 specified for the right-most parameter. 16

• The signature of an indexer consists of the type of each of its formal parameters, considered in the order 17 left to right. The signature of an indexer specifically does not include the element type or parameter 18 names, nor does it include the params modifier that can be specified for the right-most parameter. 19

• The signature of an operator consists of the name of the operator and the type of each of its formal 20 parameters, considered in the order left to right. The signature of an operator specifically does not 21 include the result type or parameter names. 22

Signatures are the enabling mechanism for overloading of members in classes, structs, and interfaces: 23

• Overloading of methods permits a class, struct, or interface to declare multiple methods with the same 24 name, provided their signatures are unique within that class, struct, or interface. 25

• Overloading of instance constructors permits a class or struct to declare multiple instance constructors, 26 provided their signatures are unique within that class or struct. 27

• Overloading of indexers permits a class, struct, or interface to declare multiple indexers, provided their 28 signatures are unique within that class, struct, or interface. 29

• Overloading of operators permits a class or struct to declare multiple operators with the same name, 30 provided their signatures are unique within that class or struct. 31

Although out and ref parameter modifiers are considered part of a signature, members declared in a single 32 type cannot differ in signature solely by ref and out. A compile-time error occurs if two members are 33 declared in the same type with signatures that would be the same if all parameters in both methods with out 34 modifiers were changed to ref modifiers. For other purposes of signature matching (e.g., hiding or 35 overriding), ref and out are considered part of the signature and do not match each other. [Note: This 36 restriction is to allow C# programs to be easily translated to run on the Common Language Infrastructure 37 (CLI), which does not provide a way to define methods that differ solely in ref and out. end note] 38

[Example: The following example shows a set of overloaded method declarations along with their 39 signatures. 40

interface ITest 41 { 42 void F(); // F() 43 void F(int x); // F(int) 44 void F(ref int x); // F(ref int) 45 void F(out int x); // F(out int) error 46

Page 114: C# Language Specification

C# LANGUAGE SPECIFICATION

94

void F(int x, int y); // F(int, int) 1 int F(string s); // F(string) 2 int F(int x); // F(int) error 3

void F(string[] a); // F(string[]) 4 void F(params string[] a); // F(string[]) error 5

void F<S>(S s); // F<`0>(`0) 6 void F<T>(T t); // F<`0>(`0) error 7

void F<S,T>(S s); // F<`0,`1>(`0) 8 void F<T,S>(S s); // F<`0,`1>(`1) ok 9 } 10

Note that any ref and out parameter modifiers (§17.5.1) are part of a signature. Thus, F(int), F(ref 11 int), and F(out int) are all unique signatures. However, F(ref int) and F(out int) cannot be 12 declared within the same interface because their signatures differ solely by ref and out. Also, note that the 13 return type and the params modifier are not part of a signature, so it is not possible to overload solely based 14 on return type or on the inclusion or exclusion of the params modifier. As such, the declarations of the 15 methods F(int) and F(params string[]) identified above, result in a compile-time error. end example] 16

10.7 Scopes 17

The scope of a name is the region of program text within which it is possible to refer to the entity declared 18 by the name without qualification of the name. Scopes can be nested, and an inner scope can redeclare the 19 meaning of a name from an outer scope. [Note: This does not, however, remove the restriction imposed by 20 §10.3 that within a nested block it is not possible to declare a local variable or local constant with the same 21 name as a local variable or local constant in an enclosing block. end note] The name from the outer scope is 22 then said to be hidden in the region of program text covered by the inner scope, and access to the outer name 23 is only possible by qualifying the name. 24

• The scope of a namespace member declared by a namespace-member-declaration (§16.5) with no 25 enclosing namespace-declaration is the entire program text. 26

• The scope of a namespace member declared by a namespace-member-declaration within a namespace-27 declaration whose fully qualified name is N, is the namespace-body of every namespace-declaration 28 whose fully qualified name is N or starts with N, followed by a period. 29

• The scope of a name defined by an extern-alias-directive (§16.3) extends over the using-directives, 30 global-attributes and namespace-member-declarations of the compilation-unit or namespace-body in 31 which the extern-alias-directive occurs. An extern-alias-directive does not contribute any new members 32 to the underlying declaration space. In other words, an extern-alias-directive is not transitive, but, rather, 33 affects only the compilation-unit or namespace-body in which it occurs. 34

• The scope of a name defined or imported by a using-directive (§16.3) extends over the global-attributes 35 and namespace-member-declarations of the compilation-unit or namespace-body in which the using-36 directive occurs. A using-directive can make zero or more namespace or type names available within a 37 particular compilation-unit or namespace-body, but does not contribute any new members to the 38 underlying declaration space. In other words, a using-directive is not transitive, but, rather, affects only 39 the compilation-unit or namespace-body in which it occurs. 40

• The scope of a member declared by a class-member-declaration (§17.1.4) is the class-body in which the 41 declaration occurs. In addition, the scope of a class member extends to the class-body of those derived 42 classes that are included in the accessibility domain (§10.5.2) of the member. 43

• The scope of a member declared by a struct-member-declaration (§18.2) is the struct-body in which the 44 declaration occurs. 45

• The scope of a member declared by an enum-member-declaration (§21.3) is the enum-body in which the 46 declaration occurs. 47

• The scope of a parameter declared in a method-declaration (§17.5) is the method-body of that method-48 declaration. 49

Page 115: C# Language Specification

Chapter 10 Basic concepts

95

• The scope of a parameter declared in an indexer-declaration (§17.8) is the accessor-declarations of that 1 indexer-declaration. 2

• The scope of a parameter declared in an operator-declaration (§17.9) is the block of that operator-3 declaration. 4

• The scope of a parameter declared in a constructor-declaration (§17.10) is the constructor-initializer 5 and block of that constructor-declaration. 6

• The scope of a label declared in a labeled-statement (§15.4) is the block in which the declaration occurs. 7

• The scope of a local variable declared in a local-variable-declaration (§15.5.1) is the block in which the 8 declaration occurs. 9

• The scope of a local variable declared in a switch-block of a switch statement (§15.7.2) is the switch-10 block. 11

• The scope of a local variable declared in a for-initializer of a for statement (§15.8.3) is the for-12 initializer, the for-condition, the for-iterator, and the contained statement of the for statement. 13

• The scope of a local constant declared in a local-constant-declaration (§15.5.2) is the block in which the 14 declaration occurs. It is a compile-time error to refer to a local constant in a textual position that 15 precedes its constant-declarator. 16

Within the scope of a namespace, class, struct, or enumeration member it is possible to refer to the member 17 in a textual position that precedes the declaration of the member. [Example: 18

class A 19 { 20 void F() { 21 i = 1; 22 } 23 int i = 0; 24 } 25

Here, it is valid for F to refer to i before it is declared. end example] 26

Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual 27 position that precedes the local-variable-declarator of the local variable. [Example: 28

class A 29 { 30 int i = 0; 31

void F() { 32 i = 1; // Error, use precedes declaration 33 int i; 34 i = 2; 35 } 36

void G() { 37 int j = (j = 1); // Valid 38 } 39

void H() { 40 int a = 1, b = ++a; // Valid 41 } 42 } 43

In the F method above, the first assignment to i specifically does not refer to the field declared in the outer 44 scope. Rather, it refers to the local variable and it results in a compile-time error because it textually 45 precedes the declaration of the variable. In the G method, the use of j in the initializer for the declaration of 46 j is valid because the use does not precede the local-variable-declarator. In the H method, a subsequent 47 local-variable-declarator correctly refers to a local variable declared in an earlier local-variable-declarator 48 within the same local-variable-declaration. end example] 49

[Note: The scoping rules for local variables and local constants are designed to guarantee that the meaning 50 of a name used in an expression context is always the same within a block. If the scope of a local variable 51

Page 116: C# Language Specification

C# LANGUAGE SPECIFICATION

96

were to extend only from its declaration to the end of the block, then in the example above, the first 1 assignment would assign to the instance variable and the second assignment would assign to the local 2 variable. (In certain situations, but not in the example above, this could lead to a compile-time error if the 3 statements of the block were later to be rearranged.) 4

The meaning of a name within a block can differ based on the context in which the name is used. In the 5 example 6

using System; 7

class A {} 8

class Test 9 { 10 static void Main() { 11 string A = "hello, world"; // declarator context 12 string s = A; // expression context 13

Type t = typeof(A); // type context 14

Console.WriteLine(s); // writes "hello, world" 15 Console.WriteLine(t.ToString()); // writes "Type: A" 16 } 17 } 18

the name A is used in an expression context to refer to the local variable A and in a type context to refer to 19 the class A. end note] 20

10.7.1 Name hiding 21

The scope of an entity typically encompasses more program text than the declaration space of the entity. In 22 particular, the scope of an entity can include declarations that introduce new declaration spaces containing 23 entities of the same name. Such declarations cause the original entity to become hidden. Conversely, an 24 entity is said to be visible when it is not hidden. 25

Name hiding occurs when scopes overlap through nesting and when scopes overlap through inheritance. The 26 characteristics of the two types of hiding are described in the following subclauses. 27

Local variables, local constants, parameters, and method type parameters cannot hide other local variables, 28 local constants, parameters, or method type parameters (§10.3). 29

10.7.1.1 Hiding through nesting 30

Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a 31 result of nesting types within classes or structs, and as a result of parameter, local variable, and local 32 constant declarations. [Example: In the following code 33

class A 34 { 35 int i = 0; 36

void F() { 37 int i = 1; 38 } 39

void G() { 40 i = 1; 41 } 42 } 43

within the F method, the instance variable i is hidden by the local variable i, but within the G method, i still 44 refers to the instance variable. end example] 45

When a name in an inner scope hides a name in an outer scope, it hides all overloaded occurrences of that 46 name. [Example: In the following code 47

class Outer 48 { 49 static void F(int i) {} 50

Page 117: C# Language Specification

Chapter 10 Basic concepts

97

static void F(string s) {} 1

class Inner 2 { 3 void G() { 4 F(1); // Invokes Outer.Inner.F 5 F("Hello"); // Error 6 } 7

static void F(long l) {} 8 } 9 } 10

the call F(1) invokes the F declared in Inner because all outer occurrences of F are hidden by the inner 11 declaration. For the same reason, the call F("Hello") results in a compile-time error. end example] 12

10.7.1.2 Hiding through inheritance 13

Name hiding through inheritance occurs when classes or structs redeclare names that were inherited from 14 base classes. This type of name hiding takes one of the following forms: 15

• A constant, field, property, or event introduced in a class or struct hides all base class members with the 16 same name and no type parameters. 17

• A type introduced in a class or struct hides all base class members with the same name and same number 18 of type parameters. 19

• A method introduced in a class or struct hides all non-method base class members with the same name 20 and either the same number of type parameters or no type parameters, and all base class methods with 21 the same signature (method name, type parameter count, parameter count, modifiers, and types). 22

• An indexer introduced in a class or struct hides all base class indexers with the same signature 23 (parameter count and types). 24

Contrary to hiding a name from an outer scope, hiding an accessible name from an inherited scope causes a 25 warning to be reported. [Example: In the following code 26

class Base 27 { 28 public void F() {} 29 } 30

class Derived: Base 31 { 32 public void F() {} // Warning, hiding an inherited name 33 } 34

the declaration of F in Derived causes a warning to be reported. Hiding an inherited name is specifically 35 not an error, since that would preclude separate evolution of base classes. For example, the above situation 36 might have come about because a later version of Base introduced an F method that wasn’t present in an 37 earlier version of the class. Had the above situation been an error, then any change made to a base class in a 38 separately versioned class library could potentially cause derived classes to become invalid. end example] 39

The warning caused by hiding an inherited name can be eliminated through use of the new modifier: 40 [Example: 41

class Base 42 { 43 public void F() {} 44 } 45

class Derived: Base 46 { 47 new public void F() {} 48 } 49

The new modifier indicates that the F in Derived is “new”, and that it is indeed intended to hide the 50 inherited member. end example] 51

Page 118: C# Language Specification

C# LANGUAGE SPECIFICATION

98

A declaration of a new member hides an inherited member only within the scope of the new member. 1 [Example: 2

class Base 3 { 4 public static void F() {} 5 } 6

class Derived: Base 7 { 8 new private static void F() {} // Hides Base.F in Derived only 9 } 10

class MoreDerived: Derived 11 { 12 static void G() { F(); } // Invokes Base.F 13 } 14

In the example above, the declaration of F in Derived hides the F that was inherited from Base, but since 15 the new F in Derived has private access, its scope does not extend to MoreDerived. Thus, the call F() in 16 MoreDerived.G is valid and will invoke Base.F. end example] 17

10.8 Namespace and type names 18

Several contexts in a C# program require a namespace-name or a type-name to be specified. 19

namespace-name: 20 namespace-or-type-name 21

type-name: 22 namespace-or-type-name 23

namespace-or-type-name: 24 identifier type-argument-listopt 25 qualified-alias-member 26 namespace-or-type-name . identifier type-argument-listop 27

A namespace-name is a namespace-or-type-name that refers to a namespace. 28

Following resolution as described below, the namespace-or-type-name of a namespace-name shall refer to a 29 namespace, or otherwise a compile-time error occurs. Type arguments (§26.5.1) shall not be present in a 30 namespace-name (only types can have type arguments). 31

A type-name is a namespace-or-type-name that refers to a type. Following resolution as described below, the 32 namespace-or-type-name of a type-name shall refer to a type, or otherwise a compile-time error occurs. 33

The syntax and semantics of qualified-alias-member are defined in §16.7. 34

A namespace-or-type-name that is not a qualified-alias-member has one of four forms: 35

• I 36

• I<A1, ..., AK> 37

• N.I 38

• N.I<A1, ..., AK> 39

where I is a single identifier, N is a namespace-or-type-name and <A1, ..., AK> is an optional type-40 argument-list. When no type-argument-list is specified, consider K to be zero. 41

The meaning of a namespace-or-type-name is determined as follows: 42

• If the namespace-or-type-name is a qualified-alias-member, the meaning is as specified in §16.7. 43

• Otherwise, if the namespace-or-type-name is of the form I or of the form I<A1, ..., AK>: 44

Page 119: C# Language Specification

Chapter 10 Basic concepts

99

o If K is zero and the namespace-or-type-name appears within the body of a generic method 1 declaration (§26.6) and if that declaration includes a type parameter (§26.1.1) with name I, then the 2 namespace-or-type-name refers to that type parameter. 3

o Otherwise, if the namespace-or-type-name appears within the body of a type declaration, then for 4 each instance type T (§26.1.2), starting with the instance type of that type declaration and continuing 5 with the instance type of each enclosing class or struct declaration (if any): 6

• If K is zero and the declaration of T includes a type parameter with name I, then the namespace-7 or-type-name refers to that type parameter. 8

• Otherwise, if T contains a nested accessible type having name I and K type parameters, then the 9 namespace-or-type-name refers to that type constructed with the given type arguments. If there 10 is more than one such type, the type declared within the more derived type is selected. [Note: 11 Non-type members (constants, fields, methods, properties, indexers, operators, instance 12 constructors, destructors, and static constructors) and type members with a different number of 13 type parameters are ignored when determining the meaning of the namespace-or-type-name. end 14 note] 15

o Otherwise, for each namespace N, starting with the namespace in which the namespace-or-type-16 name occurs, continuing with each enclosing namespace (if any), and ending with the global 17 namespace, the following steps are evaluated until an entity is located: 18

• If K is zero and I is the name of a namespace in N, then: 19

o If the location where the namespace-or-type-name occurs is enclosed by a namespace 20 declaration for N and the namespace declaration contains an extern-alias-directive or using-21 alias-directive that associates the name I with a namespace or type, then the namespace-or-22 type-name is ambiguous and a compile-time error occurs. 23

o Otherwise, the namespace-or-type-name refers to the namespace named I in N. 24

• Otherwise, if N contains an accessible type having name I and K type parameters, then: 25

o If K is zero and the location where the namespace-or-type-name occurs is enclosed by a 26 namespace declaration for N and the namespace declaration contains an extern-alias-27 directive or using-alias-directive that associates the name I with a namespace or type, then 28 the namespace-or-type-name is ambiguous and a compile-time error occurs. 29

o Otherwise, the namespace-or-type-name refers to the type constructed with the given type 30 arguments. 31

• Otherwise, if the location where the namespace-or-type-name occurs is enclosed by a 32 namespace declaration for N: 33

o If K is zero and the namespace declaration contains an extern-alias-directive or using-alias-34 directive that associates the name I with an imported namespace or type, then the 35 namespace-or-type-name refers to that namespace or type. 36

o Otherwise, if the namespaces imported by the using-namespace-directives of the namespace 37 declaration contain exactly one type having name I and K type parameters, then the 38 namespace-or-type-name refers to that type constructed with the given type arguments. 39

o Otherwise, if the namespaces imported by the using-namespace-directives of the namespace 40 declaration contain more than one type having name I and K type parameters, then the 41 namespace-or-type-name is ambiguous and an error occurs. 42

o Otherwise, the namespace-or-type-name is undefined and a compile-time error occurs. 43

• Otherwise, the namespace-or-type-name is of the form N.I or of the form N.I<A1, ..., AK>. N is first 44 resolved as a namespace-or-type-name. If the resolution of N is not successful, a compile-time error 45 occurs. Otherwise, N.I or N.I<A1, ..., AK> is resolved as follows: 46

Page 120: C# Language Specification

C# LANGUAGE SPECIFICATION

100

o If K is zero and N refers to a namespace and N contains a nested namespace with name I, then the 1 namespace-or-type-name refers to that nested namespace. 2

o Otherwise, if N refers to a namespace and N contains an accessible type having name I and K type 3 parameters, then the namespace-or-type-name refers to that type constructed with the given type 4 arguments. 5

o Otherwise, if N refers to a (possibly constructed) class or struct type and N contains a nested 6 accessible type having name I and K type parameters, then the namespace-or-type-name refers to 7 that type constructed with the given type arguments. If there is more than one such type, the type 8 declared within the more derived type is selected. 9

o Otherwise, N.I is an invalid namespace-or-type-name, and a compile-time error occurs. 10

A namespace-or-type-name is permitted to reference a static class (§17.1.1.3) if 11

• The namespace-or-type-name is the T in a namespace-or-type-name of the form T.I, or 12

• The namespace-or-type-name is the T in a typeof-expression (§14.5.11) of the form typeof(T) 13

10.8.1 Unqualified name 14

Every namespace declaration and type declaration has an unqualified name determined as follows: 15

• For a namespace declaration, the unqualified name is the qualified-identifier specified in the declaration. 16

• For a type declaration with no type-parameter-list, the unqualified name is the identifier specified in the 17 declaration. 18

• For a type declaration with K type parameters, the unqualified name is the identifier specified in the 19 declaration, followed by the < token, K-1 commas and the > token. 20

10.8.2 Fully qualified names 21

Every namespace declaration and type declaration has a fully qualified name, which uniquely identifies the 22 namespace or type amongst all others. The fully qualified name of a namespace or type declaration with 23 unqualified name N is determined as follows: 24

• If the declaration is contained directly in a compilation unit and not nested in any other declaration, its 25 fully qualified name is N. 26

• Otherwise, its fully qualified name is S.N, where S is the fully qualified name of the immediately 27 enclosing namespace or type declaration. 28

In other words, the fully qualified name of a declaration is the complete hierarchical path of identifiers (and 29 generic dimensionality specifiers) that lead to the type or namespace, starting from the global namespace. 30 The fully qualified name of a declaration shall uniquely identify the namespace, non-generic type or generic 31 instance type (§26.1.2) associated with the declaration. It is a compile-time error for the same fully qualified 32 name to refer to two distinct entities. In particular: 33

• It is an error for both a namespace declaration and a type declaration to have the same fully qualified 34 name. 35

• It is an error for two different kinds of type declarations to have the same fully qualified name (for 36 example, if both a struct and class declaration have the same fully qualified name). 37

• It is an error for a type declaration without the partial modifier to have the same fully qualified name 38 as another type declaration (§17.1.4). 39

[Example: The example below shows several namespace and type declarations along with their associated 40 fully qualified names. 41

class A {} // A 42

Page 121: C# Language Specification

Chapter 10 Basic concepts

101

namespace X // X 1 { 2 class B // X.B 3 { 4 class C {} // X.B.C 5 } 6

namespace Y // X.Y 7 { 8 class D {} // X.Y.D 9 } 10 } 11

namespace X.Y // X.Y 12 { 13 class E {} // X.Y.E 14

class G<T> { // X.Y.G<> 15 class H {} // X.Y.G<>.H 16 } 17

class G<S,T> { // X.Y.G<,> 18 class H<U> {} // X.Y.G<,>.H<> 19 } 20 } 21

end example] 22

10.9 Automatic memory management 23

C# employs automatic memory management, which frees developers from manually allocating and freeing 24 the memory occupied by objects. Automatic memory management policies are implemented by a garbage 25 collector. The memory management life cycle of an object is as follows: 26

1. When the object is created, memory is allocated for it, the constructor is run, and the object is 27 considered live. 28

2. If no part of the object can be accessed by any possible continuation of execution, other than the running 29 of destructors, the object is considered no longer in use, and it becomes eligible for destruction. [Note: 30 Implementations might choose to analyze code to determine which references to an object can be used in 31 the future. For instance, if a local variable that is in scope is the only existing reference to an object, but 32 that local variable is never referred to in any possible continuation of execution from the current 33 execution point in the procedure, an implementation might (but is not required to) treat the object as no 34 longer in use. end note] 35

3. Once the object is eligible for destruction, at some unspecified later time the destructor (§17.12) (if any) 36 for the object is run. Unless overridden by explicit calls, the destructor for the object is run once only. 37

4. Once the destructor for an object is run, if that object, or any part of it, cannot be accessed by any 38 possible continuation of execution, including the running of destructors, the object is considered 39 inaccessible and the object becomes eligible for collection. 40

5. Finally, at some time after the object becomes eligible for collection, the garbage collector frees the 41 memory associated with that object. 42

The garbage collector maintains information about object usage, and uses this information to make memory 43 management decisions, such as where in memory to locate a newly created object, when to relocate an 44 object, and when an object is no longer in use or inaccessible. 45

Like other languages that assume the existence of a garbage collector, C# is designed so that the garbage 46 collector can implement a wide range of memory management policies. For instance, C# does not require 47 that destructors be run or that objects be collected as soon as they are eligible, or that destructors be run in 48 any particular order, or on any particular thread. 49

The behavior of the garbage collector can be controlled, to some degree, via static methods on the class 50 System.GC. [Note: While not required by this specification, a Collect method might be provided on the 51

Page 122: C# Language Specification

C# LANGUAGE SPECIFICATION

102

GC class which requests that a collection be performed. The following examples presume the existence of 1 such a method. end note] 2

[Example: Since the garbage collector is allowed wide latitude in deciding when to collect objects and run 3 destructors, a conforming implementation might produce output that differs from that shown by the 4 following code. The program 5

using System; 6

class A 7 { 8 ~A() { 9 Console.WriteLine("Destruct instance of A"); 10 } 11 } 12

class B 13 { 14 object Ref; 15 public B(object o) { 16 Ref = o; 17 } 18 ~B() { 19 Console.WriteLine("Destruct instance of B"); 20 } 21 } 22

class Test 23 { 24 static void Main() { 25 B b = new B(new A()); 26 b = null; 27 GC.Collect(); 28 GC.WaitForPendingFinalizers(); 29 } 30 } 31

creates an instance of class A and an instance of class B. These objects become eligible for garbage 32 collection when the variable b is assigned the value null, since after this time it is impossible for any user-33 written code to access them. The output could be either 34

Destruct instance of A 35 Destruct instance of B 36

or 37

Destruct instance of B 38 Destruct instance of A 39

because the language imposes no constraints on the order in which objects are garbage collected. 40

In subtle cases, the distinction between “eligible for destruction” and “eligible for collection” can be 41 important. For example, 42

using System; 43

class A 44 { 45 ~A() { 46 Console.WriteLine("Destruct instance of A"); 47 } 48 public void F() { 49 Console.WriteLine("A.F"); 50 Test.RefA = this; 51 } 52 } 53

Page 123: C# Language Specification

Chapter 10 Basic concepts

103

class B 1 { 2 public A Ref; 3 ~B() { 4 Console.WriteLine("Destruct instance of B"); 5 Ref.F(); 6 } 7 } 8

class Test 9 { 10 public static A RefA; 11 public static B RefB; 12 static void Main() { 13 RefB = new B(); 14 RefA = new A(); 15 RefB.Ref = RefA; 16 RefB = null; 17 RefA = null; 18

// A and B now eligible for destruction 19 GC.Collect(); 20 GC.WaitForPendingFinalizers(); 21 // B now eligible for collection, but A is not 22 if (RefA != null) 23 Console.WriteLine("RefA is not null"); 24 } 25 } 26

In the above program, if the garbage collector chooses to run the destructor of A before the destructor of B, 27 then the output of this program might be: 28

Destruct instance of A 29 Destruct instance of B 30 A.F 31 RefA is not null 32

Note that although the instance of A was not in use and A's destructor was run, it is still possible for methods 33 of A (in this case, F) to be called from another destructor. Also, note that running of a destructor might cause 34 an object to become usable from the mainline program again. In this case, the running of B's destructor 35 caused an instance of A that was previously not in use to become accessible from the live reference RefA. 36 After the call to WaitForPendingFinalizers, the instance of B is eligible for collection, but the instance 37 of A is not, because of the reference RefA. 38

To avoid confusion and unexpected behavior, it is generally a good idea for destructors to only perform 39 cleanup on data stored in their object's own fields, and not to perform any actions on referenced objects or 40 static fields. end example] 41

10.10 Execution order 42

Execution shall proceed such that the side effects of each executing thread are preserved at critical execution 43 points. A side effect is defined as a read or write of a volatile field, a write to a non-volatile variable, a write 44 to an external resource, and the throwing of an exception. The critical execution points at which the order of 45 these side effects shall be preserved are references to volatile fields (§17.4.3), lock statements (§15.12), and 46 thread creation and termination. An implementation is free to change the order of execution of a 47 C# program, subject to the following constraints: 48

• Data dependence is preserved within a thread of execution. That is, the value of each variable is 49 computed as if all statements in the thread were executed in original program order. 50

• Initialization ordering rules are preserved (§17.4.4 and §17.4.5). 51

• The ordering of side effects is preserved with respect to volatile reads and writes (§17.4.3). Additionally, 52 an implementation need not evaluate part of an expression if it can deduce that that expression’s value is 53 not used and that no needed side effects are produced (including any caused by calling a method or 54 accessing a volatile field). When program execution is interrupted by an asynchronous event (such as an 55

Page 124: C# Language Specification

C# LANGUAGE SPECIFICATION

104

exception thrown by another thread), it is not guaranteed that the observable side effects are visible in 1 the original program order. 2

Page 125: C# Language Specification

Chapter 11 Types

105

11. Types 1

The types of the C# language are divided into three main categories: Value types, reference types , and type-2 parameter types. 3

type: 4 value-type 5 reference-type 6 type-parameter 7

Type parameters are part of generics, and are discussed in §26.1.1. A fourth category of types, pointers, is 8 available only in unsafe code. This is discussed further in §25.2. 9

Value types differ from reference types in that variables of the value types directly contain their data, 10 whereas variables of the reference types store references to their data, the latter being known as objects. 11 With reference types, it is possible for two variables to reference the same object, and thus possible for 12 operations on one variable to affect the object referenced by the other variable. With value types, the 13 variables each have their own copy of the data, and it is not possible for operations on one to affect the other. 14 [Note: When a variable is a ref or out parameter, it does not have its own storage but references the 15 storage of another variable. In this case, the ref or out variable is effectively an alias for another variable 16 and not a distinct variable. end note] 17

C#’s type system is unified such that a value of any type can be treated as an object. Every type in C# 18 directly or indirectly derives from the object class type, and object is the ultimate base class of all types. 19 Values of reference types are treated as objects simply by viewing the values as type object. Values of 20 value types are treated as objects by performing boxing and unboxing operations (§11.3). 21

11.1 Value types 22

A value type is either a struct type or an enumeration type. C# provides a set of predefined struct types 23 called the simple types. The simple types are identified through reserved words. 24

value-type: 25 struct-type 26 enum-type 27

struct-type: 28 type-name 29 simple-type 30

simple-type: 31 numeric-type 32 bool 33

numeric-type: 34 integral-type 35 floating-point-type 36 decimal 37

Page 126: C# Language Specification

C# LANGUAGE SPECIFICATION

106

integral-type: 1 sbyte 2 byte 3 short 4 ushort 5 int 6 uint 7 long 8 ulong 9 char 10

floating-point-type: 11 float 12 double 13

enum-type: 14 type-name 15

All value types implicitly inherit from class object. It is not possible for any type to derive from a value 16 type, and value types are thus implicitly sealed (§17.1.1.2). 17

A variable of a value type always contains a value of that type. Unlike reference types, it is not possible for 18 a value of a value type to be null, or to reference an object of a more derived type. 19

Assignment to a variable of a value type creates a copy of the value being assigned. This differs from 20 assignment to a variable of a reference type, which copies the reference but not the object identified by the 21 reference. 22

11.1.1 The System.ValueType type 23

All value types implicitly inherit from the class System.ValueType, which, in turn, inherits from class 24 object. 25

Note that System.ValueType is not itself a value-type. Rather, it is a class-type from which all value-types 26 are automatically derived. 27

11.1.2 Default constructors 28

All value types implicitly declare a public parameterless instance constructor called the default constructor. 29 The default constructor returns a zero-initialized instance known as the default value for the value type: 30

• For all simple-types, the default value is the value produced by a bit pattern of all zeros: 31

o For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0. 32

o For char, the default value is '\x0000'. 33

o For float, the default value is 0.0f. 34

o For double, the default value is 0.0d. 35

o For decimal, the default value is 0m. 36

o For bool, the default value is false. 37

• For an enum-type E, the default value is 0. 38

• For a struct-type, the default value is the value produced by setting all value type fields to their default 39 value and all reference type fields to null. 40

Like any other instance constructor, the default constructor of a value type is invoked using the new 41 operator. [Note: For efficiency reasons, this requirement is not intended to actually have the implementation 42 generate a constructor call. For value types, the default value expression (§14.5.13) produces the same result 43

Page 127: C# Language Specification

Chapter 11 Types

107

as using the default constructor. end note] [Example: In the code below, variables i, j and k are all 1 initialized to zero. 2

class A 3 { 4 void F() { 5 int i = 0; 6 int j = new int(); 7 int k = default(int); 8 } 9 } 10

end example] 11

Because every value type implicitly has a public parameterless instance constructor, it is not possible for a 12 struct type to contain an explicit declaration of a parameterless constructor. A struct type is however 13 permitted to declare parameterized instance constructors (§18.3.8). 14

11.1.3 Struct types 15

A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, 16 instance constructors, static constructors, and nested types. Struct types are described in §18. 17

11.1.4 Simple types 18

C# provides a set of predefined struct types called the simple types. The simple types are identified through 19 reserved words, but these reserved words are simply aliases for predefined struct types in the System 20 namespace, as described in the table below. 21

22

Reserved word Aliased type sbyte System.SByte

byte System.Byte

short System.Int16

ushort System.UInt16

int System.Int32

uint System.UInt32

long System.Int64

ulong System.UInt64

char System.Char

float System.Single

double System.Double

bool System.Boolean

decimal System.Decimal

23 Because a simple type aliases a struct type, every simple type has members. [Example: int has the members 24 declared in System.Int32 and the members inherited from System.Object, and the following 25 statements are permitted: 26

int i = int.MaxValue; // System.Int32.MaxValue constant 27 string s = i.ToString(); // System.Int32.ToString() instance method 28 string t = 123.ToString(); // System.Int32.ToString() instance method 29

end example] The simple types differ from other struct types in that they permit certain additional 30 operations: 31

Page 128: C# Language Specification

C# LANGUAGE SPECIFICATION

108

• Most simple types permit values to be created by writing literals (§9.4.4). [Example: 123 is a literal of 1 type int and 'a' is a literal of type char. end example] C# makes no provision for literals of struct 2 types in general. 3

• When the operands of an expression are all simple type constants, the compiler evaluates the expression 4 at compile-time. Such an expression is known as a constant-expression (§14.15). Expressions involving 5 operators defined by other struct types are not considered to be constant expressions. 6

• Through const declarations, it is possible to declare constants of the simple types (§17.3). It is not 7 possible to have constants of other struct types, but a similar effect is provided by static readonly 8 fields. 9

• Conversions involving simple types can participate in evaluation of conversion operators defined by 10 other struct types, but a user-defined conversion operator can never participate in evaluation of another 11 user-defined conversion operator (§13.4.2). 12

11.1.5 Integral types 13

C# supports nine integral types: sbyte, byte, short, ushort, int, uint, long, ulong, and char. The 14 integral types have the following sizes and ranges of values: 15

• The sbyte type represents signed 8-bit integers with values from –128 to 127, inclusive. 16

• The byte type represents unsigned 8-bit integers with values from 0 to 255, inclusive. 17

• The short type represents signed 16-bit integers with values from –32768 to 32767, inclusive. 18

• The ushort type represents unsigned 16-bit integers with values from 0 to 65535, inclusive. 19

• The int type represents signed 32-bit integers with values from –2147483648 to 2147483647, 20 inclusive. 21

• The uint type represents unsigned 32-bit integers with values from 0 to 4294967295, inclusive. 22

• The long type represents signed 64-bit integers with values from –9223372036854775808 to 23 9223372036854775807, inclusive. 24

• The ulong type represents unsigned 64-bit integers with values from 0 to 18446744073709551615, 25 inclusive. 26

• The char type represents unsigned 16-bit integers with values from 0 to 65535, inclusive. The set of 27 possible values for the char type corresponds to the Unicode character set. [Note: Although char has 28 the same representation as ushort, not all operations permitted on one type are permitted on the other. 29 end note] 30

The integral-type unary and binary operators always operate with signed 32-bit precision, unsigned 32-bit 31 precision, signed 64-bit precision, or unsigned 64-bit precision, as detailed in clause §14. 32

The char type is classified as an integral type, but it differs from the other integral types in two ways: 33

• There are no implicit conversions from other types to the char type. In particular, even though the 34 sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, 35 implicit conversions from sbyte, byte, or ushort to char do not exist. 36

• Constants of the char type shall be written as character-literals or as integer-literals in combination 37 with a cast to type char. [Example: (char)10 is the same as '\x000A'. end example] 38

The checked and unchecked operators and statements are used to control overflow checking for integral-39 type arithmetic operations and conversions (§14.5.12). In a checked context, an overflow produces a 40 compile-time error or causes a System.OverflowException to be thrown. In an unchecked context, 41 overflows are ignored and any high-order bits that do not fit in the destination type are discarded. 42

Page 129: C# Language Specification

Chapter 11 Types

109

11.1.6 Floating point types 1

C# supports two floating-point types: float and double. The float and double types are represented 2 using the 32-bit single-precision and 64-bit double-precision IEC 60559 formats, which provide the 3 following sets of values: 4

• Positive zero and negative zero. In most situations, positive zero and negative zero behave identically as 5 the simple value zero, but certain operations distinguish between the two (§14.7.2). 6

• Positive infinity and negative infinity. Infinities are produced by such operations as dividing a non-zero 7 number by zero. [Example: 1.0 / 0.0 yields positive infinity, and –1.0 / 0.0 yields negative infinity. 8 end example] 9

• The Not-a-Number value, often abbreviated NaN. NaNs are produced by invalid floating-point 10 operations, such as dividing zero by zero. 11

• The finite set of non-zero values of the form s × m × 2e, where s is 1 or −1, and m and e are determined 12 by the particular floating-point type: For float, 0 < m < 224 and −149 ≤ e ≤ 104, and for double, 13 0 < m < 253 and −1075 ≤ e ≤ 970. Denormalized floating-point numbers are considered valid non-zero 14 values. C# neither requires nor forbids that a conforming implementation support denormalized floating-15 point numbers. 16

The float type can represent values ranging from approximately 1.5 × 10−45 to 3.4 × 1038 with a precision 17 of 7 digits. 18

The double type can represent values ranging from approximately 5.0 × 10−324 to 1.7 × 10308 with a 19 precision of 15–16 digits. 20

The floating-point operators, including the assignment operators, never produce exceptions. Instead, in 21 exceptional situations, floating-point operations produce zero, infinity, or NaN, as described below: 22

• The result of a floating-point operation is rounded to the nearest representable value in the destination 23 format. This may cause a non-zero value to be rounded to zero. 24

• If the magnitude of the result of a floating-point operation is too large for the destination format, the 25 result of the operation becomes positive infinity or negative infinity. 26

• If a floating-point operation is invalid, the result of the operation becomes NaN. 27

• If one or both operands of a floating-point operation is NaN, the result of the operation becomes NaN. 28

Floating-point operations can be performed with higher precision than the result type of the operation. 29 [Example: Some hardware architectures support an “extended” or “long double” floating-point type with 30 greater range and precision than the double type, and implicitly perform all floating-point operations using 31 this higher precision type. Only at excessive cost in performance can such hardware architectures be made to 32 perform floating-point operations with less precision, and rather than require an implementation to forfeit 33 both performance and precision, C# allows a higher precision type to be used for all floating-point 34 operations. Other than delivering more precise results, this rarely has any measurable effects. However, in 35 expressions of the form x * y / z, where the multiplication produces a result that is outside the double 36 range, but the subsequent division brings the temporary result back into the double range, the fact that the 37 expression is evaluated in a higher range format can cause a finite result to be produced instead of an 38 infinity. end example] 39

11.1.7 The decimal type 40

The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type 41 can represent values including those in the range 1 × 10−28 through 1 × 1028 with at least 28 significant digits. 42

The finite set of values of type decimal are of the form (–1)s × c × 10-e, where the sign s is 0 or 1, the 43 coefficient c is given by 0 ≤ c <Cmax, and the scale e is such that Emin ≤ e ≤ Emax, where Cmax is at least 44 1 × 1028, Emin <= 0, and Emax >= 28. The decimal type does not necessarily support signed zeros, 45 infinities, or NaN's. 46

Page 130: C# Language Specification

C# LANGUAGE SPECIFICATION

110

A decimal is represented as an integer scaled by a power of ten. For decimals with an absolute value less 1 than 1.0m, the value is exact to at least the 28th decimal place. For decimals with an absolute value greater 2 than or equal to 1.0m, the value is exact to at least 28 digits. Contrary to the float and double data types, 3 decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. In the 4 float and double representations, such numbers often have non-terminating binary expansions, making 5 those representations more prone to round-off errors. 6

The result of an operation on values of type decimal is that which would result from calculating an exact 7 result (preserving scale, as defined for each operator) and then rounding to fit the representation. Results are 8 rounded to the nearest representable value, and, when a result is equally close to two representable values, to 9 the value that has an even number in the least significant digit position (this is known as “banker’s 10 rounding”). That is, results are exact to at least the 28th decimal place. Note that rounding may produce a 11 zero value from a non-zero value. 12

If a decimal arithmetic operation produces a result whose magnitude is too large for the decimal format, a 13 System.OverflowException is thrown. 14

The decimal type has greater precision but may have a smaller range than the floating-point types. Thus, 15 conversions from the floating-point types to decimal might produce overflow exceptions, and conversions 16 from decimal to the floating-point types might cause loss of precision or overflow exceptions. For these 17 reasons, no implicit conversions exist between the floating-point types and decimal, and without explicit 18 casts, a compile-time error occurs when floating-point and decimal operands are directly mixed in the 19 same expression. 20

11.1.8 The bool type 21

The bool type represents boolean logical quantities. The possible values of type bool are true and false. 22

No standard conversions exist between bool and other types. In particular, the bool type is distinct and 23 separate from the integral types, and a bool value cannot be used in place of an integral value, and vice 24 versa. 25

[Note: In the C and C++ languages, a zero integral or floating-point value, or a null pointer can be converted 26 to the boolean value false, and a non-zero integral or floating-point value, or a non-null pointer can be 27 converted to the boolean value true. In C#, such conversions are accomplished by explicitly comparing an 28 integral or floating-point value to zero, or by explicitly comparing an object reference to null. end note] 29

11.1.9 Enumeration types 30

An enumeration type is a distinct type with named constants. Every enumeration type has an underlying 31 type, which shall be byte, sbyte, short, ushort, int, uint, long or ulong. Enumeration types are 32 defined through enumeration declarations (§21.1). The direct base type of every enumeration type is the 33 class System.Enum. The direct base class of System.Enum is System.ValueType. 34

11.2 Reference types 35

A reference type is a class type, an interface type, an array type, or a delegate type. 36

reference-type: 37 class-type 38 interface-type 39 array-type 40 delegate-type 41

class-type: 42 type-name 43 object 44 string 45

interface-type: 46 type-name 47

Page 131: C# Language Specification

Chapter 11 Types

111

array-type: 1 non-array-type rank-specifiers 2

non-array-type: 3 value-type 4 class-type 5 interface-type 6 delegate-type 7 type-parameter 8

rank-specifiers: 9 rank-specifier 10 rank-specifiers rank-specifier 11

rank-specifier: 12 [ dim-separatorsopt ] 13

dim-separators: 14 , 15 dim-separators , 16

delegate-type: 17 type-name 18

A reference type value is a reference to an instance of the type, the latter known as an object. The special 19 value null is compatible with all reference types and indicates the absence of an instance. 20

11.2.1 Class types 21

A class type defines a data structure that contains data members (constants and fields), function members 22 (methods, properties, events, indexers, operators, instance constructors, destructors, and static constructors), 23 and nested types. Class types support inheritance, a mechanism whereby derived classes can extend and 24 specialize base classes. Instances of class types are created using object-creation-expressions (§14.5.10.1). 25

Class types are described in §17. 26

11.2.2 The object type 27

The object class type is the ultimate base class of all other types. Every type in C# directly or indirectly 28 derives from the object class type. 29

The keyword object is simply an alias for the predefined class System.Object. 30

11.2.3 The string type 31

The string type is a sealed class type that inherits directly from object. Instances of the string class 32 represent Unicode character strings. 33

Values of the string type can be written as string literals (§9.4.4). 34

The keyword string is simply an alias for the predefined class System.String. 35

11.2.4 Interface types 36

An interface defines a contract. A class or struct that implements an interface shall adhere to its contract. An 37 interface can inherit from multiple base interfaces, and a class or struct can implement multiple interfaces. 38

Interface types are described in §20. 39

11.2.5 Array types 40

An array is a data structure that contains zero or more variables which are accessed through computed 41 indices. The variables contained in an array, also called the elements of the array, are all of the same type, 42 and this type is called the element type of the array. 43

Page 132: C# Language Specification

C# LANGUAGE SPECIFICATION

112

Array types are described in §19. 1

11.2.6 Delegate types 2

A delegate is a data structure that refers to one or more methods, and for instance methods, it also refers to 3 their corresponding object instances. 4

[Note: The closest equivalent of a delegate in C or C++ is a function pointer, but whereas a function pointer 5 can only reference static functions, a delegate can reference both static and instance methods. In the latter 6 case, the delegate stores not only a reference to the method’s entry point, but also a reference to the object 7 instance on which to invoke the method. end note] 8

Delegate types are described in §22. 9

11.3 Boxing and unboxing 10

The concept of boxing and unboxing is central to C#’s type system. It provides a bridge between value-types 11 and reference-types by permitting any value of a value-type to be converted to and from type object. 12 Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately 13 be treated as an object. 14

11.3.1 Boxing conversions 15

A boxing conversion permits any value-type to be implicitly converted to object or System.ValueType 16 or to any interface-type implemented by the value-type. Furthermore, there is an implicit boxing conversion 17 from any enumeration type to System.Enum. Boxing a value of a value-type consists of allocating an object 18 instance and copying the value-type value into that instance. 19

The actual process of boxing a value of a value-type is best explained by imagining the existence of a boxing 20 class for that type. [Example: For any value-type T, the boxing class behaves as if it were declared as 21 follows: 22

sealed class T_Box 23 { 24 T value; 25

public T_Box(T t) { 26 value = t; 27 } 28 } 29

Boxing of a value v of type T now consists of executing the expression new T_Box(v), and returning the 30 resulting instance as a value of type object. Thus, the statements 31

int i = 123; 32 object box = i; 33

conceptually correspond to 34

int i = 123; 35 object box = new int_Box(i); 36

end example] 37

Boxing classes like T_Box and int_Box above don’t actually exist and the dynamic type of a boxed value 38 isn’t actually a class type. Instead, a boxed value of type T has the dynamic type T, and a dynamic type 39 check using the is operator can simply reference type T. [Example: 40

int i = 123; 41 object box = i; 42 if (box is int) { 43 Console.Write("Box contains an int"); 44 } 45

will output the string “Box contains an int” on the console. end example] 46

Page 133: C# Language Specification

Chapter 11 Types

113

A boxing conversion implies making a copy of the value being boxed. This is different from a conversion of 1 a reference-type to type object, in which the value continues to reference the same instance and simply is 2 regarded as the less derived type object. [Example: Given the declaration 3

struct Point 4 { 5 public int x, y; 6

public Point(int x, int y) { 7 this.x = x; 8 this.y = y; 9 } 10 } 11

the following statements 12

Point p = new Point(10, 10); 13 object box = p; 14 p.x = 20; 15 Console.Write(((Point)box).x); 16

will output the value 10 on the console because the implicit boxing operation that occurs in the assignment 17 of p to box causes the value of p to be copied. Had Point been declared a class instead, the value 20 18 would be output because p and box would reference the same instance. end example] 19

11.3.2 Unboxing conversions 20

An unboxing conversion permits an explicit conversion from object or System.ValueType to any value-21 type or from any interface-type to any value-type that implements the interface-type. Furthermore, there is an 22 explicit unboxing conversion from System.Enum to any enumeration type. An unboxing operation consists 23 of first checking that the object instance is a boxed value of the given value-type, and then copying the value 24 out of the instance. 25

Referring to the imaginary boxing class described in the previous subclause, an unboxing conversion of an 26 object box to a value-type T consists of executing the expression ((T_Box)box).value. [Example: Thus, 27 the statements 28

object box = 123; 29 int i = (int)box; 30

conceptually correspond to 31

object box = new int_Box(123); 32 int i = ((int_Box)box).value; 33

end example] 34

For an unboxing conversion to a given value-type to succeed at run-time, the value of the source operand 35 shall be a reference to an object that was previously created by boxing a value of that value-type. If the 36 source operand is null a System.NullReferenceException is thrown. If the source operand is a 37 reference to an incompatible object, a System.InvalidCastException is thrown. 38

Page 134: C# Language Specification
Page 135: C# Language Specification

Chapter 12 Variables

115

12. Variables 1

Variables represent storage locations. Every variable has a type that determines what values can be stored in 2 the variable. C# is a type-safe language, and the C# compiler guarantees that values stored in variables are 3 always of the appropriate type. The value of a variable can be changed through assignment or through use of 4 the ++ and -- operators. 5

A variable shall be definitely assigned (§12.3) before its value can be obtained. 6

As described in the following subclauses, variables are either initially assigned or initially unassigned. An 7 initially assigned variable has a well-defined initial value and is always considered definitely assigned. An 8 initially unassigned variable has no initial value. For an initially unassigned variable to be considered 9 definitely assigned at a certain location, an assignment to the variable shall occur in every possible execution 10 path leading to that location. 11

12.1 Variable categories 12

C# defines seven categories of variables: static variables, instance variables, array elements, value 13 parameters, reference parameters, output parameters, and local variables. The subclauses that follow 14 describe each of these categories. 15

[Example: In the following code 16

class A 17 { 18 public static int x; 19 int y; 20

void F(int[] v, int a, ref int b, out int c) { 21 int i = 1; 22 c = a + b++; 23 } 24 } 25

x is a static variable, y is an instance variable, v[0] is an array element, a is a value parameter, b is a 26 reference parameter, c is an output parameter, and i is a local variable. end example] 27

12.1.1 Static variables 28

A field declared with the static modifier is called a static variable. A static variable comes into existence 29 before execution of the static constructor (§17.11) for its containing type, and ceases to exist when the 30 associated application domain ceases to exist. 31

The initial value of a static variable is the default value (§12.2) of the variable’s type. 32

For the purposes of definite assignment checking, a static variable is considered initially assigned. 33

12.1.2 Instance variables 34

A field declared without the static modifier is called an instance variable. 35

12.1.2.1 Instance variables in classes 36

An instance variable of a class comes into existence when a new instance of that class is created, and ceases 37 to exist when there are no references to that instance and the instance’s destructor (if any) has executed. 38

The initial value of an instance variable of a class is the default value (§12.2) of the variable’s type. 39

For the purpose of definite assignment checking, an instance variable is considered initially assigned. 40

Page 136: C# Language Specification

C# LANGUAGE SPECIFICATION

116

12.1.2.2 Instance variables in structs 1

An instance variable of a struct has exactly the same lifetime as the struct variable to which it belongs. In 2 other words, when a variable of a struct type comes into existence or ceases to exist, so too do the instance 3 variables of the struct. 4

The initial assignment state of an instance variable of a struct is the same as that of the containing struct 5 variable. In other words, when a struct variable is considered initially assigned, so too are its instance 6 variables, and when a struct variable is considered initially unassigned, its instance variables are likewise 7 unassigned. 8

12.1.3 Array elements 9

The elements of an array come into existence when an array instance is created, and cease to exist when 10 there are no references to that array instance. 11

The initial value of each of the elements of an array is the default value (§12.2) of the type of the array 12 elements. 13

For the purpose of definite assignment checking, an array element is considered initially assigned. 14

12.1.4 Value parameters 15

A parameter declared without a ref or out modifier is a value parameter. 16

A value parameter comes into existence upon invocation of the function member (method, instance 17 constructor, accessor, or operator) to which the parameter belongs, and is initialized with the value of the 18 argument given in the invocation. A value parameter ceases to exist upon return of the function member 19 (except when the value parameter is captured by an anonymous method (§14.5.14.3.1) or the function 20 member body is an iterator block (§27)). 21

For the purpose of definite assignment checking, a value parameter is considered initially assigned. 22

12.1.5 Reference parameters 23

A parameter declared with a ref modifier is a reference parameter. 24

A reference parameter does not create a new storage location. Instead, a reference parameter represents the 25 same storage location as the variable given as the argument in the function member invocation. Thus, the 26 value of a reference parameter is always the same as the underlying variable. 27

The following definite assignment rules apply to reference parameters. [Note: The rules for output 28 parameters are different, and are described in §12.1.6. end note] 29

• A variable shall be definitely assigned (§12.3) before it can be passed as a reference parameter in a 30 function member invocation. 31

• Within a function member, a reference parameter is considered initially assigned. 32

Within an instance method or instance accessor of a struct type, the this keyword behaves exactly as a 33 reference parameter of the struct type (§14.5.7). 34

12.1.6 Output parameters 35

A parameter declared with an out modifier is an output parameter. 36

An output parameter does not create a new storage location. Instead, an output parameter represents the 37 same storage location as the variable given as the argument in the function member invocation. Thus, the 38 value of an output parameter is always the same as the underlying variable. 39

The following definite assignment rules apply to output parameters. [Note: The rules for reference 40 parameters are different, and are described in §12.1.5. end note] 41

Page 137: C# Language Specification

Chapter 12 Variables

117

• A variable need not be definitely assigned before it can be passed as an output parameter in a function 1 member invocation. 2

• Following the normal completion of a function member invocation, each variable that was passed as an 3 output parameter is considered assigned in that execution path. 4

• Within a function member, an output parameter is considered initially unassigned. 5

• Every output parameter of a function member shall be definitely assigned (§12.3) before the function 6 member returns normally. 7

Within an instance constructor of a struct type, the this keyword behaves exactly as an output or reference 8 parameter of the struct type, depending on whether the constructor declaration includes a constructor 9 initializer (§14.5.7). 10

12.1.7 Local variables 11

A local variable is declared by a local-variable-declaration, foreach-statement, or specific-catch-clause of a 12 try-statement. For a foreach-statement, the local variable is an iteration variable (§15.8.4). For a specific-13 catch-clause, the local variable is an exception variable (§15.10). A local variable declared by a foreach-14 statement or specific-catch-clause is considered initially assigned. 15

A local-variable-declaration can occur in a block, a for-statement, a switch-block, or a using-statement. 16

The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be 17 reserved for it. This lifetime extends from entry into the scope with which it is associated, at least until 18 execution of that scope ends in some way. (Entering an enclosed block, calling a method, or yielding a value 19 from an iterator block suspends, but does not end, execution of the current scope.) If the local variable is 20 captured by an anonymous method, the lifetime of the variable is extended at least until all referencing 21 delegates are eligible for garbage collection (§14.5.14.3.1). If the parent scope is entered recursively or 22 iteratively, a new instance of the local variable is created each time, and its local-variable-initializer, if any, 23 is evaluated each time. [Note: A local variable is instantiated each time its scope is entered. This behavior is 24 visible to user code containing anonymous methods. end note] 25

A local variable introduced by a local-variable-declaration is not automatically initialized and thus has no 26 default value. Such a local variable is considered initially unassigned. A local-variable-declaration can 27 include a local-variable-initializer, in which case the variable is considered definitely assigned in its entire 28 scope, except within the expression provided in the local-variable-initializer. 29

Within the scope of a local variable, it is a compile-time error to refer to that local variable in a textual 30 position that precedes its local-variable-declarator. 31

[Note: The actual lifetime of a local variable is implementation-dependent. For example, a compiler might 32 statically determine that a local variable in a block is only used for a small portion of that block. Using this 33 analysis, the compiler could generate code that results in the variable’s storage having a shorter lifetime than 34 its containing block. 35

The storage referred to by a local reference variable is reclaimed independently of the lifetime of that local 36 reference variable (§10.9). end note] 37

12.2 Default values 38

The following categories of variables are automatically initialized to their default values: 39

• Static variables. 40

• Instance variables of class instances. 41

• Array elements. 42

The default value of a variable depends on the type of the variable and is determined as follows: 43

Page 138: C# Language Specification

C# LANGUAGE SPECIFICATION

118

• For a variable of a value-type, the default value is the same as the value computed by the value-type’s 1 default constructor (§11.1.1). 2

• For a variable of a reference-type, the default value is null. 3

[Note: Initialization to default values is typically done by having the memory manager or garbage collector 4 initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-5 zero to represent the null reference. end note] 6

12.3 Definite assignment 7

At a given location in the executable code of a function member, a variable is said to be definitely assigned 8 if the compiler can prove, by a particular static flow analysis (§12.3.3), that the variable has been 9 automatically initialized or has been the target of at least one assignment. [Note: Informally stated, the rules 10 of definite assignment are: 11

• An initially assigned variable (§12.3.1) is always considered definitely assigned. 12

• An initially unassigned variable (§12.3.2) is considered definitely assigned at a given location if all 13 possible execution paths leading to that location contain at least one of the following: 14

o A simple assignment (§14.13.1) in which the variable is the left operand. 15

o An invocation expression (§14.5.5) or object creation expression (§14.5.10.1) that passes the 16 variable as an output parameter. 17

o For a local variable, a local variable declaration (§15.5) that includes a variable initializer. 18

The formal specification underlying the above informal rules is described in §12.3.1, §12.3.2, and §12.3.3. 19 end note] 20

The definite assignment states of instance variables of a struct-type variable are tracked individually as well 21 as collectively. In additional to the rules above, the following rules apply to struct-type variables and their 22 instance variables: 23

• An instance variable is considered definitely assigned if its containing struct-type variable is considered 24 definitely assigned. 25

• A struct-type variable is considered definitely assigned if each of its instance variables is considered 26 definitely assigned. 27

Definite assignment is a requirement in the following contexts: 28

• A variable shall be definitely assigned at each location where its value is obtained. [Note: This ensures 29 that undefined values never occur. end note] The occurrence of a variable in an expression is considered 30 to obtain the value of the variable, except when 31

o the variable is the left operand of a simple assignment, 32

o the variable is passed as an output parameter, or 33

o the variable is a struct-type variable and occurs as the left operand of a member access. 34

• A variable shall be definitely assigned at each location where it is passed as a reference parameter. 35 [Note: This ensures that the function member being invoked can consider the reference parameter 36 initially assigned. end note] 37

• All output parameters of a function member shall be definitely assigned at each location where the 38 function member returns (through a return statement or through execution reaching the end of the 39 function member body). [Note: This ensures that function members do not return undefined values in 40 output parameters, thus enabling the compiler to consider a function member invocation that takes a 41 variable as an output parameter equivalent to an assignment to the variable. end note] 42

• The this variable of a struct-type instance constructor shall be definitely assigned at each location 43 where that instance constructor returns. 44

Page 139: C# Language Specification

Chapter 12 Variables

119

12.3.1 Initially assigned variables 1

The following categories of variables are classified as initially assigned: 2

• Static variables. 3

• Instance variables of class instances. 4

• Instance variables of initially assigned struct variables. 5

• Array elements. 6

• Value parameters. 7

• Reference parameters. 8

• Variables declared by a catch clause, a foreach statement, or a using statement. 9

12.3.2 Initially unassigned variables 10

The following categories of variables are classified as initially unassigned: 11

• Instance variables of initially unassigned struct variables. 12

• Output parameters, including the this variable of struct instance constructors without a constructor 13 initializer. 14

• Local variables, except those declared in a catch clause, a foreach statement, or a using statement. 15

12.3.3 Precise rules for determining definite assignment 16

In order to determine that each used variable is definitely assigned, the compiler shall use a process that is 17 equivalent to the one described in this subclause. 18

The compiler processes the body of each function member that has one or more initially unassigned 19 variables. For each initially unassigned variable v, the compiler determines a definite assignment state for v 20 at each of the following points in the function member: 21

• At the beginning of each statement 22

• At the end point (§15.1) of each statement 23

• On each arc which transfers control to another statement or to the end point of a statement 24

• At the beginning of each expression 25

• At the end of each expression 26

The definite assignment state of v can be either: 27

• Definitely assigned. This indicates that on all possible control flows to this point, v has been assigned a 28 value. 29

• Not definitely assigned. For the state of a variable at the end of an expression of type bool, the state of a 30 variable that isn’t definitely assigned might (but doesn’t necessarily) fall into one of the following sub-31 states: 32

o Definitely assigned after true expression. This state indicates that v is definitely assigned if the 33 boolean expression evaluated as true, but is not necessarily assigned if the boolean expression 34 evaluated as false. 35

o Definitely assigned after false expression. This state indicates that v is definitely assigned if the 36 boolean expression evaluated as false, but is not necessarily assigned if the boolean expression 37 evaluated as true. 38

The following rules govern how the state of a variable v is determined at each location. 39

Page 140: C# Language Specification

C# LANGUAGE SPECIFICATION

120

12.3.3.1 General rules for statements 1

• v is not definitely assigned at the beginning of a function member body. 2

• v is definitely assigned at the beginning of any unreachable statement. 3

• The definite assignment state of v at the beginning of any other statement is determined by checking the 4 definite assignment state of v on all control flow transfers that target the beginning of that statement. If 5 (and only if) v is definitely assigned on all such control flow transfers, then v is definitely assigned at the 6 beginning of the statement. The set of possible control flow transfers is determined in the same way as 7 for checking statement reachability (§15.1). 8

• The definite assignment state of v at the end point of a block, checked, unchecked, if, while, do, 9 for, foreach, lock, using, or switch statement is determined by checking the definite assignment 10 state of v on all control flow transfers that target the end point of that statement. If v is definitely 11 assigned on all such control flow transfers, then v is definitely assigned at the end point of the statement. 12 Otherwise, v is not definitely assigned at the end point of the statement. The set of possible control flow 13 transfers is determined in the same way as for checking statement reachability (§15.1). 14

12.3.3.2 Block statements, checked, and unchecked statements 15

The definite assignment state of v on the control transfer to the first statement of the statement list in the 16 block (or to the end point of the block, if the statement list is empty) is the same as the definite assignment 17 statement of v before the block, checked, or unchecked statement. 18

12.3.3.3 Expression statements 19

For an expression statement stmt that consists of the expression expr: 20

• v has the same definite assignment state at the beginning of expr as at the beginning of stmt. 21

• If v if definitely assigned at the end of expr, it is definitely assigned at the end point of stmt; otherwise, it 22 is not definitely assigned at the end point of stmt. 23

12.3.3.4 Declaration statements 24

• If stmt is a declaration statement without initializers, then v has the same definite assignment state at the 25 end point of stmt as at the beginning of stmt. 26

• If stmt is a declaration statement with initializers, then the definite assignment state for v is determined 27 as if stmt were a statement list, with one assignment statement for each declaration with an initializer (in 28 the order of declaration). 29

12.3.3.5 If statements 30

For an if statement stmt of the form: 31

if ( expr ) then-stmt else else-stmt 32

• v has the same definite assignment state at the beginning of expr as at the beginning of stmt. 33

• If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to 34 then-stmt and to either else-stmt or to the end-point of stmt if there is no else clause. 35

• If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely 36 assigned on the control flow transfer to then-stmt, and not definitely assigned on the control flow 37 transfer to either else-stmt or to the end-point of stmt if there is no else clause. 38

• If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely 39 assigned on the control flow transfer to else-stmt, and not definitely assigned on the control flow transfer 40 to then-stmt. It is definitely assigned at the end-point of stmt if and only if it is definitely assigned at the 41 end-point of then-stmt. 42

Page 141: C# Language Specification

Chapter 12 Variables

121

• Otherwise, v is considered not definitely assigned on the control flow transfer to either the then-stmt or 1 else-stmt, or to the end-point of stmt if there is no else clause. 2

12.3.3.6 Switch statements 3

In a switch statement stmt with a controlling expression expr: 4

• The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning 5 of stmt. 6

• The definite assignment state of v on the control flow transfer to a reachable switch block statement list 7 is the same as the definite assignment state of v at the end of expr. 8

12.3.3.7 While statements 9

For a while statement stmt of the form: 10

while ( expr ) while-body 11

• v has the same definite assignment state at the beginning of expr as at the beginning of stmt. 12

• If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to 13 while-body and to the end point of stmt. 14

• If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely 15 assigned on the control flow transfer to while-body, but not definitely assigned at the end-point of stmt. 16

• If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely 17 assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control 18 flow transfer to while-body. 19

12.3.3.8 Do statements 20

For a do statement stmt of the form: 21

do do-body while ( expr ) ; 22

• v has the same definite assignment state on the control flow transfer from the beginning of stmt to do-23 body as at the beginning of stmt. 24

• v has the same definite assignment state at the beginning of expr as at the end point of do-body. 25

• If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to 26 the end point of stmt. 27

• If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely 28 assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control 29 flow transfer to do-body. 30

12.3.3.9 For statements 31

Definite assignment checking for a for statement of the form: 32

for ( for-initializer ; for-condition ; for-iterator ) embedded-statement 33

is done as if the statement were written: 34

{ 35 for-initializer ; 36 while ( for-condition ) { 37 embedded-statement ; 38 LLoop: 39 for-iterator ; 40 } 41 } 42

Page 142: C# Language Specification

C# LANGUAGE SPECIFICATION

122

with continue statements that target the for statement being translated to goto statements targeting the 1 label LLoop. If the for-condition is omitted from the for statement, then evaluation of definite assignment 2 proceeds as if for-condition were replaced with true in the above expansion. 3

12.3.3.10 Break, continue, and goto statements 4

The definite assignment state of v on the control flow transfer caused by a break, continue, or goto 5 statement is the same as the definite assignment state of v at the beginning of the statement. 6

12.3.3.11 Throw statements 7

For a statement stmt of the form 8

throw expr ; 9

the definite assignment state of v at the beginning of expr is the same as the definite assignment state of v at 10 the beginning of stmt. 11

12.3.3.12 Return statements 12

For a statement stmt of the form 13

return expr ; 14

• The definite assignment state of v at the beginning of expr is the same as the definite assignment state of 15 v at the beginning of stmt. 16

• If v is an output parameter, then it shall be definitely assigned either: 17

o after expr 18

o or at the end of the finally block of a try-finally or try-catch-finally that encloses the 19 return statement. 20

For a statement stmt of the form: 21

return ; 22

• If v is an output parameter, then it shall be definitely assigned either: 23

o before stmt 24

o or at the end of the finally block of a try-finally or try-catch-finally that encloses the 25 return statement. 26

12.3.3.13 Try-catch statements 27

For a statement stmt of the form: 28

try try-block 29 catch ( … ) catch-block-1 30 … 31 catch ( … ) catch-block-n 32

• The definite assignment state of v at the beginning of try-block is the same as the definite assignment 33 state of v at the beginning of stmt. 34

• The definite assignment state of v at the beginning of catch-block-i (for any i) is the same as the definite 35 assignment state of v at the beginning of stmt. 36

• The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) v is 37 definitely assigned at the end-point of try-block and every catch-block-i (for every i from 1 to n). 38

12.3.3.14 Try-finally statements 39

For a try statement stmt of the form: 40

Page 143: C# Language Specification

Chapter 12 Variables

123

try try-block finally finally-block 1

• The definite assignment state of v at the beginning of try-block is the same as the definite assignment 2 state of v at the beginning of stmt. 3

• The definite assignment state of v at the beginning of finally-block is the same as the definite assignment 4 state of v at the beginning of stmt. 5

• The definite assignment state of v at the end-point of stmt is definitely assigned if (and only if) either: 6

o v is definitely assigned at the end-point of try-block 7

o v is definitely assigned at the end-point of finally-block 8

If a control flow transfer (such as a goto statement) is made that begins within try-block, and ends outside 9 of try-block, then v is also considered definitely assigned on that control flow transfer if v is definitely 10 assigned at the end-point of finally-block. (This is not an only if—if v is definitely assigned for another 11 reason on this control flow transfer, then it is still considered definitely assigned.) 12

12.3.3.15 Try-catch-finally statements 13

Definite assignment analysis for a try-catch-finally statement of the form: 14

try try-block 15 catch ( … ) catch-block-1 16 … 17 catch ( … ) catch-block-n 18 finally finally-block 19

is done as if the statement were a try-finally statement enclosing a try-catch statement: 20

try { 21 try try-block 22 catch ( … ) catch-block-1 23 … 24 catch ( … ) catch-block-n 25 } 26 finally finally-block 27

[Example: The following example demonstrates how the different blocks of a try statement (§15.10) affect 28 definite assignment. 29

class A 30 { 31 static void F() { 32 int i, j; 33 try { 34 goto LABEL; 35 // neither i nor j definitely assigned 36 i = 1; 37 // i definitely assigned 38 } 39

catch { 40 // neither i nor j definitely assigned 41 i = 3; 42 // i definitely assigned 43 } 44

Page 144: C# Language Specification

C# LANGUAGE SPECIFICATION

124

finally { 1 // neither i nor j definitely assigned 2 j = 5; 3 // j definitely assigned 4 } 5 // i and j definitely assigned 6 LABEL:; 7 // j definitely assigned 8 9 } 10 } 11

end example] 12

12.3.3.16 Foreach statements 13

For a foreach statement stmt of the form: 14

foreach ( type identifier in expr ) embedded-statement 15

• The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning 16 of stmt. 17

• The definite assignment state of v on the control flow transfer to embedded-statement or to the end point 18 of stmt is the same as the state of v at the end of expr. 19

12.3.3.17 Using statements 20

For a using statement stmt of the form: 21

using ( resource-acquisition ) embedded-statement 22

• The definite assignment state of v at the beginning of resource-acquisition is the same as the state of v at 23 the beginning of stmt. 24

• The definite assignment state of v on the control flow transfer to embedded-statement is the same as the 25 state of v at the end of resource-acquisition. 26

12.3.3.18 Lock statements 27

For a lock statement stmt of the form: 28

lock ( expr ) embedded-statement 29

• The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning 30 of stmt. 31

• The definite assignment state of v on the control flow transfer to embedded-statement is the same as the 32 state of v at the end of expr. 33

12.3.3.19 General rules for simple expressions 34

The following rule applies to these kinds of expressions: literals (§14.5.1), simple names (§14.5.2), member 35 access expressions (§14.5.4), non-indexed base access expressions (§14.5.8), and typeof expressions 36 (§14.5.11). 37

• The definite assignment state of v at the end of such an expression is the same as the definite assignment 38 state of v at the beginning of the expression. 39

12.3.3.20 General rules for expressions with embedded expressions 40

The following rules apply to these kinds of expressions: parenthesized expressions (§14.5.3), element access 41 expressions (§14.5.6), base access expressions with indexing (§14.5.8), increment and decrement 42 expressions (§14.5.9, §14.6.5), cast expressions (§14.6.6), unary +, -, ~, * expressions, binary +, -, *, /, %, 43 <<, >>, <, <=, >, >=, ==, !=, is, as, &, |, ^ expressions (§14.7, §14.8, §14.9, §14.10), compound 44

Page 145: C# Language Specification

Chapter 12 Variables

125

assignment expressions (§14.13.2), checked and unchecked expressions (§14.5.12), array and delegate 1 creation expressions (§14.5.10). 2

Each of these expressions has one or more sub-expressions that are unconditionally evaluated in a fixed 3 order. [Example: The binary % operator evaluates the left hand side of the operator, then the right hand side. 4 An indexing operation evaluates the indexed expression, and then evaluates each of the index expressions, in 5 order from left to right. end example] For an expression expr, which has sub-expressions expr1, expr2, ..., 6 exprn, evaluated in that order: 7

• The definite assignment state of v at the beginning of expr1 is the same as the definite assignment state at 8 the beginning of expr. 9

• The definite assignment state of v at the beginning of expri (i greater than one) is the same as the definite 10 assignment state at the end of expri-1. 11

• The definite assignment state of v at the end of expr is the same as the definite assignment state at the 12 end of exprn. 13

12.3.3.21 Invocation expressions and object creation expressions 14

For an invocation expression expr of the form: 15

primary-expression ( arg1, arg2, … , argn ) 16

or an object creation expression expr of the form: 17

new type ( arg1, arg2, … , argn ) 18

• For an invocation expression, the definite assignment state of v before primary-expression is the same as 19 the state of v before expr. 20

• For an invocation expression, the definite assignment state of v before arg1 is the same as the state of v 21 after primary-expression. 22

• For an object creation expression, the definite assignment state of v before arg1 is the same as the state 23 of v before expr. 24

• For each argument argi, the definite assignment state of v after argi is determined by the normal 25 expression rules, ignoring any ref or out modifiers. 26

• For each argument argi for any i greater than one, the definite assignment state of v before argi is the 27 same as the state of v after argi-1. 28

• If the variable v is passed as an out argument (i.e., an argument of the form “out v”) in any of the 29 arguments, then the state of v after expr is definitely assigned. Otherwise, the state of v after expr is the 30 same as the state of v after argn. 31

12.3.3.22 Simple assignment expressions 32

For an expression expr of the form w = expr-rhs: 33

• The definite assignment state of v before w is the same as the definite assignment state of v before expr. 34

• The definite assignment state of v before expr-rhs is the same as the definite assignment state of v after 35 w. 36

• If w is the same variable as v, then the definite assignment state of v after expr is definitely assigned. 37 Otherwise, the definite assignment state of v after expr is the same as the definite assignment state of v 38 after expr-rhs. 39

[Example: In the following code 40

Page 146: C# Language Specification

C# LANGUAGE SPECIFICATION

126

class A 1 { 2 static void F(int[] arr) { 3 int x; 4

arr[x = 1] = x; // ok 5 } 6 } 7

the variable x is considered definitely assigned after arr[x = 1] is evaluated as the left hand side of the 8 second simple assignment. end example] 9

12.3.3.23 && expressions 10

For an expression expr of the form expr-first && expr-second: 11

• The definite assignment state of v before expr-first is the same as the definite assignment state of v 12 before expr. 13

• The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-14 first is either definitely assigned or “definitely assigned after true expression”. Otherwise, it is not 15 definitely assigned. 16

• The definite assignment state of v after expr is determined by: 17

o If the state of v after expr-first is definitely assigned, then the state of v after expr is definitely 18 assigned. 19

o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first 20 is “definitely assigned after false expression”, then the state of v after expr is definitely assigned. 21

o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after true 22 expression”, then the state of v after expr is “definitely assigned after true expression”. 23

o Otherwise, if the state of v after expr-first is “definitely assigned after false expression”, and the 24 state of v after expr-second is “definitely assigned after false expression”, then the state of v after 25 expr is “definitely assigned after false expression”. 26

o Otherwise, the state of v after expr is not definitely assigned. 27

[Example: In the following code 28

class A 29 { 30 static void F(int x, int y) { 31 int i; 32 if (x >= 0 && (i = y) >= 0) { 33 // i definitely assigned 34 } 35 else { 36 // i not definitely assigned 37 } 38 // i not definitely assigned 39 } 40 } 41

the variable i is considered definitely assigned in one of the embedded statements of an if statement but not 42 in the other. In the if statement in method F, the variable i is definitely assigned in the first embedded 43 statement because execution of the expression (i = y) always precedes execution of this embedded 44 statement. In contrast, the variable i is not definitely assigned in the second embedded statement, since 45 x >= 0 might have tested false, resulting in the variable i’s being unassigned. end example] 46

12.3.3.24 || expressions 47

For an expression expr of the form expr-first || expr-second: 48

Page 147: C# Language Specification

Chapter 12 Variables

127

• The definite assignment state of v before expr-first is the same as the definite assignment state of v 1 before expr. 2

• The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-3 first is either definitely assigned or “definitely assigned after false expression”. Otherwise, it is not 4 definitely assigned. 5

• The definite assignment statement of v after expr is determined by: 6

o If the state of v after expr-first is definitely assigned, then the state of v after expr is definitely 7 assigned. 8

o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first 9 is “definitely assigned after true expression”, then the state of v after expr is definitely assigned. 10

o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after false 11 expression”, then the state of v after expr is “definitely assigned after false expression”. 12

o Otherwise, if the state of v after expr-first is “definitely assigned after true expression”, and the state 13 of v after expr-second is “definitely assigned after true expression”, then the state of v after expr is 14 “definitely assigned after true expression”. 15

o Otherwise, the state of v after expr is not definitely assigned. 16

[Example: In the following code 17

class A 18 { 19 static void G(int x, int y) { 20 int i; 21 if (x >= 0 || (i = y) >= 0) { 22 // i not definitely assigned 23 } 24 else { 25 // i definitely assigned 26 } 27 // i not definitely assigned 28 } 29 } 30

the variable i is considered definitely assigned in one of the embedded statements of an if statement but not 31 in the other. In the if statement in method G, the variable i is definitely assigned in the second embedded 32 statement because execution of the expression (i = y) always precedes execution of this embedded 33 statement. In contrast, the variable i is not definitely assigned in the first embedded statement, since 34 x >= 0 might have tested true, resulting in the variable i's being unassigned. end example] 35

12.3.3.25 ! expressions 36

For an expression expr of the form ! expr-operand: 37

• The definite assignment state of v before expr-operand is the same as the definite assignment state of v 38 before expr. 39

• The definite assignment state of v after expr is determined by: 40

o If the state of v after expr-operand is definitely assigned, then the state of v after expr is definitely 41 assigned. 42

o If the state of v after expr-operand is not definitely assigned, then the state of v after expr is not 43 definitely assigned. 44

o If the state of v after expr-operand is “definitely assigned after false expression”, then the state of v 45 after expr is “definitely assigned after true expression”. 46

o If the state of v after expr-operand is “definitely assigned after true expression”, then the state of v 47 after expr is “definitely assigned after false expression”. 48

Page 148: C# Language Specification

C# LANGUAGE SPECIFICATION

128

12.3.3.26 ?: expressions 1

For an expression expr of the form expr-cond ? expr-true : expr-false: 2

• The definite assignment state of v before expr-cond is the same as the state of v before expr. 3

• The definite assignment state of v before expr-true is definitely assigned if and only if the state of v after 4 expr-cond is definitely assigned or “definitely assigned after true expression”. 5

• The definite assignment state of v before expr-false is definitely assigned if and only if the state of v 6 after expr-cond is definitely assigned or “definitely assigned after false expression”. 7

• The definite assignment state of v after expr is determined by: 8

o If expr-cond is a constant expression (§14.15) with value true then the state of v after expr is the 9 same as the state of v after expr-true. 10

o Otherwise, if expr-cond is a constant expression (§14.15) with value false then the state of v after 11 expr is the same as the state of v after expr-false. 12

o Otherwise, if the state of v after expr-true is definitely assigned and the state of v after expr-false is 13 definitely assigned, then the state of v after expr is definitely assigned. 14

o Otherwise, the state of v after expr is not definitely assigned. 15

12.3.3.27 Anonymous method expressions 16

The definite assignment state of a parameter of an anonymous method (§14.5.14) is the same as for a 17 parameter of a named method. That is, reference parameters and value parameters are initially definitely 18 assigned and output parameters are initially unassigned. Furthermore, output parameters shall be definitely 19 assigned before the anonymous method returns normally (§12.1.6). 20

The definite assignment state of an outer variable v on the control transfer to the block of an anonymous-21 method-expression is the same as the definite assignment state of v before the anonymous-method-22 expression. That is, definite assignment of outer variables is inherited from the context of the anonymous-23 method-expression. Within the block of an anonymous-method-expression, definite assignment evolves as in 24 a normal block (§12.3.3). 25

The definite assignment state of a variable v after an anonymous-method-expression is the same as its 26 definite assignment state before the anonymous-method-expression. 27

[Example: The example 28

delegate bool Filter(int i); 29

void F() { 30 int max; 31

// Error, max is not definitely assigned 32 Filter f = delegate(int n) { return n < max; } 33

max = 5; 34 DoWork(f); 35 } 36

generates a compile-time error since max is not definitely assigned where the anonymous method is 37 declared. end example] [Example: The example 38

delegate void D(); 39

void F() { 40 int n; 41 D d = delegate { n = 1; }; 42

d(); 43

// Error, n is not definitely assigned 44 Console.WriteLine(n); 45 } 46

Page 149: C# Language Specification

Chapter 12 Variables

129

also generates a compile-time error since the assignment to n in the anonymous method has no affect on the 1 definite assignment state of n outside the anonymous method. end example] 2

12.3.3.28 Yield statements 3

For a yield return statement stmt of the form: 4

yield return expr ; 5

• A variable v has the same definite assignment state at the beginning of expr as at the beginning of stmt. 6

• If a variable v is definitely assigned at the end of expr, it is definitely assigned at the end point of stmt; 7 otherwise, it is not definitely assigned at the end point of stmt. 8

12.4 Variable references 9

A variable-reference is an expression that is classified as a variable. A variable-reference denotes a storage 10 location that can be accessed both to fetch the current value and to store a new value. 11

variable-reference: 12 expression 13

[Note: In C and C++, a variable-reference is known as an lvalue. end note] 14

12.5 Atomicity of variable references 15

Reads and writes of the following data types shall be atomic: bool, char, byte, sbyte, short, ushort, 16 uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type 17 in the previous list shall also be atomic. Reads and writes of other types, including long, ulong, double, 18 and decimal, as well as user-defined types, need not be atomic. Aside from the library functions designed 19 for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or 20 decrement. 21

Page 150: C# Language Specification
Page 151: C# Language Specification

Chapter 13 Conversions

131

13. Conversions 1

A conversion enables an expression of one type to be treated as another type. Conversions can be implicit or 2 explicit, and this determines whether an explicit cast is required. [Example: For instance, the conversion 3 from type int to type long is implicit, so expressions of type int can implicitly be treated as type long. 4 The opposite conversion, from type long to type int, is explicit and so an explicit cast is required. 5

int a = 123; 6 long b = a; // implicit conversion from int to long 7 int c = (int) b; // explicit conversion from long to int 8

end example] Some conversions are defined by the language. Programs can also define their own 9 conversions (§13.4). 10

13.1 Implicit conversions 11

The following conversions are classified as implicit conversions: 12

• Identity conversions 13

• Implicit numeric conversions 14

• Implicit enumeration conversions. 15

• Implicit reference conversions 16

• Boxing conversions 17

• Implicit type parameter conversions 18

• Implicit constant expression conversions 19

• User-defined implicit conversions 20

• Implicit conversions from an anonymous method expression to a compatible delegate type 21

• Implicit conversion from a method group to a compatible delegate type 22

Implicit conversions can occur in a variety of situations, including function member invocations (§14.4.3), 23 cast expressions (§14.6.6), and assignments (§14.13). 24

The pre-defined implicit conversions always succeed and never cause exceptions to be thrown. [Note: 25 Properly designed user-defined implicit conversions should exhibit these characteristics as well. end note] 26

13.1.1 Identity conversion 27

An identity conversion converts from any type to the same type. This conversion exists only such that an 28 entity that already has a required type can be said to be convertible to that type. 29

13.1.2 Implicit numeric conversions 30

The implicit numeric conversions are: 31

• From sbyte to short, int, long, float, double, or decimal. 32

• From byte to short, ushort, int, uint, long, ulong, float, double, or decimal. 33

• From short to int, long, float, double, or decimal. 34

• From ushort to int, uint, long, ulong, float, double, or decimal. 35

Page 152: C# Language Specification

C# LANGUAGE SPECIFICATION

132

• From int to long, float, double, or decimal. 1

• From uint to long, ulong, float, double, or decimal. 2

• From long to float, double, or decimal. 3

• From ulong to float, double, or decimal. 4

• From char to ushort, int, uint, long, ulong, float, double, or decimal. 5

• From float to double. 6

Conversions from int, uint, long or ulong to float and from long or ulong to double can cause a 7 loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never 8 lose any information. 9

There are no implicit conversions to the char type, so values of the other integral types do not automatically 10 convert to the char type. 11

13.1.3 Implicit enumeration conversions 12

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type. 13

13.1.4 Implicit reference conversions 14

The implicit reference conversions are: 15

• From any reference-type to object. 16

• From any class-type S to any class-type T, provided S is derived from T. 17

• From any class-type S to any interface-type T, provided S implements T. 18

• From any interface-type S to any interface-type T, provided S is derived from T. 19

• From an array-type S with an element type SE to an array-type T with an element type TE, provided all 20 of the following are true: 21

o S and T differ only in element type. In other words, S and T have the same number of dimensions. 22

o An implicit reference conversion exists from SE to TE. 23

• From a one-dimensional array-type S[] to System.Collections.Generic.IList<S> and base 24 interfaces of this interface. 25

• From a one-dimensional array-type S[] to System.Collections.Generic.IList<T> and base 26 interfaces of this interface, provided there is an implicit reference conversion from S to T. 27

• From any array-type to System.Array. 28

• From any delegate-type to System.Delegate. 29

• From any array-type to any interface implemented by System.Array. 30

• From any delegate-type to System.ICloneable. 31

• From the null type to any reference-type. 32

For a type-parameter T that is known to be a reference type (§26.7), the following implicit reference 33 conversions exist: 34

• From T to its effective base class C, from T to any base class of C, and from T to any interface 35 implemented by C. 36

• From T to an interface-type I in T’s effective interface set and from T to any base interface of I. 37

Page 153: C# Language Specification

Chapter 13 Conversions

133

• From T to a type parameter U provided that T depends on U (§26.7). [Note: Since T is known to be a 1 reference type, within the scope of T, the run-time type of U will always be a reference type, even if U is 2 not known to be a reference type at compile-time. end note] 3

• From the null type to T. 4

The implicit reference conversions are those conversions between reference-types that can be proven to 5 always succeed, and therefore require no checks at run-time. 6

Reference conversions, implicit or explicit, never change the referential identity of the object being 7 converted. [Note: In other words, while a reference conversion can change the type of the reference, it never 8 changes the type or value of the object being referred to. end note] 9

13.1.5 Boxing conversions 10

A boxing conversion permits any value-type to be implicitly converted to the type object or 11 System.ValueType or to any interface-type implemented by the value-type, and any enum type to be 12 implicitly converted to System.Enum as well. Boxing a value of a value-type consists of allocating an 13 object instance and copying the value-type value into that instance. An enum can be boxed to the type 14 System.Enum, since that is the direct base class for all enums (§21.4). A struct or enum can be boxed to the 15 type System.ValueType, since that is the direct base class for all structs (§18.3.2) and a base class for all 16 enums. 17

For a type-parameter T that is not known to be a reference type (§26.7), the following conversions involving 18 T are considered to be boxing conversions at compile-time. At run-time, if T is a value type, the conversion 19 is executed as a boxing conversion. At run-time, if T is a reference type, the conversion is executed as an 20 implicit reference conversion or identity conversion. 21

• From T to its effective base class C, from T to any base class of C, and from T to any interface 22 implemented by C. [Note: C will be one of the types System.Object, System.ValueType, or 23 System.Enum (otherwise T would be known to be a reference type and §13.1.4 would apply instead of 24 this clause). end note] 25

• From T to an interface-type I in T’s effective interface set and from T to any base interface of I. 26

Boxing conversions are described further in §11.3.1. 27

13.1.6 Implicit type parameter conversions 28

This clause details implicit conversions involving type parameters that are not classified as implicit 29 reference conversions or implicit boxing conversions. 30

For a type-parameter T that is not known to be a reference type, there is an implicit conversion from T to a 31 type parameter U provided T depends on U. At run-time, if T is a value type and U is a reference type, the 32 conversion is executed as a boxing conversion. At run-time, if both T and U are value types, then T and U are 33 necessarily the same type and no conversion is performed. At run-time, if T is a reference type, then U is 34 necessarily also a reference type and the conversion is executed as an implicit reference conversion or 35 identity conversion (§26.7). 36

13.1.7 Implicit constant expression conversions 37

An implicit constant expression conversion permits the following conversions: 38

• A constant-expression (§14.15) of type int can be converted to type sbyte, byte, short, ushort, 39 uint, or ulong, provided the value of the constant-expression is within the range of the destination 40 type. 41

• A constant-expression of type long can be converted to type ulong, provided the value of the constant-42 expression is not negative. 43

Page 154: C# Language Specification

C# LANGUAGE SPECIFICATION

134

13.1.8 User-defined implicit conversions 1

A user-defined implicit conversion consists of an optional standard implicit conversion, followed by 2 execution of a user-defined implicit conversion operator, followed by another optional standard implicit 3 conversion. The exact rules for evaluating user-defined conversions are described in §13.4.3. 4

13.2 Explicit conversions 5

The following conversions are classified as explicit conversions: 6

• All implicit conversions. 7

• Explicit numeric conversions. 8

• Explicit enumeration conversions. 9

• Explicit reference conversions. 10

• Explicit interface conversions. 11

• Unboxing conversions. 12

• Explicit type parameter conversions. 13

• User-defined explicit conversions. 14

Explicit conversions can occur in cast expressions (§14.6.6). 15

The set of explicit conversions includes all implicit conversions. [Note: This means that redundant cast 16 expressions are allowed. end note] 17

The explicit conversions that are not implicit conversions are conversions that cannot be proven to always 18 succeed, conversions that are known to possibly lose information, and conversions across domains of types 19 sufficiently different to merit explicit notation. 20

13.2.1 Explicit numeric conversions 21

The explicit numeric conversions are the conversions from a numeric-type to another numeric-type for 22 which an implicit numeric conversion (§13.1.2) does not already exist: 23

• From sbyte to byte, ushort, uint, ulong, or char. 24

• From byte to sbyte or char. 25

• From short to sbyte, byte, ushort, uint, ulong, or char. 26

• From ushort to sbyte, byte, short, or char. 27

• From int to sbyte, byte, short, ushort, uint, ulong, or char. 28

• From uint to sbyte, byte, short, ushort, int, or char. 29

• From long to sbyte, byte, short, ushort, int, uint, ulong, or char. 30

• From ulong to sbyte, byte, short, ushort, int, uint, long, or char. 31

• From char to sbyte, byte, or short. 32

• From float to sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal. 33

• From double to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal. 34

• From decimal to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double. 35

Because the explicit conversions include all implicit and explicit numeric conversions, it is always possible 36 to convert from any numeric-type to any other numeric-type using a cast expression (§14.6.6). 37

Page 155: C# Language Specification

Chapter 13 Conversions

135

The explicit numeric conversions possibly lose information or possibly cause exceptions to be thrown. An 1 explicit numeric conversion is processed as follows: 2

• For a conversion from an integral type to another integral type, the processing depends on the overflow 3 checking context (§14.5.12) in which the conversion takes place: 4

o In a checked context, the conversion succeeds if the value of the source operand is within the range 5 of the destination type, but throws a System.OverflowException if the value of the source 6 operand is outside the range of the destination type. 7

o In an unchecked context, the conversion always succeeds, and proceeds as follows. 8

• If the source type is larger than the destination type, then the source value is truncated by 9 discarding its “extra” most significant bits. The result is then treated as a value of the destination 10 type. 11

• If the source type is smaller than the destination type, then the source value is either sign-12 extended or zero-extended so that it is the same size as the destination type. Sign-extension is 13 used if the source type is signed; zero-extension is used if the source type is unsigned. The result 14 is then treated as a value of the destination type. 15

• If the source type is the same size as the destination type, then the source value is treated as a 16 value of the destination type 17

• For a conversion from decimal to an integral type, the source value is rounded towards zero to the 18 nearest integral value, and this integral value becomes the result of the conversion. If the resulting 19 integral value is outside the range of the destination type, a System.OverflowException is thrown. 20

• For a conversion from float or double to an integral type, the processing depends on the overflow-21 checking context (§14.5.12) in which the conversion takes place: 22

o In a checked context, the conversion proceeds as follows: 23

• The value is rounded towards zero to the nearest integral value. If this integral value is within 24 the range of the destination type, then this value is the result of the conversion. 25

• Otherwise, a System.OverflowException is thrown. 26

o In an unchecked context, the conversion always succeeds, and proceeds as follows. 27

• The value is rounded towards zero to the nearest integral value. If this integral value is within 28 the range of the destination type, then this value is the result of the conversion. 29

• Otherwise, the result of the conversion is an unspecified value of the destination type. 30

• For a conversion from double to float, the double value is rounded to the nearest float value. This 31 rounding may cause a non-zero value to be rounded to a zero value of the same sign. If the magnitude of 32 the double value is too large to represent as a float, the result becomes positive infinity or negative 33 infinity. If the double value is NaN, the result is also NaN. 34

• For a conversion from float or double to decimal, the source value is converted to decimal 35 representation and rounded to the nearest number after the 28th decimal place if required (§11.1.7). This 36 rounding may cause a non-zero value to be rounded to zero. If the source value is NaN, infinite, or its 37 magnitude is too large to represent as a decimal, a System.OverflowException is thrown. 38

• For a conversion from decimal to float or double, the decimal value is rounded to the nearest 39 double or float value. However, if the value being converted is not within the range of the destination 40 type, a System.OverflowException is thrown. 41

13.2.2 Explicit enumeration conversions 42

The explicit enumeration conversions are: 43

Page 156: C# Language Specification

C# LANGUAGE SPECIFICATION

136

• From sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal to 1 any enum-type. 2

• From any enum-type to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, 3 double, or decimal. 4

• From any enum-type to any other enum-type. 5

An explicit enumeration conversion between two types is processed by treating any participating enum-type 6 as the underlying type of that enum-type, and then performing an implicit or explicit numeric conversion 7 between the resulting types. [Example: Given an enum-type E with and underlying type of int, a conversion 8 from E to byte is processed as an explicit numeric conversion (§13.2.1) from int to byte, and a 9 conversion from byte to E is processed as an implicit numeric conversion (§13.1.2) from byte to int. end 10 example] 11

13.2.3 Explicit reference conversions 12

The explicit reference conversions are: 13

• From object to any reference-type. 14

• From any class-type S to any class-type T, provided S is a base class of T. 15

• From any class-type S to any interface-type T, provided S is not sealed and provided S does not 16 implement T. 17

• From any interface-type S to any class-type T, provided T is not sealed or provided T implements S. 18

• `From any interface-type S to any interface-type T, provided S is not derived from T. 19

• From an array-type S with an element type SE to an array-type T with an element type TE, provided all 20 of the following are true: 21

o S and T differ only in element type. (In other words, S and T have the same number of dimensions.) 22

o An explicit reference conversion exists from SE to TE. 23

• From System.Array and the interfaces it implements, to any array-type. 24

• From System.Delegate and the interfaces it implements, to any delegate-type. 25

• From a one-dimensional array-type S[] to System.Collections.Generic.IList<T> and its base 26 interfaces, provided there is an explicit reference conversion from S to T. 27

• From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional 28 array-type S[], provided there is an implicit or explicit reference conversion from S[] to 29 System.Collections.Generic.IList<T>. This is precisely when either S and T are the same type 30 or there is an implicit or explicit reference conversion from S to T. 31

For a type-parameter T that is known to be a reference type (§26.7), the following explicit reference 32 conversions exist: 33

• From the effective base class C of T to T and from any base class of C to T. 34

• From any interface-type to T. 35

• From T to any interface-type I provided there isn’t already an implicit reference conversion from T to I. 36

• From a type-parameter U to T provided that T depends on U (§26.7). [Note: Since T is known to be a 37 reference type, within the scope of T, the run-time type of U will always be a reference type, even if U is 38 not known to be a reference type at compile-time. end note] 39

The explicit reference conversions are those conversions between reference-types that require run-time 40 checks to ensure they are correct. 41

Page 157: C# Language Specification

Chapter 13 Conversions

137

For an explicit reference conversion to succeed at run-time, the value of the source operand shall be null, 1 or the actual type of the object referenced by the source operand shall be a type that can be converted to the 2 destination type by an implicit reference conversion (§13.1.4). If an explicit reference conversion fails, a 3 System.InvalidCastException is thrown. 4

Reference conversions, implicit or explicit, never change the referential identity of the object being 5 converted. [Note: In other words, while a reference conversion can change the type of the reference, it never 6 changes the type or value of the object being referred to. end note] 7

13.2.4 Unboxing conversions 8

An unboxing conversion permits an explicit conversion from type object or System.ValueType to any 9 value-type, or from any interface-type to any value-type that implements the interface-type, and from the 10 type System.Enum to any enumeration type. An unboxing operation consists of first checking that the 11 object instance is a boxed value of the given value-type or enumeration type, and then copying the value out 12 of the instance. An enum can be unboxed from the type System.Enum, since that is the direct base class for 13 all enum types (§21.4). A struct or enum can be unboxed from the type System.ValueType, since that is 14 the direct base class for all structs (§18.3.2) and a base class for all enums. 15

For a type-parameter T that is not known to be a reference type (§26.7), the following conversions involving 16 T are considered to be unboxing conversions at compile-time. At run-time, if T is a value type, the 17 conversion is executed as an unboxing conversion. At run-time, if T is a reference type, the conversion is 18 executed as an explicit reference conversion or identity conversion. 19

• From the effective base class C of T to T and from any base class of C to T. [Note: C will be one of the 20 types System.Object, System.ValueType, or System.Enum (otherwise T would be known to be a 21 reference type and §13.2.3 would apply instead of this clause). end note] 22

• From any interface-type to T. 23

13.2.5 Unboxing conversions are described further in §11.3.2.Explicit type parameter 24 conversions 25

This clause details explicit conversions involving type parameters that are not classified as explicit reference 26 conversions or explicit unboxing conversions. 27

For a type-parameter T that is not known to be a reference type (§26.7), the following explicit conversions 28 exist: 29

• From T to any interface-type I provided there is not already an implicit conversion from T to I. This 30 conversion consists of an implicit boxing conversion (§13.1.5) from T to object followed by an 31 explicit reference conversion from object to I. At run-time, if T is a value type, the conversion is 32 executed as a boxing conversion followed by an explicit reference conversion. At run-time, if T is a 33 reference type, the conversion is executed as an explicit reference conversion. 34

• From a type parameter U to T provided that T depends on U (§26.7). At run-time, if T is a value type and 35 U is a reference type, the conversion is executed as an unboxing conversion. At run-time, if both T and U 36 are value types, then T and U are necessarily the same type and no conversion is performed. At run-time, 37 if T is a reference type, then U is necessarily also a reference type and the conversion is executed as an 38 explicit reference conversion or identity conversion. 39

13.2.6 User-defined explicit conversions 40

A user-defined explicit conversion consists of an optional standard explicit conversion, followed by 41 execution of a user-defined implicit or explicit conversion operator, followed by another optional standard 42 explicit conversion. The exact rules for evaluating user-defined conversions are described in §13.4.4. 43

Page 158: C# Language Specification

C# LANGUAGE SPECIFICATION

138

13.3 Standard conversions 1

The standard conversions are those pre-defined conversions that can occur as part of a user-defined 2 conversion. 3

13.3.1 Standard implicit conversions 4

The following implicit conversions are classified as standard implicit conversions: 5

• Identity conversions (§13.1.1) 6

• Implicit numeric conversions (§13.1.2) 7

• Implicit reference conversions (§13.1.4) 8

• Boxing conversions (§13.1.5) 9

• Implicit type parameter conversions (§13.1.6) 10

• Implicit constant expression conversions (§13.1.7) 11

The standard implicit conversions specifically exclude user-defined implicit conversions. 12

13.3.2 Standard explicit conversions 13

The standard explicit conversions are all standard implicit conversions plus the subset of the explicit 14 conversions for which an opposite standard implicit conversion exists. [Note: In other words, if a standard 15 implicit conversion exists from a type A to a type B, then a standard explicit conversion exists from type A to 16 type B and from type B to type A. end note] 17

13.4 User-defined conversions 18

C# allows the pre-defined implicit and explicit conversions to be augmented by user-defined conversions. 19 User-defined conversions are introduced by declaring conversion operators (§17.9.3) in class and struct 20 types. 21

13.4.1 Permitted user-defined conversions 22

C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine 23 an already existing implicit or explicit conversion. A class or struct is permitted to declare a conversion 24 operator from a source type S to a target type T only if all of the following are true: 25

• S and T are different types. 26

• Either S or T is the class or struct type in which the operator declaration takes place. 27

• Neither S nor T is object or an interface-type. 28

• T is not a base class of S, and S is not a base class of T. 29

The restrictions that apply to user-defined conversions are discussed further in §17.9.3. 30

13.4.2 Evaluation of user-defined conversions 31

A user-defined conversion converts a value from its type, called the source type, to another type, called the 32 target type. Evaluation of a user-defined conversion centers on finding the most specific user-defined 33 conversion operator for the particular source and target types. This determination is broken into several 34 steps: 35

• Finding the set of classes and structs from which user-defined conversion operators will be considered. 36 This set consists of the source type and its base classes and the target type and its base classes (with the 37 implicit assumptions that only classes and structs can declare user-defined operators, and that non-class 38 types have no base classes). 39

Page 159: C# Language Specification

Chapter 13 Conversions

139

• From that set of types, determining which user-defined conversion operators are applicable. For a 1 conversion operator to be applicable, it shall be possible to perform a standard conversion (§13.3) from 2 the source type to the operand type of the operator, and it shall be possible to perform a standard 3 conversion from the result type of the operator to the target type. If the set of applicable user-defined 4 conversion operators is empty then there is no user-defined conversion from the source type to the target 5 type. 6

• From the set of applicable user-defined operators, determining which operator is unambiguously the 7 most specific. In general terms, the most specific operator is the operator whose operand type is 8 “closest” to the source type and whose result type is “closest” to the target type. The exact rules for 9 establishing the most specific user-defined conversion operator are defined in the following subclauses. 10

For the purposes of overload resolution, a user-defined conversion from the source type to the target type 11 exists if and only if the set of applicable user-defined conversion operators is non-empty. If the set of 12 applicable operators is non-empty but does not contain a unique most specific operator, the user-defined 13 conversion is deemed to exist even though application of the conversion will always produce a compile-time 14 error. 15

Once a most specific user-defined conversion operator has been identified, the actual execution of the user-16 defined conversion involves up to three steps: 17

• First, if required, performing a standard conversion from the source type to the operand type of the user-18 defined conversion operator. 19

• Next, invoking the user-defined conversion operator to perform the conversion. 20

• Finally, if required, performing a standard conversion from the result type of the user-defined 21 conversion operator to the target type. 22

Evaluation of a user-defined conversion never involves more than one user-defined conversion operator. In 23 other words, a conversion from type S to type T will never first execute a user-defined conversion from S to 24 X and then execute a user-defined conversion from X to T. 25

Exact definitions of evaluation of user-defined implicit or explicit conversions are given in the following 26 subclauses. The definitions make use of the following terms: 27

• If a standard implicit conversion (§13.3.1) exists from a type A to a type B, and if neither A nor B are 28 interface-types, then A is said to be encompassed by B, and B is said to encompass A. 29

• The most encompassing type in a set of types is the one type that encompasses all other types in the set. 30 If no single type encompasses all other types, then the set has no most encompassing type. In more 31 intuitive terms, the most encompassing type is the “largest” type in the set—the one type to which each 32 of the other types can be implicitly converted. 33

• The most encompassed type in a set of types is the one type that is encompassed by all other types in the 34 set. If no single type is encompassed by all other types, then the set has no most encompassed type. In 35 more intuitive terms, the most encompassed type is the “smallest” type in the set—the one type that can 36 be implicitly converted to each of the other types. 37

13.4.3 User-defined implicit conversions 38

Processing of a user-defined implicit conversion from type S to type T takes one of two forms: 39

• Testing whether a user defined conversion exists from S to T. This is used by overload resolution to 40 determine whether a function member is applicable. 41

• Applying the user defined conversion from S to T. This requires that the existence of the user defined 42 conversion has been verified. 43

To test whether a user-defined implicit conversion from S to T exists: 44

Page 160: C# Language Specification

C# LANGUAGE SPECIFICATION

140

• Find the set of types, D, from which user-defined conversion operators will be considered. This set 1 consists of S (if S is a class or struct), the base classes of S (if S is a class), T (if T is a class or struct), 2 and the base classes of T (if T is a class). 3

• Find the set of applicable user-defined conversion operators, U. This set consists of the user-defined 4 implicit conversion operators declared by the classes or structs in D that convert from a type 5 encompassing S to a type encompassed by T. 6

The user-defined conversion from S to T exists if and only if U is not empty. 7

To apply the user-defined conversion from S to T: 8

• Construct the set of applicalbe user-defined conversion operators U as above. 9

• Find the most specific source type, SX, of the operators in U: 10

o If any of the operators in U convert from S, then SX is S. 11

o Otherwise, SX is the most encompassed type in the combined set of source types of the operators 12 in U. If no most encompassed type can be found, then the conversion is ambiguous and a compile-13 time error occurs. 14

• Find the most specific target type, TX, of the operators in U: 15

o If any of the operators in U convert to T, then TX is T. 16

o Otherwise, TX is the most encompassing type in the combined set of target types of the operators 17 in U. If no most encompassing type can be found, then the conversion is ambiguous and a compile-18 time error occurs. 19

• If U contains exactly one user-defined conversion operator that converts from SX to TX, then this is the 20 most specific conversion operator. If no such operator exists, or if more than one such operator exists, 21 then the conversion is ambiguous and a compile-time error occurs. Otherwise, the user-defined 22 conversion is applied: 23

o If S is not SX, then a standard implicit conversion from S to SX is performed. 24

o The most specific user-defined conversion operator is invoked to convert from SX to TX. 25

o If TX is not T, then a standard implicit conversion from TX to T is performed. 26

13.4.4 User-defined explicit conversions 27

A user-defined explicit conversion from type S to type T is processed as follows: 28

• Find the set of types, D, from which user-defined conversion operators will be considered. This set 29 consists of S (if S is a class or struct), the base classes of S (if S is a class), T (if T is a class or struct), 30 and the base classes of T (if T is a class). 31

• Find the set of applicable user-defined conversion operators, U. This set consists of the user-defined 32 implicit or explicit conversion operators declared by the classes or structs in D that convert from a type 33 encompassing or encompassed by S to a type encompassing or encompassed by T. If U is empty, the 34 conversion is undefined and a compile-time error occurs. 35

• Find the most specific source type, SX, of the operators in U: 36

o If any of the operators in U convert from S, then SX is S. 37

o Otherwise, if any of the operators in U convert from types that encompass S, then SX is the most 38 encompassed type in the combined set of source types of those operators. If no most encompassed 39 type can be found, then the conversion is ambiguous and a compile-time error occurs. 40

o Otherwise, SX is the most encompassing type in the combined set of source types of the operators 41 in U. If no most encompassing type can be found, then the conversion is ambiguous and a compile-42 time error occurs. 43

Page 161: C# Language Specification

Chapter 13 Conversions

141

• Find the most specific target type, TX, of the operators in U: 1

o If any of the operators in U convert to T, then TX is T. 2

o Otherwise, if any of the operators in U convert to types that are encompassed by T, then TX is the 3 most encompassing type in the combined set of target types of those operators. If no most 4 encompassing type can be found, then the conversion is ambiguous and a compile-time error occurs. 5

o Otherwise, TX is the most encompassed type in the combined set of target types of the operators in U. 6 If no most encompassed type can be found, then the conversion is ambiguous and a compile-time 7 error occurs. 8

• If U contains exactly one user-defined conversion operator that converts from SX to TX, then this is the 9 most specific conversion operator. If no such operator exists, or if more than one such operator exists, 10 then the conversion is ambiguous and a compile-time error occurs. Otherwise, the user-defined 11 conversion is applied: 12

o If S is not SX, then a standard explicit conversion from S to SX is performed. 13

o The most specific user-defined conversion operator is invoked to convert from SX to TX. 14

o If TX is not T, then a standard explicit conversion from TX to T is performed. 15

13.5 Anonymous method conversions 16

An implicit conversion (§13.1) exists from an anonymous-method-expression to any compatible delegate 17 type. If D is a delegate type, and A is an anonymous-method-expression, then D is compatible with A if and 18 only if the following two conditions are met. 19

• First, the parameter types of D shall be compatible with A: 20

o If A does not contain an anonymous-method-signature, then D can have zero or more parameters of 21 any type, as long as no parameter of D has the out parameter modifier. 22

o If A has an anonymous-method-signature, then D shall have the same number of parameters and each 23 parameter of A shall be compatible with the corresponding parameter of D. A parameter of A is 24 considered compatible with a parameter of D if they are both of the same type and the presence or 25 absence of the ref or out modifier on the parameter of A matches the corresponding parameter of 26 D. Whether the final parameter of D is a parameter-array is not considered when determining the 27 compatibility of A and D. A parameter which has the parameter-array modifier is compatible with a 28 parameter without the parameter-array modifier if they are both of the same type. 29

• Second, the return type of D shall be compatible with A. For these rules, A is not considered to contain 30 the block of any other anonymous methods: 31

o If D is declared with a void return type, then any return statement contained in A shall not specify 32 an expression. 33

o If D is declared with a return type of R, then any return statement contained in A shall specify an 34 expression which is implicitly convertible (§13.1) to R. Furthermore, the end-point of the block of A 35 shall not be reachable. 36

Besides the implicit conversions to compatible delegate types, no other conversions exist from an 37 anonymous-method-expression, not even to the type object. 38

[Example: The following examples illustrate these rules: 39

delegate void D(int x); 40

D d1 = delegate { }; // Ok 41 D d2 = delegate() { }; // Error, signature mismatch 42 D d3 = delegate(long x) { }; // Error, signature mismatch 43 D d4 = delegate(int x) { }; // Ok 44 D d5 = delegate(int x) { return; }; // Ok 45 D d6 = delegate(int x) { return x; }; // Error, return type mismatch 46

Page 162: C# Language Specification

C# LANGUAGE SPECIFICATION

142

delegate void E(out int x); 1

E e1 = delegate { }; // Error, E has an out parameter 2 E e2 = delegate(out int x) { x = 1; }; // Ok 3 E e3 = delegate(ref int x) { x = 1; }; // Error, signature mismatch 4

delegate int P(params int[] a); 5

P p1 = delegate { }; // Error, end of block reachable 6 P p2 = delegate { return; }; // Error, return type mismatch 7 P p3 = delegate { return 1; }; // Ok 8 P p4 = delegate { return "Hello"; }; // Error, return type mismatch 9 P p5 = delegate(int[] a) { // Ok 10 return a[0]; 11 }; 12 P p6 = delegate(params int[] a) { // Error, params modifier 13 return a[0]; 14 }; 15 P p7 = delegate(int[] a) { // Error, return type mismatch 16 if (a.Length > 0) return a[0]; 17 return "Hello"; 18 }; 19

delegate object Q(params int[] a); 20

Q q1 = delegate(int[] a) { // Ok 21 if (a.Length > 0) return a[0]; 22 return "Hello"; 23 }; 24

end example] 25

A delegate-creation-expression (§14.5.10.3) can be used as an alternate syntax for converting an anonymous 26 method to a delegate type. 27

13.6 Method group conversions 28

Similar to the implicit anonymous method conversions described in §13.5, an implicit conversion exists 29 from a method group (§14.1) to a compatible delegate type. If D is a delegate type, and E is an expression 30 that is classified as a method group, then D is compatible with E if and only if E contains at least one method 31 that is applicable in its normal form (§14.4.2.1) to any argument list (§14.4.1) having types and modifiers 32 matching the parameter types and modifiers of D. 33

The compile-time application of the conversion from E to D is the same as the compile-time processing of 34 the delegate creation expression new D(E) (§14.5.10.3). Note that the existence of an implicit conversion 35 from E to D just indicates that the set of applicable methods is not empty, but does not guarantee that the 36 compile-time application of the conversion will succeed without error. 37

[Example: In the following code 38

using System; 39 using System.Windows.Forms; 40

class AlertDialog 41 { 42 Label message = new Label(); 43 Button okButton = new Button(); 44 Button cancelButton = new Button(); 45

public AlertDialog() { 46 okButton.Click += new EventHandler(OkClick); 47 cancelButton.Click += new EventHandler(CancelClick); 48 … 49 } 50

void OkClick(object sender, EventArgs e) { 51 … 52 } 53

Page 163: C# Language Specification

Chapter 13 Conversions

143

void CancelClick(object sender, EventArgs e) { 1 … 2 } 3 } 4

the constructor creates two delegate instances using the new operator. Implicit method group conversions 5 permit this to be shortened to 6

public AlertDialog() { 7 okButton.Click += OkClick; 8 cancelButton.Click += CancelClick; 9 … 10 } 11

end example] 12

As with all other implicit and explicit conversions, the cast operator can be used to explicitly perform a 13 particular conversion. [Example: Thus, the example 14

object obj = new EventHandler(myDialog.OkClick); 15

could instead be written 16

object obj = (EventHandler)myDialog.OkClick; 17

end example] 18

Although method groups and anonymous method expressions can influence overload resolution, they do not 19 participate in type inferencing (§26.6.4). 20

Page 164: C# Language Specification
Page 165: C# Language Specification

Chapter 14 Expressions

145

14. Expressions 1

An expression is a sequence of operators and operands. This clause defines the syntax, order of evaluation of 2 operands and operators, and meaning of expressions. 3

14.1 Expression classifications 4

An expression is classified as one of the following: 5

• A value. Every value has an associated type. 6

• A variable. Every variable has an associated type, namely the declared type of the variable. 7

• A namespace. An expression with this classification can only appear as the left-hand side of a member-8 access (§14.5.4). In any other context, an expression classified as a namespace causes a compile-time 9 error. 10

• A type. An expression with this classification can only appear as the left-hand side of a member-access 11 (§14.5.4), or as an operand for the as operator (§14.9.10), the is operator (§14.9.9), or the typeof 12 operator (§14.5.11). In any other context, an expression classified as a type causes a compile-time error. 13

• A method group, which is a set of overloaded methods resulting from a member lookup (§14.3). A 14 method group can have an associated instance expression. When an instance method is invoked, the 15 result of evaluating the instance expression becomes the instance represented by this (§14.5.7). A 16 method group can be used in an invocation-expression (§14.5.5), used in a delegate-creation-expression 17 (§14.5.10.3), or implicitly converted to a compatible delegate type. In any other context, an expression 18 classified as a method group causes a compile-time error. 19

• An anonymous method. An expression with this classification can be used in a delegate-creation-20 expression (§14.5.10.3) or implicitly converted to a compatible delegate type. In any other context, an 21 expression classified as an anonymous method causes a compile-time error. 22

• A property access. Every property access has an associated type, namely the type of the property. 23 Furthermore, a property access can have an associated instance expression. When an accessor (the get 24 or set block) of an instance property access is invoked, the result of evaluating the instance expression 25 becomes the instance represented by this (§14.5.7). 26

• An event access. Every event access has an associated type, namely the type of the event. Furthermore, 27 an event access can have an associated instance expression. An event access can appear as the left-hand 28 operand of the += and -= operators (§14.13.3). In any other context, an expression classified as an event 29 access causes a compile-time error. 30

• An indexer access. Every indexer access has an associated type, namely the element type of the indexer. 31 Furthermore, an indexer access has an associated instance expression and an associated argument list. 32 When an accessor (the get or set block) of an indexer access is invoked, the result of evaluating the 33 instance expression becomes the instance represented by this (§14.5.7), and the result of evaluating the 34 argument list becomes the parameter list of the invocation. 35

• Nothing. This occurs when the expression is an invocation of a method with a return type of void. An 36 expression classified as nothing is only valid in the context of a statement-expression (§15.6). 37

The final result of an expression is never a namespace, type, method group, anonymous method, or event 38 access. Rather, as noted above, these categories of expressions are intermediate constructs that are only 39 permitted in certain contexts. 40

Page 166: C# Language Specification

C# LANGUAGE SPECIFICATION

146

A property access or indexer access is always reclassified as a value by performing an invocation of the get-1 accessor or the set-accessor. The particular accessor is determined by the context of the property or indexer 2 access: If the access is the target of an assignment, the set-accessor is invoked to assign a new value 3 (§14.13.1). Otherwise, the get-accessor is invoked to obtain the current value (§14.1.1). 4

14.1.1 Values of expressions 5

Most of the constructs that involve an expression ultimately require the expression to denote a value. In such 6 cases, if the actual expression denotes a namespace, a type, or nothing, a compile-time error occurs. 7 However, if the expression denotes a property access, an indexer access, or a variable, the value of the 8 property, indexer, or variable is implicitly substituted: 9

• The value of a variable is simply the value currently stored in the storage location identified by the 10 variable. A variable shall be considered definitely assigned (§12.3) before its value can be obtained, or 11 otherwise a compile-time error occurs. 12

• The value of a property access expression is obtained by invoking the get-accessor of the property. If the 13 property has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation 14 (§14.4.3) is performed, and the result of the invocation becomes the value of the property access 15 expression. 16

• The value of an indexer access expression is obtained by invoking the get-accessor of the indexer. If the 17 indexer has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation 18 (§14.4.3) is performed with the argument list associated with the indexer access expression, and the 19 result of the invocation becomes the value of the indexer access expression. 20

14.2 Operators 21

Expressions are constructed from operands and operators. The operators of an expression indicate which 22 operations to apply to the operands. [Example: Examples of operators include +, -, *, /, and new. Examples 23 of operands include literals, fields, local variables, and expressions. end example] 24

There are three kinds of operators: 25

• Unary operators. The unary operators take one operand and use either prefix notation (such as –x) or 26 postfix notation (such as x++). 27

• Binary operators. The binary operators take two operands and all use infix notation (such as x + y). 28

• Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation 29 (c ? x : y). 30

The order of evaluation of operators in an expression is determined by the precedence and associativity of 31 the operators (§14.2.1). 32

The order in which operands in an expression are evaluated, is left to right. [Example: In 33 F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old 34 value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to 35 operator precedence. end example] 36

Certain operators can be overloaded. Operator overloading permits user-defined operator implementations to 37 be specified for operations where one or both of the operands are of a user-defined class or struct type 38 (§14.2.2). 39

14.2.1 Operator precedence and associativity 40

When an expression contains multiple operators, the precedence of the operators controls the order in which 41 the individual operators are evaluated. [Note: For example, the expression x + y * z is evaluated as 42 x + (y * z) because the * operator has higher precedence than the binary + operator. end note] The 43 precedence of an operator is established by the definition of its associated grammar production. [Note: For 44

Page 167: C# Language Specification

Chapter 14 Expressions

147

example, an additive-expression consists of a sequence of multiplicative-expressions separated by + or -1 operators, thus giving the + and - operators lower precedence than the *, /, and % operators. end note] 2

The following table summarizes all operators in order of precedence from highest to lowest: 3 4

Subclause Category Operators

14.5 Primary x.y f(x) a[x] x++ x-- new

typeof checked unchecked

14.6 Unary + - ! ~ ++x --x (T)x

14.7.1 Multiplicative * / %

14.7 Additive + -

14.8 Shift << >>

14.9 Relational and type-testing

< > <= >= is as

14.9 Equality == !=

14.10 Logical AND &

14.10 Logical XOR ^

14.10 Logical OR |

14.11 Conditional AND

&&

14.11 Conditional OR ||

14.12 Conditional ?:

14.13 Assignment = *= /= %= += -= <<= >>= &= ^= |=

5 When an operand occurs between two operators with the same precedence, the associativity of the operators 6 controls the order in which the operations are performed: 7

• Except for the assignment operators, all binary operators are left-associative, meaning that operations 8 are performed from left to right. [Example: x + y + z is evaluated as (x + y) + z. end example] 9

• The assignment operators and the conditional operator (?:) are right-associative, meaning that 10 operations are performed from right to left. [Example: x = y = z is evaluated as x = (y = z). end 11 example] 12

Precedence and associativity can be controlled using parentheses. [Example: x + y * z first multiplies y 13 by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z. 14 end example] 15

14.2.2 Operator overloading 16

All unary and binary operators have predefined implementations that are automatically available in any 17 expression. In addition to the predefined implementations, user-defined implementations can be introduced 18 by including operator declarations in classes and structs (§17.9). User-defined operator implementations 19 always take precedence over predefined operator implementations: Only when no applicable user-defined 20 operator implementations exist will the predefined operator implementations be considered, as described in 21 §14.2.3 and §14.2.4. 22

The overloadable unary operators are: 23

+ - ! ~ ++ -- true false 24

Page 168: C# Language Specification

C# LANGUAGE SPECIFICATION

148

[Note: Although true and false are not used explicitly in expressions (and therefore are not included in 1 the precedence table in §14.2.1), they are considered operators because they are invoked in several 2 expression contexts: boolean expressions (§14.16) and expressions involving the conditional (§14.12), and 3 conditional logical operators (§14.11). end note] 4

The overloadable binary operators are: 5

+ - * / % & | ^ << >> == != > < >= <= 6

Only the operators listed above can be overloaded. In particular, it is not possible to overload member 7 access, method invocation, or the =, &&, ||, ?:, checked, unchecked, new, typeof, as, and 8 is operators. 9

When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly 10 overloaded. [Example: An overload of operator * is also an overload of operator *=. This is described 11 further in §14.13. end example] The assignment operator itself (=) cannot be overloaded. An assignment 12 always performs a simple bit-wise copy of a value into a variable. 13

Cast operations, such as (T)x, are overloaded by providing user-defined conversion operators (§13.4). 14

Element access, such as a[x], is not considered an overloadable operator. Instead, user-defined indexing is 15 supported through indexers (§17.8). 16

In expressions, operators are referenced using operator notation, and in declarations, operators are referenced 17 using functional notation. The following table shows the relationship between operator and functional 18 notations for unary and binary operators. In the first entry, op denotes any overloadable unary prefix 19 operator. In the second entry, op denotes the unary postfix ++ and -- operators. In the third entry, op 20 denotes any overloadable binary operator. [Note: For an example of overloading the ++ and -- operators see 21 §17.9.1. end note] 22

23

Operator notation Functional notation

op x operator op(x)

x op operator op(x)

x op y operator op(x, y) 24 User-defined operator declarations always require at least one of the parameters to be of the class or struct 25 type that contains the operator declaration. [Note: Thus, it is not possible for a user-defined operator to have 26 the same signature as a predefined operator. end note] 27

User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator. 28 [Example: The / operator is always a binary operator, always has the precedence level specified in §14.2.1, 29 and is always left-associative. end example] 30

[Note: While it is possible for a user-defined operator to perform any computation it pleases, 31 implementations that produce results other than those that are intuitively expected are strongly discouraged. 32 For example, an implementation of operator == should compare the two operands for equality and return 33 an appropriate bool result. end note] 34

The descriptions of individual operators in §14.5 through §14.13 specify the predefined implementations of 35 the operators and any additional rules that apply to each operator. The descriptions make use of the terms 36 unary operator overload resolution, binary operator overload resolution, and numeric promotion, 37 definitions of which are found in the following subclauses. 38

14.2.3 Unary operator overload resolution 39

An operation of the form op x or x op, where op is an overloadable unary operator, and x is an expression of 40 type X, is processed as follows: 41

Page 169: C# Language Specification

Chapter 14 Expressions

149

• The set of candidate user-defined operators provided by X for the operation operator op(x) is 1 determined using the rules of §14.2.5. 2

• If the set of candidate user-defined operators is not empty, then this becomes the set of candidate 3 operators for the operation. Otherwise, the predefined unary operator op implementations become the 4 set of candidate operators for the operation. If type X is not an enum type, then any predefined unary 5 operator with a parameter type that is an enum type is removed from consideration. The predefined 6 implementations of a given operator are specified in the description of the operator (§14.5 and 7 §14.5.13). 8

• The overload resolution rules of §14.4.2 are applied to the set of candidate operators to select the best 9 operator with respect to the argument list (x), and this operator becomes the result of the overload 10 resolution process. If overload resolution fails to select a single best operator, a compile-time error 11 occurs. 12

14.2.4 Binary operator overload resolution 13

An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X, 14 and y is an expression of type Y, is processed as follows: 15

• The set of candidate user-defined operators provided by X and Y for the operation operator op(x, y) 16 is determined. The set consists of the union of the candidate operators provided by X and the candidate 17 operators provided by Y, each determined using the rules of §14.2.5. If X and Y are the same type, or if X 18 and Y are derived from a common base type, then shared candidate operators only occur in the combined 19 set once. 20

• If the set of candidate user-defined operators is not empty, then this becomes the set of candidate 21 operators for the operation. Otherwise, the predefined binary operator op implementations become the 22 set of candidate operators for the operation. If neither X nor Y is an enum type, then any predefined 23 binary operator with a parameter type that is an enum type is removed from consideration. Similarly, if 24 neither X nor Y is a delegate type, then any predefined binary operator with a parameter type that is a 25 delegate type is removed from consideration. The predefined implementations of a given operator are 26 specified in the description of the operator (§14.7 through §14.13). 27

• The overload resolution rules of §14.4.2 are applied to the set of candidate operators to select the best 28 operator with respect to the argument list (x, y), and this operator becomes the result of the overload 29 resolution process. If overload resolution fails to select a single best operator, a compile-time error 30 occurs. 31

14.2.5 Candidate user-defined operators 32

Given a type T and an operation operator op(A), where op is an overloadable operator and A is an 33 argument list, the set of candidate user-defined operators provided by T for operator op(A) is 34 determined as follows: 35

• For all operator op declarations in T, if at least one operator is applicable (§14.4.2.1) with respect to 36 the argument list A, then the set of candidate operators consists of all applicable operator op 37 declarations in T. 38

• Otherwise, if T is object, the set of candidate operators is empty. 39

• Otherwise, the set of candidate operators provided by T is the set of candidate operators provided by the 40 direct base class of T. 41

14.2.6 Numeric promotions 42

This subclause is informative. 43

Numeric promotion consists of automatically performing certain implicit conversions of the operands of the 44 predefined unary and binary numeric operators. Numeric promotion is not a distinct mechanism, but rather 45 an effect of applying overload resolution to the predefined operators. Numeric promotion specifically does 46

Page 170: C# Language Specification

C# LANGUAGE SPECIFICATION

150

not affect evaluation of user-defined operators, although user-defined operators can be implemented to 1 exhibit similar effects. 2

As an example of numeric promotion, consider the predefined implementations of the binary * operator: 3

int operator *(int x, int y); 4 uint operator *(uint x, uint y); 5 long operator *(long x, long y); 6 ulong operator *(ulong x, ulong y); 7 void operator *(long x, ulong y); 8 void operator *(ulong x, long y); 9 float operator *(float x, float y); 10 double operator *(double x, double y); 11 decimal operator *(decimal x, decimal y); 12

When overload resolution rules (§14.4.2) are applied to this set of operators, the effect is to select the first of 13 the operators for which implicit conversions exist from the operand types. [Example: For the operation 14 b * s, where b is a byte and s is a short, overload resolution selects operator *(int, int) as the 15 best operator. Thus, the effect is that b and s are converted to int, and the type of the result is int. 16 Likewise, for the operation i * d, where i is an int and d is a double, overload resolution selects 17 operator *(double, double) as the best operator. end example] 18

End of informative text. 19

14.2.6.1 Unary numeric promotions 20

This subclause is informative. 21

Unary numeric promotion occurs for the operands of the predefined +, –, and ~ unary operators. Unary 22 numeric promotion simply consists of converting operands of type sbyte, byte, short, ushort, or char 23 to type int. Additionally, for the unary – operator, unary numeric promotion converts operands of type 24 uint to type long. 25

End of informative text. 26

14.2.6.2 Binary numeric promotions 27

This subclause is informative. 28

Binary numeric promotion occurs for the operands of the predefined +, –, *, /, %, &, |, ^, ==, !=, >, <, >=, 29 and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type 30 which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric 31 promotion consists of applying the following rules, in the order they appear here: 32

• If either operand is of type decimal, the other operand is converted to type decimal, or a compile-33 time error occurs if the other operand is of type float or double. 34

• Otherwise, if either operand is of type double, the other operand is converted to type double. 35

• Otherwise, if either operand is of type float, the other operand is converted to type float. 36

• Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a 37 compile-time error occurs if the other operand is of type sbyte, short, int, or long. 38

• Otherwise, if either operand is of type long, the other operand is converted to type long. 39

• Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, 40 both operands are converted to type long. 41

• Otherwise, if either operand is of type uint, the other operand is converted to type uint. 42

• Otherwise, both operands are converted to type int. 43

[Note: The first rule disallows any operations that mix the decimal type with the double and float 44 types. The rule follows from the fact that there are no implicit conversions between the decimal type 45 and the double and float types. end note] 46

Page 171: C# Language Specification

Chapter 14 Expressions

151

[Note: Also note that it is not possible for an operand to be of type ulong when the other operand is of a 1 signed integral type. The reason is that no integral type exists that can represent the full range of ulong 2 as well as the signed integral types. end note] 3

In both of the above cases, a cast expression can be used to explicitly convert one operand to a type that is 4 compatible with the other operand. 5

[Example: In the following code 6

decimal AddPercent(decimal x, double percent) { 7 return x * (1.0 + percent / 100.0); 8 } 9

a compile-time error occurs because a decimal cannot be multiplied by a double. The error is resolved by 10 explicitly converting the second operand to decimal, as follows: 11

decimal AddPercent(decimal x, double percent) { 12 return x * (decimal)(1.0 + percent / 100.0); 13 } 14

end example] 15

End of informative text. 16

14.3 Member lookup 17

A member lookup is the process whereby the meaning of a name in the context of a type is determined. A 18 member lookup can occur as part of evaluating a simple-name (§14.5.2) or a member-access (§14.5.4) in an 19 expression. 20

Member lookup considers not only the name of a member but also the number of type parameters the 21 member has and whether the member is accessible. For the purposes of member lookup, generic methods 22 and nested generic types have the number of type parameters indicated in their respective declarations and 23 all other members have zero type parameters. 24

A member lookup of a name N with K type parameters in a type T is processed as follows: 25

• First, a set of accessible members named N is determined: 26

o If T is a type parameter, then the set is the union of the sets of accessible members named N in each 27 of the types specified as a primary constraint or secondary constraint (§26.7) for T, along with the 28 set of accessible members named N in object. 29

o Otherwise, the set consists of all accessible (§10.5) members named N in T, including inherited 30 members and the accessible members named N in object. If T is a constructed type, the set of 31 members is obtained by substituting type arguments as described in §26.5.4. Members that include 32 an override modifier are excluded from the set. 33

• Next, if the set of accessible members is empty, the member lookup does not produce a match and no 34 further steps are taken. 35

• Next, if K is zero, remove all nested types whose declaration included type parameters. If K is not zero, 36 remove all members with a different number of type parameters. Note that when K is zero, we do not 37 remove all methods having type parameters, since the type inference process (§26.6.4) might be able to 38 infer the type arguments. 39

• Next, members that are hidden by other members are removed from the set. Members that are removed 40 during this step may still cause other members to be removed. For every member S.M in the set, where S 41 is the type in which the member M is declared, the following rules are applied: 42

o If M is a constant, field, property, event, enumeration member, or type declaration then all members 43 declared in a base type of S are removed from the set. 44

o If M is a method, then all non-method members declared in a base type of S are removed from the 45 set. 46

Page 172: C# Language Specification

C# LANGUAGE SPECIFICATION

152

• Next, interface members that are hidden by class members are removed from the set. This step only has 1 an effect if T is a type parameter and T has both an effective base class other than object and a non-2 empty effective interface set (§26.7). For every member S.M in the set, where S is the type in which the 3 member M is declared, the following rules are applied if S is a class declaration other than object: 4

o If M is a constant, field, property, event, enumeration member, or type declaration, then all members 5 declared in an interface declaration are removed from the set. 6

o If M is a method, then all non-method members declared in an interface declaration are removed 7 from the set. 8

• Finally, having removed hidden members, the result of the lookup is determined: 9

o If the set consists of a single member that is not a method, then this member is the result of the 10 lookup. 11

o Otherwise, if the set contains only methods, then this group of methods is the result of the lookup. 12

o Otherwise, the lookup is ambiguous, and a compile-time error occurs. 13

For member lookups in types other than type parameters and interfaces, and member lookups in interfaces 14 that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or one direct base 15 interface), the effect of the lookup rules is simply that derived members hide base members with the same 16 name. Such single-inheritance lookups are never ambiguous. The ambiguities that can possibly arise from 17 member lookups in multiple-inheritance interfaces are described in §20.2.5. 18

14.3.1 Base types 19

For purposes of member lookup, a type T is considered to have the following base types: 20

• If T is object, then T has no base type. 21

• If T is an enum-type, the base types of T are the class types System.Enum, System.ValueType, and 22 object. 23

• If T is a struct-type, the base types of T are the class types System.ValueType and object. 24

• If T is a class-type, the base types of T are the base classes of T, including the class type object. 25

• If T is an interface-type, the base types of T are the base interfaces of T and the class type object. 26

• If T is an array-type, the base types of T are the class types System.Array and object. 27

• If T is a delegate-type, the base types of T are the class types System.Delegate and object. 28

14.4 Function members 29

Function members are members that contain executable statements. Function members are always members 30 of types and cannot be members of namespaces. C# defines the following categories of function members: 31

• Methods 32

• Properties 33

• Events 34

• Indexers 35

• User-defined operators 36

• Instance constructors 37

• Static constructors 38

• Destructors 39

Page 173: C# Language Specification

Chapter 14 Expressions

153

Except for static constructors and destructors (which cannot be invoked explicitly), the statements contained 1 in function members are executed through function member invocations. The actual syntax for writing a 2 function member invocation depends on the particular function member category. 3

The argument list (§14.4.1) of a function member invocation provides actual values or variable references 4 for the parameters of the function member. 5

Invocations of methods, indexers, operators, and instance constructors employ overload resolution to 6 determine which of a candidate set of function members to invoke. This process is described in §14.4.2. 7

Once a particular function member has been identified at compile-time, possibly through overload 8 resolution, the actual run-time process of invoking the function member is described in §14.4.3. 9

[Note: The following table summarizes the processing that takes place in constructs involving the six 10 categories of function members that can be explicitly invoked. In the table, e, x, y, and value indicate 11 expressions classified as variables or values, T indicates an expression classified as a type, F is the simple 12 name of a method, and P is the simple name of a property. 13

14

Construct Example Description F(x, y) Overload resolution is applied to select the best method F in the

containing class or struct. The method is invoked with the argument list (x, y). If the method is not static, the instance expression is this.

T.F(x, y) Overload resolution is applied to select the best method F in the class or struct T. A compile-time error occurs if the method is not static. The method is invoked with the argument list (x, y).

Method invocation

e.F(x, y) Overload resolution is applied to select the best method F in the class, struct, or interface given by the type of e. A compile-time error occurs if the method is static. The method is invoked with the instance expression e and the argument list (x, y).

P The get accessor of the property P in the containing class or struct is invoked. A compile-time error occurs if P is write-only. If P is not static, the instance expression is this.

P = value The set accessor of the property P in the containing class or struct is invoked with the argument list (value). A compile-time error occurs if P is read-only. If P is not static, the instance expression is this.

T.P The get accessor of the property P in the class or struct T is invoked. A compile-time error occurs if P is not static or if P is write-only.

T.P = value The set accessor of the property P in the class or struct T is invoked with the argument list (value). A compile-time error occurs if P is not static or if P is read-only.

e.P The get accessor of the property P in the class, struct, or interface given by the type of e is invoked with the instance expression e. A compile-time error occurs if P is static or if P is write-only.

Property access

e.P = value The set accessor of the property P in the class, struct, or interface given by the type of e is invoked with the instance expression e and the argument list (value). A compile-time error occurs if P is static or if P is read-only.

Page 174: C# Language Specification

C# LANGUAGE SPECIFICATION

154

Construct Example Description E += value The add accessor of the event E in the containing class or struct

is invoked. If E is not static, the instance expression is this. E -= value The remove accessor of the event E in the containing class or

struct is invoked. If E is not static, the instance expression is this.

T.E += value The add accessor of the event E in the class or struct T is invoked. A compile-time error occurs if E is not static.

T.E -= value The remove accessor of the event E in the class or struct T is invoked. A compile-time error occurs if E is not static.

e.E += value The add accessor of the event E in the class, struct, or interface given by the type of e is invoked with the instance expression e. A compile-time error occurs if E is static.

Event access

e.E -= value The remove accessor of the event E in the class, struct, or interface given by the type of e is invoked with the instance expression e. A compile-time error occurs if E is static.

e[x, y] Overload resolution is applied to select the best indexer in the class, struct, or interface given by the type of e. The get accessor of the indexer is invoked with the instance expression e and the argument list (x, y). A compile-time error occurs if the indexer is write-only.

Indexer access

e[x, y] = value Overload resolution is applied to select the best indexer in the class, struct, or interface given by the type of e. The set accessor of the indexer is invoked with the instance expression e and the argument list (x, y, value). A compile-time error occurs if the indexer is read-only.

-x Overload resolution is applied to select the best unary operator in the class or struct given by the type of x. The selected operator is invoked with the argument list (x).

Operator invocation

x + y Overload resolution is applied to select the best binary operator in the classes or structs given by the types of x and y. The selected operator is invoked with the argument list (x, y).

Instance constructor invocation

new T(x, y) Overload resolution is applied to select the best instance constructor in the class or struct T. The instance constructor is invoked with the argument list (x, y).

1 end note] 2

14.4.1 Argument lists 3

Every function member invocation includes an argument list, which provides actual values or variable 4 references for the parameters of the function member. The syntax for specifying the argument list of a 5 function member invocation depends on the function member category: 6

• For instance constructors, methods, and delegates, the arguments are specified as an argument-list, as 7 described below. 8

• For properties, the argument list is empty when invoking the get accessor, and consists of the 9 expression specified as the right operand of the assignment operator when invoking the set accessor. 10

Page 175: C# Language Specification

Chapter 14 Expressions

155

• For events, the argument list consists of the expression specified as the right operand of the += or -= 1 operator. 2

• For indexers, the argument list consists of the expressions specified between the square brackets in the 3 indexer access. When invoking the set accessor, the argument list additionally includes the expression 4 specified as the right operand of the assignment operator. [Note: The additional argument is not used for 5 overload resolution, just during invocation of the set accessor. end note] 6

• For user-defined operators, the argument list consists of the single operand of the unary operator or the 7 two operands of the binary operator. 8

The arguments of properties (§17.6), events (§17.7), and user-defined operators (§17.9) are always passed as 9 value parameters (§17.5.1.1). The arguments of indexers (§17.8) are always passed as value parameters 10 (§17.5.1.1) or parameter arrays (§17.5.1.4). Reference and output parameters are not supported for these 11 categories of function members. 12

The arguments of an instance constructor, method, or delegate invocation are specified as an argument-list: 13

argument-list: 14 argument 15 argument-list , argument 16

argument: 17 expression 18 ref variable-reference 19 out variable-reference 20

An argument-list consists of one or more arguments, separated by commas. Each argument can take one of 21 the following forms: 22

• An expression, indicating that the argument is passed as a value parameter (§17.5.1.1). 23

• The keyword ref followed by a variable-reference (§12.3.3.27), indicating that the argument is passed 24 as a reference parameter (§17.5.1.2). A variable shall be definitely assigned (§12.3) before it can be 25 passed as a reference parameter. 26

• The keyword out followed by a variable-reference (§12.3.3.27), indicating that the argument is passed 27 as an output parameter (§17.5.1.3). A variable is considered definitely assigned (§12.3) following a 28 function member invocation in which the variable is passed as an output parameter. 29

Passing a volatile field (§17.4.3) as a reference parameter or output parameter causes a warning, since the 30 field may not be treated as volatile by the invoked method. 31

During the run-time processing of a function member invocation (§14.4.3), the expressions or variable 32 references of an argument list are evaluated in order, from left to right, as follows: 33

• For a value parameter, the argument expression is evaluated and an implicit conversion (§13.1) to the 34 corresponding parameter type is performed. The resulting value becomes the initial value of the value 35 parameter in the function member invocation. 36

• For a reference or output parameter, the variable reference is evaluated and the resulting storage location 37 becomes the storage location represented by the parameter in the function member invocation. If the 38 variable reference given as a reference or output parameter is an array element of a reference-type, a 39 run-time check is performed to ensure that the element type of the array is identical to the type of the 40 parameter. If this check fails, a System.ArrayTypeMismatchException is thrown. 41

Methods, indexers, and instance constructors can declare their right-most parameter to be a parameter array 42 (§17.5.1.4). Such function members are invoked either in their normal form or in their expanded form 43 depending on which is applicable (§14.4.2.1): 44

• When a function member with a parameter array is invoked in its normal form, the argument given for 45 the parameter array shall be a single expression of a type that is implicitly convertible (§13.1) to the 46 parameter array type. In this case, the parameter array acts precisely like a value parameter. 47

Page 176: C# Language Specification

C# LANGUAGE SPECIFICATION

156

• When a function member with a parameter array is invoked in its expanded form, the invocation shall 1 specify zero or more arguments for the parameter array, where each argument is an expression of a type 2 that is implicitly convertible (§13.1) to the element type of the parameter array. In this case, the 3 invocation creates an instance of the parameter array type with a length corresponding to the number of 4 arguments, initializes the elements of the array instance with the given argument values, and uses the 5 newly created array instance as the actual argument. 6

The expressions of an argument list are always evaluated in the order they are written. [Example: Thus, 7 the example 8

class Test 9 { 10 static void F(int x, int y, int z) { 11 System.Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z); 12 } 13

static void Main() { 14 int i = 0; 15 F(i++, i++, i++); 16 } 17 } 18

produces the output 19

x = 0, y = 1, z = 2 20

end example] 21

The array covariance rules (§19.5) permit a value of an array type A[] to be a reference to an instance of an 22 array type B[], provided an implicit reference conversion exists from B to A. Because of these rules, when 23 an array element of a reference-type is passed as a reference or output parameter, a run-time check is 24 required to ensure that the actual element type of the array is identical to that of the parameter. [Example: In 25 the following code 26

class Test 27 { 28 static void F(ref object x) {…} 29

static void Main() { 30 object[] a = new object[10]; 31 object[] b = new string[10]; 32 F(ref a[0]); // Ok 33 F(ref b[1]); // ArrayTypeMismatchException 34 } 35 } 36

the second invocation of F causes a System.ArrayTypeMismatchException to be thrown because the 37 actual element type of b is string and not object. end example] 38

When a function member with a parameter array is invoked in its expanded form, the invocation is 39 processed exactly as if an array creation expression with an array initializer (§14.5.10.2) was inserted around 40 the expanded parameters. [Example: Given the declaration 41

void F(int x, int y, params object[] args); 42

the following invocations of the expanded form of the method 43

F(10, 20); 44 F(10, 20, 30, 40); 45 F(10, 20, 1, "hello", 3.0); 46

correspond exactly to 47

F(10, 20, new object[] {}); 48 F(10, 20, new object[] {30, 40}); 49 F(10, 20, new object[] {1, "hello", 3.0}); 50

In particular, note that an empty array is created when there are zero arguments given for the parameter 51 array. end example] 52

Page 177: C# Language Specification

Chapter 14 Expressions

157

14.4.2 Overload resolution 1

Overload resolution is a compile-time mechanism for selecting the best function member to invoke given an 2 argument list and a set of candidate function members. Overload resolution selects the function member to 3 invoke in the following distinct contexts within C#: 4

• Invocation of a method named in an invocation-expression (§14.5.5). 5

• Invocation of an instance constructor named in an object-creation-expression (§14.5.10.1). 6

• Invocation of an indexer accessor through an element-access (§14.5.6). 7

• Invocation of a predefined or user-defined operator referenced in an expression (§14.2.3 and §14.2.4). 8

Each of these contexts defines the set of candidate function members and the list of arguments in its own 9 unique way. However, once the candidate function members and the argument list have been identified, the 10 selection of the best function member is the same in all cases: 11

• First, the set of candidate function members is reduced to those function members that are applicable 12 with respect to the given argument list (§14.4.2.1). If this reduced set is empty, a compile-time error 13 occurs. 14

• Then, given the set of applicable candidate function members, the best function member in that set is 15 located. If the set contains only one function member, then that function member is the best function 16 member. Otherwise, the best function member is the one function member that is better than all other 17 function members with respect to the given argument list, provided that each function member is 18 compared to all other function members using the rules in §14.4.2.2. If there is not exactly one function 19 member that is better than all other function members, then the function member invocation is 20 ambiguous and a compile-time error occurs. 21

The following subclauses define the exact meanings of the terms applicable function member and better 22 function member. 23

14.4.2.1 Applicable function member 24

A function member is said to be an applicable function member with respect to an argument list A when all 25 of the following are true: 26

• The number of arguments in A is identical to the number of parameters in the function member 27 declaration. 28

• For each argument in A, the parameter passing mode of the argument (i.e., value, ref, or out) is 29 identical to the parameter passing mode of the corresponding parameter, and 30

o for a value parameter or a parameter array, an implicit conversion (§13.1) exists from the type of the 31 argument to the type of the corresponding parameter, or 32

o for a ref or out parameter, the type of the argument is identical to the type of the corresponding 33 parameter. [Note: After all, a ref or out parameter is an alias for the argument passed. end note] 34

For a function member that includes a parameter array, if the function member is applicable by the above 35 rules, it is said to be applicable in its normal form. If a function member that includes a parameter array is 36 not applicable in its normal form, the function member might instead be applicable in its expanded form: 37

• The expanded form is constructed by replacing the parameter array in the function member declaration 38 with zero or more value parameters of the element type of the parameter array such that the number of 39 arguments in the argument list A matches the total number of parameters. If A has fewer arguments than 40 the number of fixed parameters in the function member declaration, the expanded form of the function 41 member cannot be constructed and is thus not applicable. 42

• If the class, struct, or interface in which the function member is declared already contains another 43 applicable function member with the same signature as the expanded form, the expanded form is not 44 applicable. 45

Page 178: C# Language Specification

C# LANGUAGE SPECIFICATION

158

• Otherwise, the expanded form is applicable if for each argument in A the parameter passing mode of the 1 argument is identical to the parameter passing mode of the corresponding parameter, and 2

o for a fixed value parameter or a value parameter created by the expansion, an implicit conversion 3 (§13.1) exists from the type of the argument to the type of the corresponding parameter, or 4

o for a ref or out parameter, the type of the argument is identical to the type of the corresponding 5 parameter. 6

14.4.2.2 Better function member 7

Given an argument list A with a set of argument types A1, A2, …, AN and two applicable function members MP 8 and MQ with parameter types P1, P2, …, PN and Q1, Q2, …, QN, MP is defined to be a better function member 9 than MQ if 10

• for each argument, the implicit conversion from AX to PX is not worse than the implicit conversion from 11 AX to QX, and 12

• for at least one argument, the conversion from AX to PX is better than the conversion from AX to QX. 13

When performing this evaluation, if MP or MQ is applicable in its expanded form, then PX or QX refers to a 14 parameter in the expanded form of the parameter list. 15

14.4.2.3 Better conversion 16

Given an implicit conversion C1 that converts from a type S to a type T1, and an implicit conversion C2 that 17 converts from a type S to a type T2, the better conversion of the two conversions is determined as follows: 18

• If T1 and T2 are the same type, neither conversion is better. 19

• If S is T1, C1 is the better conversion. 20

• If S is T2, C2 is the better conversion. 21

• If an implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists, C1 is the 22 better conversion. 23

• If an implicit conversion from T2 to T1 exists, and no implicit conversion from T1 to T2 exists, C2 is the 24 better conversion. 25

• If T1 is sbyte and T2 is byte, ushort, uint, or ulong, C1 is the better conversion. 26

• If T2 is sbyte and T1 is byte, ushort, uint, or ulong, C2 is the better conversion. 27

• If T1 is short and T2 is ushort, uint, or ulong, C1 is the better conversion. 28

• If T2 is short and T1 is ushort, uint, or ulong, C2 is the better conversion. 29

• If T1 is int and T2 is uint, or ulong, C1 is the better conversion. 30

• If T2 is int and T1 is uint, or ulong, C2 is the better conversion. 31

• If T1 is long and T2 is ulong, C1 is the better conversion. 32

• If T2 is long and T1 is ulong, C2 is the better conversion. 33

• Otherwise, neither conversion is better. 34

If an implicit conversion C1 is defined by these rules to be a better conversion than an implicit conversion C2, 35 then it is also the case that C2 is a worse conversion than C1. 36

14.4.3 Function member invocation 37

This subclause describes the process that takes place at run-time to invoke a particular function member. It is 38 assumed that a compile-time process has already determined the particular member to invoke, possibly by 39 applying overload resolution to a set of candidate function members. 40

Page 179: C# Language Specification

Chapter 14 Expressions

159

For purposes of describing the invocation process, function members are divided into two categories: 1

• Static function members. These are static methods, static property accessors, and user-defined operators. 2 Static function members are always non-virtual. 3

• Instance function members. These are instance methods, instance constructors, instance property 4 accessors, and indexer accessors. Instance function members are either non-virtual or virtual, and are 5 always invoked on a particular instance. The instance is computed by an instance expression, and it 6 becomes accessible within the function member as this (§14.5.7). For an instance constructor, the 7 instance expression is taken to be the newly allocated object. 8

The run-time processing of a function member invocation consists of the following steps, where M is the 9 function member and, if M is an instance member, E is the instance expression: 10

• If M is a static function member: 11

o The argument list is evaluated as described in §14.4.1. 12

o M is invoked. 13

• If M is an instance function member declared in a value-type: 14

o E is evaluated. If this evaluation causes an exception, then no further steps are executed. For an 15 instance constructor, this evaluation consists of allocating the storage (typically from an execution 16 stack) for the new object. In this case E is classified as a variable. 17

o If E is not classified as a variable, then a temporary local variable of E’s type is created and the value 18 of E is assigned to that variable. E is then reclassified as a reference to that temporary local variable. 19 The temporary variable is accessible as this within M, but not in any other way. Thus, only when E 20 is a true variable is it possible for the caller to observe the changes that M makes to this. 21

o The argument list is evaluated as described in §14.4.1. 22

o M is invoked. The variable referenced by E becomes the variable referenced by this. 23

• If M is an instance function member declared in a reference-type: 24

o E is evaluated. If this evaluation causes an exception, then no further steps are executed. For an 25 instance constructor, this evaluation consists of allocating (typically from a garbage collected heap) 26 the storage for the new object. 27

o The argument list is evaluated as described in §14.4.1. 28

o If the type of E is a value-type, a boxing conversion (§11.3.1) is performed to convert E to type 29 System.Enum (if E is an enum) or System.ValueType, and E is considered to be of this type in 30 the following steps. [Note: In this case, M could only be a member of System.Enum, 31 System.ValueType, or System.Object. end note] 32

o The value of E is checked to be valid. If the value of E is null, a 33 System.NullReferenceException is thrown and no further steps are executed. 34

o The function member implementation to invoke is determined: 35

• If the compile-time type of E is an interface, the function member to invoke is the 36 implementation of M provided by the run-time type of the instance referenced by E. This 37 function member is determined by applying the interface mapping rules (§20.4.2). 38

• Otherwise, if M is a virtual function member, the function member to invoke is the 39 implementation of M provided by the run-time type of the instance referenced by E. This 40 function member is determined by applying the rules for determining the most derived 41 implementation (§17.5.3) of M with respect to the run-time type of the instance referenced by E. 42

• Otherwise, M is a non-virtual function member, and the function member to invoke is M itself. 43

Page 180: C# Language Specification

C# LANGUAGE SPECIFICATION

160

o The function member implementation determined in the step above is invoked. The object 1 referenced by E becomes the object referenced by this. 2

14.4.3.1 Invocations on boxed instances 3

A function member implemented in a value-type can be invoked through a boxed instance of that value-type 4 in the following situations: 5

• When the function member is an override of a method inherited from type object and is invoked 6 through an instance expression of type object. 7

• When the function member is an implementation of an interface function member and is invoked 8 through an instance expression of an interface-type. 9

• When the function member is invoked through a delegate. 10

In these situations, the boxed instance is considered to contain a variable of the value-type, and this variable 11 becomes the variable referenced by this within the function member invocation. [Note: In particular, this 12 means that when a function member is invoked on a boxed instance, it is possible for the function member to 13 modify the value contained in the boxed instance. end note] 14

14.5 Primary expressions 15

Primary expressions include the simplest forms of expressions. 16

primary-expression: 17 array-creation-expression 18 primary-no-array-creation-expression 19

primary-no-array-creation-expression: 20 literal 21 simple-name 22 parenthesized-expression 23 member-access 24 invocation-expression 25 element-access 26 this-access 27 base-access 28 post-increment-expression 29 post-decrement-expression 30 object-creation-expression 31 delegate-creation-expression 32 typeof-expression 33 checked-expression 34 unchecked-expression 35 default-value-expression 36 anonymous-method-expression 37

Primary expressions are divided between array-creation-expressions and primary-no-array-creation-38 expressions. Treating array-creation-expression in this way, rather than listing it along with the other simple 39 expression forms, enables the grammar to disallow potentially confusing code such as 40

object o = new int[3][1]; 41

which would otherwise be interpreted as 42

object o = (new int[3])[1]; 43

A primary-expression is permitted to reference a static class (§17.1.1.3) if the primary-expression is the E in 44 a member-access (§14.5.4) of the form E.I. 45

Page 181: C# Language Specification

Chapter 14 Expressions

161

14.5.1 Literals 1

A primary-expression that consists of a literal (§9.4.4) is classified as a value. 2

14.5.2 Simple names 3

A simple-name consists of an identifier, optionally followed by a type argument list: 4

simple-name: 5 identifier type-argument-listopt 6

A simple-name is either of the form I or of the form I<A1, ..., AK>, where I is a single identifier and <A1, 7 ..., AK> is an optional type-argument-list. When no type-argument-list is specified, consider K to be zero. 8 The simple-name is evaluated and classified as follows: 9

• If K is zero and the simple-name appears within a block and if the block’s (or an enclosing block’s) local 10 variable declaration space (§10.3) contains a local variable, parameter or constant with name I, then the 11 simple-name refers to that local variable, parameter or constant and is classified as a variable or value. 12

• If K is zero and the simple-name appears within the body of a generic method declaration and if that 13 declaration includes a type parameter with name I, then the simple-name refers to that type parameter. 14

• Otherwise, for each instance type T (§26.1.2), starting with the instance type of the immediately 15 enclosing type declaration and continuing with the instance type of each enclosing class or struct 16 declaration (if any): 17

o If K is zero and the declaration of T includes a type parameter with name I, then the simple-name 18 refers to that type parameter. 19

o Otherwise, if a member lookup (§14.3) of I in T with K type arguments produces a match: 20

• If T is the instance type of the immediately enclosing class or struct type and the lookup 21 identifies one or more methods, the result is a method group with an associated instance 22 expression of this. If a type argument list was specified, it is used in calling a generic method 23 (§26.6.3). 24

• Otherwise, if T is the instance type of the immediately enclosing class or struct type, if the 25 lookup identifies an instance member, and if the reference occurs within the block of an instance 26 constructor, an instance method, or an instance accessor, the result is the same as a member 27 access (§14.5.4) of the form this.I. This can only happen when K is zero. 28

• Otherwise, the result is the same as a member access (§14.5.4) of the form T.I or T.I<A1, ..., 29 AK>. In this case, it is a compile-time error for the simple-name to refer to an instance member. 30

• Otherwise, for each namespace N, starting with the namespace in which the simple-name occurs, 31 continuing with each enclosing namespace (if any), and ending with the global namespace, the following 32 steps are evaluated until an entity is located: 33

o If K is zero and I is the name of a namespace in N, then: 34

• If the location where the simple-name occurs is enclosed by a namespace declaration for N and 35 the namespace declaration contains an extern-alias-directive or using-alias-directive that 36 associates the name I with a namespace or type, then the simple-name is ambiguous and a 37 compile-time error occurs. 38

• Otherwise, the simple-name refers to the namespace named I in N. 39

o Otherwise, if N contains an accessible type having name I and K type parameters, then: 40

• If K is zero and the location where the simple-name occurs is enclosed by a namespace 41 declaration for N and the namespace declaration contains an extern-alias-directive or using-42 alias-directive that associates the name I with a namespace or type, then the simple-name is 43 ambiguous and a compile-time error occurs. 44

Page 182: C# Language Specification

C# LANGUAGE SPECIFICATION

162

• Otherwise, the namespace-or-type-name refers to the type constructed with the given type 1 arguments. 2

o Otherwise, if the location where the simple-name occurs is enclosed by a namespace declaration 3 for N: 4

• If K is zero and the namespace declaration contains an extern-alias-directive or using-alias-5 directive that associates the name I with an imported namespace or type, then the simple-name 6 refers to that namespace or type. 7

• Otherwise, if the namespaces imported by the using-namespace-directives of the namespace 8 declaration contain exactly one type having name I and K type parameters, then the simple-9 name refers to that type constructed with the given type arguments. 10

• Otherwise, if the namespaces imported by the using-namespace-directives of the namespace 11 declaration contain more than one type having name I and K type parameters, then the simple-12 name is ambiguous and an error occurs. 13

[Note: This entire step is exactly parallel to the corresponding step in the processing of a namespace-or-14 type-name (§10.8). end note] 15

• Otherwise, the simple-name is undefined and a compile-time error occurs. 16

14.5.2.1 Invariant meaning in blocks 17

For each occurrence of a given identifier as a simple-name in an expression or declarator, every other 18 occurrence of the same identifier as a simple-name in an expression or declarator within the immediately 19 enclosing block (§15.2) or switch-block (§15.7.2) shall refer to the same entity. [Note: This rule ensures that 20 the meaning of a name is always the same within a block. end note] 21

[Example: The example 22

class Test 23 { 24 double x; 25

void F(bool b) { 26 x = 1.0; 27 if (b) { 28 int x = 1; 29 } 30 } 31 } 32

results in a compile-time error because x refers to different entities within the outer block (the extent of 33 which includes the nested block in the if statement). In contrast, the example 34

class Test 35 { 36 double x; 37

void F(bool b) { 38 if (b) { 39 x = 1.0; 40 } 41 else { 42 int x = 1; 43 } 44 } 45 } 46

is permitted because the name x is never used in the outer block. end example] 47

[Note: The rule of invariant meaning applies only to simple names. It is perfectly valid for the same 48 identifier to have one meaning as a simple name and another meaning as right operand of a member access 49 (§14.5.4). end note] [Example: 50

Page 183: C# Language Specification

Chapter 14 Expressions

163

struct Point 1 { 2 int x, y; 3

public Point(int x, int y) { 4 this.x = x; 5 this.y = y; 6 } 7 } 8

The example above illustrates a common pattern of using the names of fields as parameter names in an 9 instance constructor. In the example, the simple names x and y refer to the parameters, but that does not 10 prevent the member access expressions this.x and this.y from accessing the fields. end example] 11

14.5.3 Parenthesized expressions 12

A parenthesized-expression consists of an expression enclosed in parentheses. 13

parenthesized-expression: 14 ( expression ) 15

A parenthesized-expression is evaluated by evaluating the expression within the parentheses. If the 16 expression within the parentheses denotes a namespace or type, a compile-time error occurs. Otherwise, the 17 result of the parenthesized-expression is the result of the evaluation of the contained expression. 18

14.5.4 Member access 19

A member-access consists of a primary-expression, a predefined-type, or a qualified-alias-member, followed 20 by a “.” token, followed by an identifier, optionally followed by a type-argument-list. 21

member-access: 22 primary-expression . identifier type-argument-listopt 23 predefined-type . identifier type-argument-listopt 24 qualified-alias-member . identifier type-argument-listopt 25

predefined-type: one of 26 bool byte char decimal double float int long 27 object sbyte short string uint ulong ushort 28

qualified-alias-member is defined in §16.7. 29

A member-access is either of the form E.I or of the form E.I<A1, ..., AK>, where E is a primary-30 expression, predefined-type or qualified-alias-member, I is a single identifier and <A1, ..., AK> is an 31 optional type-argument-list. When no type-argument-list is specified, consider K to be zero. The member-32 access is evaluated and classified as follows: 33

• If K is zero and E is a namespace and E contains a nested namespace with name I, then the result is that 34 namespace. 35

• Otherwise, if E is a namespace and E contains an accessible type having name I and K type parameters, 36 then the result is that type constructed with the given type arguments. 37

• If E is a classified as a type, if E is not a type parameter, and if a member lookup (§14.3) of I in E with K 38 type parameters produces a match, then E.I is evaluated and classified as follows: 39

o If I identifies a type, then the result is that type constructed with the given type arguments. 40

o If I identifies one or more methods, then the result is a method group with no associated instance 41 expression. If a type argument list was specified, it is used in calling a generic method (§26.6.3). 42

o If I identifies a static property, then the result is a property access with no associated instance 43 expression. 44

o If I identifies a static field: 45

Page 184: C# Language Specification

C# LANGUAGE SPECIFICATION

164

• If the field is readonly and the reference occurs outside the static constructor of the class or 1 struct in which the field is declared, then the result is a value, namely the value of the static 2 field I in E. 3

• Otherwise, the result is a variable, namely the static field I in E. 4

o If I identifies a static event: 5

• If the reference occurs within the class or struct in which the event is declared, and the event 6 was declared without event-accessor-declarations (§17.7), then E.I is processed exactly as if I 7 were a static field. 8

• Otherwise, the result is an event access with no associated instance expression. 9

o If I identifies a constant, then the result is a value, namely the value of that constant. 10

o If I identifies an enumeration member, then the result is a value, namely the value of that 11 enumeration member. 12

o Otherwise, E.I is an invalid member reference, and a compile-time error occurs. 13

• If E is a property access, indexer access, variable, or value, the type of which is T, and a member lookup 14 (§14.3) of I in T with K type arguments produces a match, then E.I is evaluated and classified as 15 follows: 16

o First, if E is a property or indexer access, then the value of the property or indexer access is obtained 17 (§14.1.1) and E is reclassified as a value. 18

o If I identifies one or more methods, then the result is a method group with an associated instance 19 expression of E. If a type argument list was specified, the method group consists only of generic 20 methods having K type parameters and the type argument list is implicitly applied to each method in 21 the method group (§26.6.3). If no type argument list was specified, the method group may contain 22 both generic and non-generic methods. 23

o If I identifies an instance property, then the result is a property access with an associated instance 24 expression of E. 25

o If T is a class-type and I identifies an instance field of that class-type: 26

• If the value of E is null, then a System.NullReferenceException is thrown. 27

• Otherwise, if the field is readonly and the reference occurs outside an instance constructor of 28 the class in which the field is declared, then the result is a value, namely the value of the field I 29 in the object referenced by E. 30

• Otherwise, the result is a variable, namely the field I in the object referenced by E. 31

o If T is a struct-type and I identifies an instance field of that struct-type: 32

• If E is a value, or if the field is readonly and the reference occurs outside an instance 33 constructor of the struct in which the field is declared, then the result is a value, namely the 34 value of the field I in the struct instance given by E. 35

• Otherwise, the result is a variable, namely the field I in the struct instance given by E. 36

o If I identifies an instance event: 37

• If the reference occurs within the class or struct in which the event is declared, and the event 38 was declared without event-accessor-declarations (§17.7), then E.I is processed exactly as if I 39 was an instance field. 40

• Otherwise, the result is an event access with an associated instance expression of E. 41

• Otherwise, E.I is an invalid member reference, and a compile-time error occurs. 42

Page 185: C# Language Specification

Chapter 14 Expressions

165

14.5.4.1 Identical simple names and type names 1

In a member access of the form E.I, if E is a single identifier, and if the meaning of E as a simple-name 2 (§14.5.2) is a constant, field, property, local variable, or parameter with the same type as the meaning of E as 3 a type-name (§10.8), then both possible meanings of E are permitted. The two possible meanings of E.I are 4 never ambiguous, since I shall necessarily be a member of the type E in both cases. In other words, the rule 5 simply permits access to the static members and nested types of E where a compile-time error would 6 otherwise have occurred. [Example: 7

struct Color 8 { 9 public static readonly Color White = new Color(…); 10 public static readonly Color Black = new Color(…); 11

public Color Complement() {…} 12 } 13

class A 14 { 15 public Color Color; // Field A.Color of type Color 16

void F() { 17 Color = Color.Black; // References Color.Black 18 Color = Color.Complement(); // Invokes Complement() on A.Color 19 } 20

static void G() { 21 Color c = Color.White; // References Color.White 22 } 23 } 24

Within the A class, those occurrences of the Color identifier that reference the Color type are underlined, 25 and those that reference the Color field are not underlined. end example] 26

14.5.5 Invocation expressions 27

An invocation-expression is used to invoke a method. 28

invocation-expression: 29 primary-expression ( argument-listopt ) 30

The primary-expression of an invocation-expression shall be a method group or a value of a delegate-type. If 31 the primary-expression is a method group, the invocation-expression is a method invocation (§14.5.5.1). If 32 the primary-expression is a value of a delegate-type, the invocation-expression is a delegate invocation 33 (§14.5.5.2). If the primary-expression is neither a method group nor a value of a delegate-type, a compile-34 time error occurs. 35

The optional argument-list (§14.4.1) provides values or variable references for the parameters of the method. 36

The result of evaluating an invocation-expression is classified as follows: 37

• If the invocation-expression invokes a method or delegate that returns void, the result is nothing. An 38 expression that is classified as nothing cannot be an operand of any operator, and is permitted only in the 39 context of a statement-expression (§15.6). 40

• Otherwise, the result is a value of the type returned by the method or delegate. 41

14.5.5.1 Method invocations 42

For a method invocation, the primary-expression of the invocation-expression shall be a method group. The 43 method group identifies the one method to invoke or the set of overloaded methods from which to choose a 44 specific method to invoke. In the latter case, determination of the specific method to invoke is based on the 45 context provided by the types of the arguments in the argument-list. 46

The compile-time processing of a method invocation of the form M(A), where M is a method group (possibly 47 including a type-argument-list), and A is an optional argument-list, consists of the following steps: 48

Page 186: C# Language Specification

C# LANGUAGE SPECIFICATION

166

• The set of candidate methods for the method invocation is constructed. For each method F associated 1 with the method group M: 2

o If F is non-generic, F is a candidate when: 3

• M has no type argument list, and 4

• F is applicable with respect to A (§14.4.2.1). 5

o If F is generic and M has no type argument list, F is a candidate when: 6

• Type inference (§26.6.4) succeeds, inferring a list of type arguments for the call, and 7

• Once the inferred type arguments are substituted for the corresponding method type parameters, 8 the parameter list of F is applicable with respect to A (§14.4.2.1), and 9

• The parameter list of F, after substituting type arguments, is not the same as an applicable non-10 generic method, possibly in expanded form (§14.4.2.1), declared in the same type as F. 11

o If F is generic and M includes a type argument list, F is a candidate when: 12

• F has the same number of method type parameters as were supplied in the type argument list, 13 and 14

• Once the type arguments are substituted for the corresponding method type parameters, the 15 parameter list of F is applicable with respect to A (§14.4.2.1). 16

• The set of candidate methods is reduced to contain only methods from the most derived types: For each 17 method C.F in the set, where C is the type in which the method F is declared, all methods declared in a 18 base type of C are removed from the set. Furthermore, if C is a class type other than object, all methods 19 declared in an interface type are removed from the set. [Note: This latter rule only has affect when the 20 method group was the result of a member lookup on a type parameter having an effective base class 21 other than object and a non-empty effective interface set (§26.7). end note] 22

• If the resulting set of candidate methods is empty, then no applicable methods exist, and a compile-time 23 error occurs. If the candidate methods are not all declared in the same type, the method invocation is 24 ambiguous, and a compile-time error occurs (this latter situation can only occur for an invocation of a 25 method in an interface that has multiple direct base interfaces, as described in §20.2.5, or for an 26 invocation of a method on a type parameter). 27

• The best method of the set of candidate methods is identified using the overload resolution rules of 28 §14.4.2. If a single best method cannot be identified, the method invocation is ambiguous, and a 29 compile-time error occurs. When performing overload resolution, the parameters of a generic method 30 are considered after substituting the type arguments (supplied or inferred) for the corresponding method 31 type parameters. 32

• Final validation of the chosen best method is performed: 33

o The method is validated in the context of the method group: If the best method is a static method, 34 the method group shall have resulted from a simple-name or a member-access through a type. If the 35 best method is an instance method, the method group shall have resulted from a simple-name, a 36 member-access through a variable or value, or a base-access. If neither of these requirements is true, 37 a compile-time error occurs. 38

o If the best method is a generic method, the type arguments (supplied or inferred) are checked against 39 the constraints (§26.7.1) declared on the generic method. If any type argument does not satisfy the 40 corresponding constraint(s) on the type parameter, a compile-time error occurs. 41

Once a method has been selected and validated at compile-time by the above steps, the actual run-time 42 invocation is processed according to the rules of function member invocation described in §14.4.3. 43

[Note: The intuitive effect of the resolution rules described above is as follows: To locate the particular 44 method invoked by a method invocation, start with the type indicated by the method invocation and proceed 45

Page 187: C# Language Specification

Chapter 14 Expressions

167

up the inheritance chain until at least one applicable, accessible, non-override method declaration is found. 1 Then perform overload resolution on the set of applicable, accessible, non-override methods declared in that 2 type and invoke the method thus selected. end note] 3

14.5.5.2 Delegate invocations 4

For a delegate invocation, the primary-expression of the invocation-expression shall be a value of a 5 delegate-type. Furthermore, considering the delegate-type to be a function member with the same parameter 6 list as the delegate-type, the delegate-type shall be applicable (§14.4.2.1) with respect to the argument-list of 7 the invocation-expression. 8

The run-time processing of a delegate invocation of the form D(A), where D is a primary-expression of a 9 delegate-type and A is an optional argument-list, consists of the following steps: 10

• D is evaluated. If this evaluation causes an exception, no further steps are executed. 11

• The argument list A is evaluated. If this evaluation causes an exception, no further steps are executed. 12

• The value of D is checked to be valid. If the value of D is null, a 13 System.NullReferenceException is thrown and no further steps are executed. 14

• Otherwise, D is a reference to a delegate instance. A function member invocation (§14.4.3) is performed 15 on each callable entity referenced by the delegate. If invocation of a callable entity causes an exception, 16 the remaining callable entities are not invoked. Otherwise, the return result of the invocation is the result 17 returned from the last callable entity. 18

14.5.6 Element access 19

An element-access consists of a primary-no-array-creation-expression, followed by a “[“ token, followed 20 by an expression-list, followed by a “]” token. The expression-list consists of one or more expressions, 21 separated by commas. 22

element-access: 23 primary-no-array-creation-expression [ expression-list ] 24

expression-list: 25 expression 26 expression-list , expression 27

If the primary-no-array-creation-expression of an element-access is a value of an array-type, the element-28 access is an array access (§14.5.6.1). Otherwise, the primary-no-array-creation-expression shall be a 29 variable or value of a class, struct, or interface type that has one or more indexer members, in which case the 30 element-access is an indexer access (§14.5.6.2). 31

14.5.6.1 Array access 32

For an array access, the primary-no-array-creation-expression of the element-access shall be a value of an 33 array-type. The number of expressions in the expression-list shall be the same as the rank of the array-type, 34 and each expression shall be of type int, uint, long, ulong, or of a type that can be implicitly converted 35 to one or more of these types. 36

The result of evaluating an array access is a variable of the element type of the array, namely the array 37 element selected by the value(s) of the expression(s) in the expression-list. 38

The run-time processing of an array access of the form P[A], where P is a primary-no-array-creation-39 expression of an array-type and A is an expression-list, consists of the following steps: 40

• P is evaluated. If this evaluation causes an exception, no further steps are executed. 41

• The index expressions of the expression-list are evaluated in order, from left to right. Following 42 evaluation of each index expression, an implicit conversion (§13.1) to one of the following types is 43 performed: int, uint, long, ulong. The first type in this list for which an implicit conversion exists is 44

Page 188: C# Language Specification

C# LANGUAGE SPECIFICATION

168

chosen. For instance, if the index expression is of type short then an implicit conversion to int is 1 performed, since implicit conversions from short to int and from short to long are possible. If 2 evaluation of an index expression or the subsequent implicit conversion causes an exception, then no 3 further index expressions are evaluated and no further steps are executed. 4

• The value of P is checked to be valid. If the value of P is null, a 5 System.NullReferenceException is thrown and no further steps are executed. 6

• The value of each expression in the expression-list is checked against the actual bounds of each 7 dimension of the array instance referenced by P. If one or more values are out of range, a 8 System.IndexOutOfRangeException is thrown and no further steps are executed. 9

• The location of the array element given by the index expression(s) is computed, and this location 10 becomes the result of the array access. 11

14.5.6.2 Indexer access 12

For an indexer access, the primary-no-array-creation-expression of the element-access shall be a variable 13 or value of a class, struct, or interface type, and this type shall implement one or more indexers that are 14 applicable with respect to the expression-list of the element-access. 15

The compile-time processing of an indexer access of the form P[A], where P is a primary-no-array-16 creation-expression of a class, struct, or interface type T, and A is an expression-list, consists of the 17 following steps: 18

• The set of indexers provided by T is constructed. The set consists of all indexers declared in T or a base 19 type of T that are not override declarations and are accessible in the current context (§10.5). 20

• The set is reduced to those indexers that are applicable and not hidden by other indexers. The following 21 rules are applied to each indexer S.I in the set, where S is the type in which the indexer I is declared: 22

o If I is not applicable with respect to A (§14.4.2.1), then I is removed from the set. 23

o If I is applicable with respect to A (§14.4.2.1), then all indexers declared in a base type of S are 24 removed from the set. 25

• If the resulting set of candidate indexers is empty, then no applicable indexers exist, and a compile-time 26 error occurs. If the candidate indexers are not all declared in the same type, the indexer access is 27 ambiguous, and a compile-time error occurs (this latter situation can only occur for an indexer access on 28 an instance of an interface that has multiple direct base interfaces, or for an invocation of an indexer on a 29 type parameter). 30

• The best indexer of the set of candidate indexers is identified using the overload resolution rules of 31 §14.4.2. If a single best indexer cannot be identified, the indexer access is ambiguous, and a compile-32 time error occurs. 33

• The index expressions of the expression-list are evaluated in order, from left to right. The result of 34 processing the indexer access is an expression classified as an indexer access. The indexer access 35 expression references the indexer determined in the step above, and has an associated instance 36 expression of P and an associated argument list of A. 37

Depending on the context in which it is used, an indexer access causes invocation of either the get-accessor 38 or the set-accessor of the indexer. If the indexer access is the target of an assignment, the set-accessor is 39 invoked to assign a new value (§14.13.1). In all other cases, the get-accessor is invoked to obtain the current 40 value (§14.1.1). 41

14.5.7 This access 42

A this-access consists of the reserved word this. 43

this-access: 44 this 45

Page 189: C# Language Specification

Chapter 14 Expressions

169

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance 1 accessor. It has one of the following meanings: 2

• When this is used in a primary-expression within an instance constructor of a class, it is classified as a 3 value. The type of the value is the class within which the usage occurs, and the value is a reference to the 4 object being constructed. 5

• When this is used in a primary-expression within an instance method or instance accessor of a class, it 6 is classified as a value. The type of the value is the class within which the usage occurs, and the value is 7 a reference to the object for which the method or accessor was invoked. 8

• When this is used in a primary-expression within an instance constructor of a struct, it is classified as a 9 variable. The type of the variable is the struct within which the usage occurs, and the variable represents 10 the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the 11 same as: 12

o an out parameter of the struct type if the constructor declaration has no constructor initializer. In 13 particular, this means that the variable shall be definitely assigned in every execution path of the 14 instance constructor. 15

o a ref parameter of the struct type if the constructor declaration has a constructor initializer. In 16 particular, this means that the variable is considered initially assigned. 17

• When this is used in a primary-expression within an instance method or instance accessor of a struct, it 18 is classified as a variable. The type of the variable is the struct within which the usage occurs, and the 19 variable represents the struct for which the method or accessor was invoked. The this variable of an 20 instance method of a struct behaves exactly the same as a ref parameter of the struct type. 21

Use of this in a primary-expression in a context other than the ones listed above is a compile-time error. In 22 particular, it is not possible to refer to this in a static method, a static property accessor, or in a variable-23 initializer of a field declaration. 24

14.5.8 Base access 25

A base-access consists of the reserved word base followed by either a “.” token and an identifier and 26 optional type-argument-list or an expression-list enclosed in square brackets: 27

base-access: 28 base . identifier type-argument-listopt 29 base [ expression-list ] 30

A base-access is used to access base class members that are hidden by similarly named members in the 31 current class or struct. A base-access is permitted only in the block of an instance constructor, an instance 32 method, or an instance accessor. When base.I occurs in a class or struct, I shall denote a member of the 33 base class of that class or struct. Likewise, when base[E] occurs in a class, an applicable indexer shall exist 34 in the base class. 35

At compile-time, base-access expressions of the form base.I and base[E] are evaluated exactly as if they 36 were written ((B)this).I and ((B)this)[E], where B is the base class of the class or struct in which 37 the construct occurs. Thus, base.I and base[E] correspond to this.I and this[E], except this is 38 viewed as an instance of the base class. 39

When a base-access references a virtual function member (a method, property, or indexer), the 40 determination of which function member to invoke at run-time (§14.4.3) is changed. The function member 41 that is invoked is determined by finding the most derived implementation (§17.5.3) of the function member 42 with respect to B (instead of with respect to the run-time type of this, as would be usual in a non-base 43 access). Thus, within an override of a virtual function member, a base-access can be used to invoke the 44 inherited implementation of the function member. If the function member referenced by a base-access is 45 abstract, a compile-time error occurs. 46

Page 190: C# Language Specification

C# LANGUAGE SPECIFICATION

170

14.5.9 Postfix increment and decrement operators 1

post-increment-expression: 2 primary-expression ++ 3

post-decrement-expression: 4 primary-expression -- 5

The operand of a postfix increment or decrement operation shall be an expression classified as a variable, a 6 property access, or an indexer access. The result of the operation is a value of the same type as the operand. 7

If the operand of a postfix increment or decrement operation is a property or indexer access, the property or 8 indexer shall have both a get and a set accessor. If this is not the case, a compile-time error occurs. 9

Unary operator overload resolution (§14.2.3) is applied to select a specific operator implementation. 10 Predefined ++ and -- operators exist for the following operand types: sbyte, byte, short, ushort, int, 11 uint, long, ulong, char, float, double, decimal, and any enum type. The result type of each of these 12 predefined operators is the same as the operand type. The predefined ++ operators return the value produced 13 by adding 1 to the operand, and the predefined -- operators return the value produced by subtracting 1 from 14 the operand. In a checked context, if the result of this addition or subtraction is outside the range of the 15 result type, a System.OverflowException is thrown. 16

There shall be an implicit conversion from the return type of the selected unary operator to the type of the 17 primary-expression, otherwise a compile-time error occurs. 18

The run-time processing of a postfix increment or decrement operation of the form x++ or x-- consists of 19 the following steps: 20

• If x is classified as a variable: 21

o x is evaluated to produce the variable. 22

o The value of x is saved. 23

o The saved value of x is converted to the operand type of the selected operator and the operator is 24 invoked with this value as its argument. 25

o The value returned by the operator is converted to the type of x and stored in the location given by 26 the evaluation of x. 27

o The saved value of x becomes the result of the operation. 28

• If x is classified as a property or indexer access: 29

o The instance expression (if x is not static) and the argument list (if x is an indexer access) 30 associated with x are evaluated, and the results are used in the subsequent get and set accessor 31 invocations. 32

o The get accessor of x is invoked and the returned value is saved. 33

o The saved value of x is converted to the operand type of the selected operator and the operator is 34 invoked with this value as its argument. 35

o The value returned by the operator is converted to the type of x and the set accessor of x is invoked 36 with this value as its value argument. 37

o The saved value of x becomes the result of the operation. 38

The ++ and -- operators also support prefix notation (§14.6.5). The result of x++ or x-- is the value of 39 x before the operation, whereas the result of ++x or --x is the value of x after the operation. In either 40 case, x itself has the same value after the operation. 41

An operator ++ or operator -- implementation can be invoked using either postfix or prefix notation. 42 It is not possible to have separate operator implementations for the two notations. 43

Page 191: C# Language Specification

Chapter 14 Expressions

171

14.5.10 The new operator 1

The new operator is used to create new instances of types. 2

There are three forms of new expressions: 3

• Object creation expressions are used to create new instances of class types and value types. 4

• Array creation expressions are used to create new instances of array types. 5

• Delegate creation expressions are used to create new instances of delegate types. 6

The new operator implies creation of an instance of a type, but does not necessarily imply dynamic 7 allocation of memory. In particular, instances of value types require no additional memory beyond the 8 variables in which they reside, and no dynamic allocations occur when new is used to create instances of 9 value types. 10

14.5.10.1 Object creation expressions 11

An object-creation-expression is used to create a new instance of a class-type or a value-type. 12

object-creation-expression: 13 new type ( argument-listopt ) 14

The type of an object-creation-expression shall be a class-type, a value-type, or a type-parameter having the 15 constructor-constraint or the value type constraint (§26.7). The type cannot be an abstract class-type. 16

The optional argument-list (§14.4.1) is permitted only if the type is a class-type or a struct-type, and not a 17 type-parameter. 18

The compile-time processing of an object-creation-expression of the form new T(A), where T is a class-19 type, a value-type, or a type-parameter, and A is an optional argument-list, consists of the following steps: 20

• If T is a value-type and A is not present: 21

o The object-creation-expression is a default constructor invocation. The result of the object-creation-22 expression is a value of type T, namely the default value for T as defined in §11.1.1. 23

• Otherwise, if T is a class-type or a struct-type: 24

o If T is an abstract class-type, a compile-time error occurs. 25

o The instance constructor to invoke is determined using the overload resolution rules of §14.4.2. The 26 set of candidate instance constructors consists of all accessible instance constructors declared in T. If 27 the set of candidate instance constructors is empty, or if a single best instance constructor cannot be 28 identified, a compile-time error occurs. 29

o The result of the object-creation-expression is a value of type T, namely the value produced by 30 invoking the instance constructor determined in the step above. 31

• Otherwise, if T is a type-parameter: 32

o If A is present, a compile-time error occurs. 33

o Otherwise, if T has the constructor-constraint or the value type constraint, the result of the object-34 creation-expression is a value of type T. 35

o Otherwise, the object-creation-expression is invalid, and a compile-time error occurs. 36

• Otherwise, the object-creation-expression is invalid, and a compile-time error occurs. 37

The run-time processing of an object-creation-expression of the form new T(A), where T is class-type, a 38 struct-type, or a type-parameter, and A is an optional argument-list, consists of the following steps: 39

• If T is a class-type: 40

o A new instance of class T is allocated. If there is not enough memory available to allocate the new 41 instance, a System.OutOfMemoryException is thrown and no further steps are executed. 42

Page 192: C# Language Specification

C# LANGUAGE SPECIFICATION

172

o All fields of the new instance are initialized to their default values (§12.2). 1

o The instance constructor is invoked according to the rules of function member invocation (§14.4.3). 2 A reference to the newly allocated instance is automatically passed to the instance constructor and 3 the instance can be accessed from within that constructor as this. 4

• If T is a struct-type: 5

o An instance of type T is created by allocating a temporary local variable. Since an instance 6 constructor of a struct-type is required to definitely assign a value to each field of the instance being 7 created, no initialization of the temporary variable is necessary. 8

o The instance constructor is invoked according to the rules of function member invocation (§14.4.3). 9 A reference to the newly allocated instance is automatically passed to the instance constructor and 10 the instance can be accessed from within that constructor as this. 11

• If T is a type-parameter: 12

o If T evaluates at run-time to a value type, the result is the default value of this type (§11.1.2). This is 13 always the case if T has the value type constraint. 14

o Otherwise T shall evaluate at run-time to a non-abstract class type having a public constructor taking 15 no parameters. The result is a new instance of this class type: 16

• A new instance of the class is allocated. If there is not enough memory available to allocate the 17 new instance, a System.OutOfMemoryException is thrown and no further steps are 18 executed. 19

• All fields of the new instance are initialized to their default values (§12.2). 20

• The instance constructor is invoked according to the rules of function member invocation 21 (§14.4.3). A reference to the newly allocated instance is automatically passed to the instance 22 constructor and the instance can be accessed from within that constructor as this. 23

[Note: The run-time type need not be accessible to the code containing the object-creation-24 expression. Nevertheless, the run-time environment shall support invoking the constructor. end note] 25

26

14.5.10.2 Array creation expressions 27

An array-creation-expression is used to create a new instance of an array-type. 28

array-creation-expression: 29 new non-array-type [ expression-list ] rank-specifiersopt array-initializeropt 30 new array-type array-initializer 31

An array creation expression of the first form allocates an array instance of the type that results from 32 deleting each of the individual expressions from the expression list. [Example: The array creation expression 33 new int[10,20] produces an array instance of type int[,], and the array creation expression new 34 int[10][,] produces an array of type int[][,]. end example] Each expression in the expression list 35 shall be of type int, uint, long, or ulong, or of a type that can be implicitly converted to one or more of 36 these types. The value of each expression determines the length of the corresponding dimension in the newly 37 allocated array instance. Since the length of an array dimension shall be nonnegative, it is a compile-time 38 error to have a constant expression with a negative value, in the expression list. 39

Except in an unsafe context (§25.1), the layout of arrays is unspecified. 40

If an array creation expression of the first form includes an array initializer, each expression in the 41 expression list shall be a constant expression (§14.15) and the rank and dimension lengths specified by the 42 expression list shall match those of the array initializer. 43

Page 193: C# Language Specification

Chapter 14 Expressions

173

In an array creation expression of the second form, the rank of the specified array type shall match that of 1 the array initializer. The individual dimension lengths are inferred from the number of elements in each of 2 the corresponding nesting levels of the array initializer. Thus, the expression 3

new int[,] {{0, 1}, {2, 3}, {4, 5}} 4

exactly corresponds to 5

new int[3, 2] {{0, 1}, {2, 3}, {4, 5}} 6

Array initializers are described further in §19.7. 7

The result of evaluating an array creation expression is classified as a value, namely a reference to the newly 8 allocated array instance. The run-time processing of an array creation expression consists of the following 9 steps: 10

• The dimension length expressions of the expression-list are evaluated in order, from left to right. 11 Following evaluation of each expression, an implicit conversion (§13.1) to one of the following types is 12 performed: int, uint, long, ulong. The first type in this list for which an implicit conversion exists is 13 chosen. If evaluation of an expression or the subsequent implicit conversion causes an exception, then 14 no further expressions are evaluated and no further steps are executed. 15

• The computed values for the dimension lengths are validated, as follows: If one or more of the values 16 are less than zero, a System.OverflowException is thrown and no further steps are executed. 17

• An array instance with the given dimension lengths is allocated. If there is not enough memory available 18 to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are 19 executed. 20

• All elements of the new array instance are initialized to their default values (§12.2). 21

• If the array creation expression contains an array initializer, then each expression in the array initializer 22 is evaluated and assigned to its corresponding array element. The evaluations and assignments are 23 performed in the order the expressions are written in the array initializer—in other words, elements are 24 initialized in increasing index order, with the rightmost dimension increasing first. If evaluation of a 25 given expression or the subsequent assignment to the corresponding array element causes an exception, 26 then no further elements are initialized (and the remaining elements will thus have their default values). 27

An array creation expression permits instantiation of an array with elements of an array type, but the 28 elements of such an array shall be manually initialized. [Example: The statement 29

int[][] a = new int[100][]; 30

creates a single-dimensional array with 100 elements of type int[]. The initial value of each element is 31 null. It is not possible for the same array creation expression to also instantiate the sub-arrays (without an 32 array initializer), and the statement 33

int[][] a = new int[100][5]; // Error 34

results in a compile-time error. Instantiation of the sub-arrays can instead be performed manually or using an 35 array initializer, as in 36

int[][] a = new int[100][]; 37 for (int i = 0; i < 100; i++) a[i] = new int[5]; 38

int[][] b = new int[3][] { new int[1], new int[2], new int[3] }; 39

end example] 40

[Note:When an array of arrays has a “rectangular” shape, that is when the sub-arrays are all of the same 41 length, it is more efficient to use a multi-dimensional array. In the example above, instantiation of the array 42 of arrays creates 101 objects—one outer array and 100 sub-arrays. In contrast, 43

int[,] = new int[100, 5]; 44

creates only a single object, a two-dimensional array, and accomplishes the allocation in a single statement. 45 end note] 46

Page 194: C# Language Specification

C# LANGUAGE SPECIFICATION

174

14.5.10.3 Delegate creation expressions 1

A delegate-creation-expression is used to create a new instance of a delegate-type. 2

delegate-creation-expression: 3 new delegate-type ( expression ) 4

The argument of a delegate creation expression shall be a method group (§14.1), an anonymous-method-5 expression (§14.5.14), or a value of a delegate-type. If the argument is a method group, it identifies the 6 method, type arguments (for a generic method) and, for an instance method, the object for which to create a 7 delegate. If the argument is a value of a delegate-type, it identifies a delegate instance of which to create a 8 copy. 9

If the expression is an anonymous-method-expression (§14.5.14), then the anonymous method is converted 10 to the given delegate type using the implicit conversion rules defined in §13.5. [Example: If D is a delegate 11 type, then the expression 12

new D(delegate { Console.WriteLine("hello"); }) 13

is equivalent to the expression 14

(D) delegate { Console.WriteLine("hello"); } 15

end example] 16

The compile-time processing of a delegate-creation-expression of the form new D(E), where D is a 17 delegate-type and E is an expression, consists of the following steps: 18

• If E is a method group: 19

o A single method is selected corresponding to a method invocation (§14.5.5.1) of the form E(A), 20 with the following modifications: 21

• The parameter types and modifiers (ref or out) of D are used as the argument types and 22 modifiers of the argument list A. 23

• The candidate methods considered are only those methods that are applicable in their normal 24 form (§14.4.2.1), not those applicable only in their expanded form. 25

• If the algorithm of §14.5.5.1 produces an error, then a compile-time error occurs. Otherwise the 26 algorithm produces a single best method M, together with its type arguments (if the method is 27 generic), having the same number of parameters as D. 28

o If the selected M is not consistent (§22.1) with the delegate type D, a compiler-time error occurs. 29

o If the method M is an instance method, the instance expression associated with E determines the 30 target object of the delegate. 31

o The result is a value of type D, namely a newly created delegate that refers to the method M and 32 target object. 33

• Otherwise, if E is a value of a delegate-type: 34

o If D and E have different numbers of parameters, a compile-time error occurs. 35

o If there is not an identity conversion or implicit reference conversion from the return type of E to the 36 return type of D, a compile-time error occors. 37

o If for some parameter of D there is not an identity conversion or implicit reference conversion from 38 the parameter type to the corresponding parameter type of E, a compile-time error occurs. 39

o If any parameter of D or E has a parameter modifier (ref or out), then the other corresponding 40 parameter must have the same modifier and the same type. 41

o The result is a value of type D, namely a newly created delegate that refers to the same invocation 42 list as E. 43

• Otherwise, the delegate creation expression is invalid, and a compile-time error occurs. 44

Page 195: C# Language Specification

Chapter 14 Expressions

175

The run-time processing of a delegate-creation-expression of the form new D(E), where D is a delegate-type 1 and E is an expression, consists of the following steps: 2

• If E is a method group: 3

o If the method selected at compile-time is a static method, the target object of the delegate is null. 4 Otherwise, the selected method is an instance method, and the target object of the delegate is 5 determined from the instance expression associated with E: 6

• The instance expression is evaluated. If this evaluation causes an exception, no further steps are 7 executed. 8

• If the instance expression is of a reference-type, the value computed by the instance expression 9 becomes the target object. If the target object is null, a System.NullReferenceException 10 is thrown and no further steps are executed. 11

• If the instance expression is of a value-type, a boxing operation (§11.3.1) is performed to 12 convert the value to an object, and this object becomes the target object. 13

o A new instance of the delegate type D is allocated. If there is not enough memory available to 14 allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are 15 executed. 16

o The new delegate instance is initialized with a reference to the method that was determined at 17 compile-time and a reference to the target object computed above. 18

• If E is a value of a delegate-type: 19

o E is evaluated. If this evaluation causes an exception, no further steps are executed. 20

o If the value of E is null, a System.NullReferenceException is thrown and no further steps 21 are executed. 22

o A new instance of the delegate type D is allocated. If there is not enough memory available to 23 allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are 24 executed. 25

o The new delegate instance is initialized with references to the same invocation list as the delegate 26 instance given by E. 27

The method and object to which a delegate refers are determined when the delegate is instantiated and then 28 remain constant for the entire lifetime of the delegate. In other words, it is not possible to change the target 29 method or object of a delegate once it has been created. [Note: Remember, when two delegates are 30 combined or one is removed from another, a new delegate results; no existing delegate has its content 31 changed. end note] 32

It is not possible to create a delegate that refers to a property, indexer, user-defined operator, instance 33 constructor, destructor, or static constructor. 34

[Example: As described above, when a delegate is created from a method group, the formal parameter list 35 and return type of the delegate determine which of the overloaded methods to select. In the example 36

delegate double DoubleFunc(double x); 37

class A 38 { 39 DoubleFunc f = new DoubleFunc(Square); 40

static float Square(float x) { 41 return x * x; 42 } 43

static double Square(double x) { 44 return x * x; 45 } 46 } 47

Page 196: C# Language Specification

C# LANGUAGE SPECIFICATION

176

the A.f field is initialized with a delegate that refers to the second Square method because overload 1 resolution with an argument list consisting of a lone double value would choose that method. Had the 2 second Square method not been present, a compile-time error would have occurred. 3

The definition of a method being consistent with a delegate type (§22.1) permits co-variance in the return 4 type and contra-variance in the parameter types. That is, the method return type may be more specific than 5 the delegate return type and the method parameter types may be less specific than the delegate parameter 6 types. In the example 7

delegate object StrToObj(string s); 8

class A 9 { 10 StrToObj f = new StrToObj(ObjToStr); 11

static string ObjToStr(object x) { 12 return x.ToString(); 13 } 14

} 15

the method ObjToStr is used to create a delegate of type StrToObj. The method is consistent with the 16 delegate type since there is an implicit reference conversion from the parameter type of the delegate to the 17 parameter type of the method and an implicit reference conversion from the return type of the method to the 18 return type of the delegate. end example] 19

14.5.11 The typeof operator 20

The typeof operator is used to obtain the System.Type object for a type. 21

typeof-expression: 22 typeof ( type ) 23 typeof ( unbound-type-name ) 24 typeof ( void ) 25

unbound-type-name: 26 identifier generic-dimension-specifieropt 27 identifier :: identifier generic-dimension-specifieropt 28 unbound-type-name . identifier generic-dimension-specifieropt 29

generic-dimension-specifier: 30 < commasopt > 31

commas: 32 , 33 commas , 34

The first form of typeof-expression consists of a typeof keyword followed by a parenthesized type. The 35 result of an expression of this form is the System.Type object for the indicated type. There is only one 36 System.Type object for any given type. [Note: This means that for type T, typeof(T) == typeof(T) 37 is always true. end note] 38

The second form of typeof-expression consists of a typeof keyword followed by a parenthesized unbound-39 type-name. [Note: An unbound-type-name is very similar to a type-name (§10.8) except that an unbound-40 type-name contains generic-dimension-specifiers where a type-name contains type-argument-lists. end note] 41 When the operand of a typeof-expression is a sequence of tokens that satisfies the grammars of both 42 unbound-type-name and type-name, namely when it contains neither a generic-dimension-specifier nor a 43 type-argument-list, the sequence of tokens is considered to be a type-name. The meaning of an unbound-44 type-name is determined as follows: 45

• Convert the sequence of tokens to a type-name by replacing each generic-dimension-specifier with a 46 type-argument-list having the same number of commas and the keyword object as each type-47 argument. 48

Page 197: C# Language Specification

Chapter 14 Expressions

177

• Evaluate the resulting type-name, while ignoring all type parameter constraints. 1

• The unbound-type-name resolves to the unbound generic type associated with the resulting constructed 2 type (§26.5). 3

The result of the typeof-expression is the System.Type object for the resulting unbound generic type. 4

The third form of typeof-expression consists of a typeof keyword followed by a parenthesized void 5 keyword. The result of an expression of this form is the System.Type object that represents the absence of 6 a type. The type object returned by typeof(void) is distinct from the type object returned for any type. 7 [Note: This special type object is useful in class libraries that allow reflection onto methods in the language, 8 where those methods wish to have a way to represent the return type of any method, including void methods, 9 with an instance of System.Type. end note] 10

[Example: The example 11

using System; 12

class Test 13 { 14 static void Main() { 15 Type[] t = { 16 typeof(int), 17 typeof(System.Int32), 18 typeof(string), 19 typeof(double[]), 20 typeof(void) }; 21 for (int i = 0; i < t.Length; i++) { 22 Console.WriteLine(t[i].FullName); 23 } 24 } 25 } 26

produces the following output: 27

System.Int32 28 System.Int32 29 System.String 30 System.Double[] 31 System.Void 32

Note that int and System.Int32 are the same type. end example] 33

The typeof operator can be used on a type-parameter (§26.1.1). The result is the System.Type object for 34 the run-time type that was bound to the type-parameter. The typeof operator can also be used on a 35 constructed type (§26.5) or an unbound generic type. The System.Type object for an unbound generic type 36 is not the same as the System.Type object of the instance type. The instance type is always a closed 37 constructed type at run-time so its System.Type object depends on the actual type arguments in use, while 38 the unbound generic type has no type arguments. [Example: 39

class X<T> 40 { 41 public static void PrintTypes() { 42 Console.WriteLine(typeof(T).FullName); 43 Console.WriteLine(typeof(X<T>).FullName); 44 Console.WriteLine(typeof(X<X<T>>).FullName); 45 Console.WriteLine(typeof(X<>).FullName); 46 } 47 } 48

class M 49 { 50 static void Main() { 51 X<int>.PrintTypes(); 52 X<string>.PrintTypes(); 53 } 54 } 55

The above program will print: 56

Page 198: C# Language Specification

C# LANGUAGE SPECIFICATION

178

System.Int32 1 x`1[System.Int32] 2 X`1[X`1[System.Int32]] 3 X`1[T] 4

System.String 5 x`1[System.String] 6 X`1[X`1[System.String]] 7 X`1[T] 8

Note that the result of typeof(X<>) does not depend on the type argument but the result of 9 typeof(X<T>) does depend on the type argument. end example] 10

14.5.12 The checked and unchecked operators 11

The checked and unchecked operators are used to control the overflow checking context for integral-type 12 arithmetic operations and conversions. 13

checked-expression: 14 checked ( expression ) 15

unchecked-expression: 16 unchecked ( expression ) 17

The checked operator evaluates the contained expression in a checked context, and the unchecked 18 operator evaluates the contained expression in an unchecked context. A checked-expression or unchecked-19 expression corresponds exactly to a parenthesized-expression (§14.5.3), except that the contained expression 20 is evaluated in the given overflow checking context. 21

The overflow checking context can also be controlled through the checked and unchecked statements 22 (§15.11). 23

The following operations are affected by the overflow checking context established by the checked and 24 unchecked operators and statements: 25

• The predefined ++ and -- unary operators (§14.5.9 and §14.6.5), when the operand type is an integral 26 or enum type. 27

• The predefined - unary operator (§14.6.2), when the operand type is an integral type. 28

• The predefined +, -, *, and / binary operators (§14.7), when the operand types are integral or enum 29 types. 30

• Explicit numeric conversions (§13.2.1) from one integral or enum type to another integral or enum type, 31 or from float or double to an integral or enum type. 32

When one of the above operations produces a result that is too large to represent in the destination type, the 33 context in which the operation is performed controls the resulting behavior: 34

• In a checked context, if the operation is a constant expression (§14.15), a compile-time error occurs. 35 Otherwise, when the operation is performed at run-time, a System.OverflowException is thrown. 36

• In an unchecked context, the result is truncated by discarding any high-order bits that do not fit in the 37 destination type. 38

For non-constant expressions (expressions that are evaluated at run-time) that are not enclosed by any 39 checked or unchecked operators or statements, the default overflow checking context is unchecked, 40 unless external factors (such as compiler switches and execution environment configuration) call for 41 checked evaluation. 42

For constant expressions (expressions that can be fully evaluated at compile-time), the default overflow 43 checking context is always checked. Unless a constant expression is explicitly placed in an unchecked 44 context, overflows that occur during the compile-time evaluation of the expression always cause compile-45 time errors. 46

Page 199: C# Language Specification

Chapter 14 Expressions

179

[Note: Developers might benefit if they exercise their code using checked mode (as well as unchecked 1 mode). It also seems reasonable that, unless otherwise requested, the default overflow checking context is 2 set to checked when debugging is enabled. end note] 3

[Example: In the following code 4

class Test 5 { 6 static readonly int x = 1000000; 7 static readonly int y = 1000000; 8

static int F() { 9 return checked(x * y); // Throws OverflowException 10 } 11

static int G() { 12 return unchecked(x * y); // Returns -727379968 13 } 14

static int H() { 15 return x * y; // Depends on default 16 } 17 } 18

no compile-time errors are reported since neither of the expressions can be evaluated at compile-time. At 19 run-time, the F method throws a System.OverflowException, and the G method returns –727379968 20 (the lower 32 bits of the out-of-range result). The behavior of the H method depends on the default overflow 21 checking context for the compilation, but it is either the same as F or the same as G. end example] 22

[Example: In the following code 23

class Test 24 { 25 const int x = 1000000; 26 const int y = 1000000; 27

static int F() { 28 return checked(x * y); // Compile error, overflow 29 } 30

static int G() { 31 return unchecked(x * y); // Returns -727379968 32 } 33

static int H() { 34 return x * y; // Compile error, overflow 35 } 36 } 37

the overflows that occur when evaluating the constant expressions in F and H cause compile-time errors to 38 be reported because the expressions are evaluated in a checked context. An overflow also occurs when 39 evaluating the constant expression in G, but since the evaluation takes place in an unchecked context, the 40 overflow is not reported. end example] 41

The checked and unchecked operators only affect the overflow checking context for those operations that 42 are textually contained within the “(” and “)” tokens. The operators have no effect on function members 43 that are invoked as a result of evaluating the contained expression. [Example: In the following code 44

class Test 45 { 46 static int Multiply(int x, int y) { 47 return x * y; 48 } 49

static int F() { 50 return checked(Multiply(1000000, 1000000)); 51 } 52 } 53

the use of checked in F does not affect the evaluation of x * y in Multiply, so x * y is evaluated in 54 the default overflow checking context. end example] 55

Page 200: C# Language Specification

C# LANGUAGE SPECIFICATION

180

The unchecked operator is convenient when writing constants of the signed integral types in hexadecimal 1 notation. [Example: 2

class Test 3 { 4 public const int AllBits = unchecked((int)0xFFFFFFFF); 5

public const int HighBit = unchecked((int)0x80000000); 6 } 7

Both of the hexadecimal constants above are of type uint. Because the constants are outside the int range, 8 without the unchecked operator, the casts to int would produce compile-time errors. end example] 9

[Note: The checked and unchecked operators and statements allow programmers to control certain 10 aspects of some numeric calculations. However, the behavior of some numeric operators depends on their 11 operands’ data types. For example, multiplying two decimals always results in an exception on overflow 12 even within an explicitly unchecked construct. Similarly, multiplying two floats never results in an 13 exception on overflow even within an explicitly checked construct. In addition, other operators are never 14 affected by the mode of checking, whether default or explicit. As a service to programmers, it is 15 recommended that the compiler issue a warning when there is an arithmetic expression within an explicitly 16 checked or unchecked context (by operator or statement) that cannot possibly be affected by the specified 17 mode of checking. Since such a warning is not required, the compiler has flexibility in determining the 18 circumstances that merit the issuance of such warnings. end note] 19

14.5.13 Default value expression 20

A default value expression is used to obtain the default value (§12.2) of a type. Typically a default value 21 expression is used for type parameters (§26.1.1), since it might not be known if the type parameter is a value 22 type or a reference type. (No conversion exists from the null literal to a type parameter unless the type 23 parameter is known to be a reference type (§26.7).) 24

default-value-expression: 25 default ( type ) 26

If the type in a default-value-expression evaluates at run-time to a reference type, the result is null 27 converted to that type. If the type in a default-value-expression evaluates at run-time to a value type, the 28 result is the value-type’s default value (§11.1.2). 29

A default-value-expression is a constant expression (§14.15) if the type is a reference type or a type 30 parameter that is known to be a reference type (§26.7). In addition, a default-value-expression is a constant 31 expression if the type is one of the following value types: sbyte, byte, short, ushort, int, uint, long, 32 ulong, char, float, double, decimal, or bool. 33

14.5.14 Anonymous methods 34

An anonymous-method-expression defines an anonymous method and evaluates to a value referencing the 35 method: 36

anonymous-method-expression: 37 delegate anonymous-method-signatureopt block 38

anonymous-method-signature: 39 ( anonymous-method-parameter-listopt ) 40

anonymous-method-parameter-list: 41 anonymous-method-parameter 42 anonymous-method-parameter-list , anonymous-method-parameter 43

anonymous-method-parameter: 44 parameter-modifieropt type identifier 45

An anonymous-method-expression is classified as a value with no type, and with special conversion rules 46 (§13.5). The value does not have a type but can be implicitly converted to a compatible delegate type. 47

Page 201: C# Language Specification

Chapter 14 Expressions

181

An anonymous-method-expression can be used in a delegate-creation-expression (§14.5.10.3). All other 1 valid uses of an anonymous-method-expression depend on the implicit conversions defined in §13.5. 2

An anonymous-method-expression defines a new declaration space for parameters, locals and constants and 3 a new declaration space for labels (§10.3). 4

14.5.14.1 Anonymous method signatures 5

The optional anonymous-method-signature defines the names and types of the formal parameters for the 6 anonymous method. The scope of the parameters of the anonymous method is the block. It is a compile-time 7 error for the name of a parameter of the anonymous method to match the name of a local variable, local 8 constant or parameter whose scope includes the anonymous-method-expression. 9

If an anonymous-method-expression has an anonymous-method-signature, then the set of compatible 10 delegate types is restricted to those that have the same parameter types and modifiers, in the same order 11 (§13.5). In constrast to method groups, contra-variance of anonymous method parameter types is not 12 supported (§14.5.10.3). If an anonymous-method-expression doesn’t have an anonymous-method-signature, 13 then the set of compatible delegate types is restricted to those that have no out parameters. 14

Note that an anonymous-method-signature cannot include attributes or a parameter-array. Nevertheless, an 15 anonymous-method-signature may be compatible with a delegate type whose formal-parameter-list contains 16 a parameter-array. 17

14.5.14.2 Anonymous method blocks 18

The block of an anonymous-method-expression is subject to the following rules: 19

• If the anonymous method includes a signature, the parameters specified in the signature are available in 20 the block. If the anonymous method has no signature, it can be converted to a delegate type having 21 parameters (§13.5), but the parameters cannot be accessed in the block. 22

• Except for ref or out parameters specified in the signature (if any) of the nearest enclosing anonymous 23 method, it is a compile-time error for the block to access a ref or out parameter. 24

• When the type of this is a struct type, it is a compile-time error for the block to access this. This is 25 true whether the access is explicit (as in this.x) or implicit (as in x, where x is an instance member of 26 the struct). This rule simply prohibits such access and does not affect whether member lookup results in 27 a member of the struct. 28

• The block has access to the outer variables (§14.5.14.3) of the anonymous method. Access of an outer 29 variable will reference the instance of the variable that is active at the time the anonymous-method-30 expression is evaluated (§14.5.14.4). 31

• It is a compile-time error for the block to contain a goto statement, break statement, or continue 32 statement whose target is outside the block or within the block of a contained anonymous method. 33

• A return statement in the block returns control from an invocation of the nearest enclosing anonymous 34 method, not from the enclosing function member. An expression specified in a return statement shall 35 be compatible with the delegate type to which the nearest enclosing anonymous-method-expression is 36 converted (§14.5.14.4). 37

It is explicitly unspecified whether there is any way to execute the block of an anonymous method other than 38 through evaluation and invocation of the anonymous-method-expression. In particular, a compiler might 39 choose to implement an anonymous method by synthesizing one or more named methods or types. The 40 names of any such synthesized elements shall be in the space reserved for implementations (that is, the 41 names shall contain two consecutive underscore characters). 42

14.5.14.3 Outer variables 43

Any local variable, value parameter, or parameter array whose scope includes an anonymous-method-44 expression is called an outer variable of that anonymous-method-expression. In an instance function 45

Page 202: C# Language Specification

C# LANGUAGE SPECIFICATION

182

member of a class, the this value is considered a value parameter and is an outer variable of any 1 anonymous-method-expression contained within that function member. 2

14.5.14.3.1 Captured outer variables 3

When an outer variable is referenced by an anonymous method, the outer variable is said to have been 4 captured by the anonymous method. Ordinarily, the lifetime of a local variable is limited to execution of the 5 block or statement with which it is associated (§12.1.7). However, the lifetime of a captured outer variable is 6 extended at least until the delegate referring to the anonymous method becomes eligible for garbage 7 collection. 8

[Example: In the following code 9

using System; 10

delegate int D(); 11

class Test 12 { 13 static D F() { 14 int x = 0; 15 D result = delegate { return ++x; }; 16 return result; 17 } 18

static void Main() { 19 D d = F(); 20 Console.WriteLine(d()); 21 Console.WriteLine(d()); 22 Console.WriteLine(d()); 23 } 24 } 25

the local variable x is captured by the anonymous method, and the lifetime of x is extended at least until the 26 delegate returned from F becomes eligible for garbage collection. Since each invocation of the anonymous 27 method operates on the same instance of x, the output of the example is: 28

1 29 2 30 3 31

end example] 32

14.5.14.3.2 Instantiation of local variables 33

A local variable is considered to be instantiated when execution enters the scope of the variable. [Example: 34 When the following method is invoked, the local variable x is instantiated and initialized three times—once 35 each iteration of the loop. 36

static void F() { 37 for (int i = 0; i < 3; i++) { 38 int x = i * 2 + 1; 39 … 40 } 41 } 42

However, moving the declaration of x outside the loop results in a single instantiation of x: 43

static void F() { 44 int x; 45 for (int i = 0; i < 3; i++) { 46 x = i * 2 + 1; 47 … 48 } 49 } 50

end example] 51

Page 203: C# Language Specification

Chapter 14 Expressions

183

Ordinarily, there is no way to observe exactly how often a local variable is instantiated—because the 1 lifetimes of the instantiations are disjoint, it is possible for each instantation to simply use the same storage 2 location. However, when an anonymous method captures a local variable, the effects of instantiation become 3 apparent. [Example: The example 4

using System; 5

delegate void D(); 6

class Test 7 { 8 static D[] F() { 9 D[] result = new D[3]; 10 for (int i = 0; i < 3; i++) { 11 int x = i * 2 + 1; 12 result[i] = delegate { Console.WriteLine(x); }; 13 } 14 return result; 15 } 16

static void Main() { 17 foreach (D d in F()) d(); 18 } 19 } 20

produces the output: 21

1 22 3 23 5 24

However, when the declaration of x is moved outside the loop: 25

static D[] F() { 26 D[] result = new D[3]; 27 int x; 28 for (int i = 0; i < 3; i++) { 29 x = i * 2 + 1; 30 result[i] = delegate { Console.WriteLine(x); }; 31 } 32 return result; 33 } 34

the output is: 35

5 36 5 37 5 38

Note that the three delegates created in the version of F directly above will be equal according to the equality 39 operator (§14.9.8). Furthermore, note that the compiler is permitted (but not required) to optimize the three 40 instantiations into a single delegate instance (§14.5.14.4). end example] 41

It is possible for anonymous method delegates to share some captured variables yet have separate instances 42 of others. [Example: If F is changed to 43

static D[] F() { 44 D[] result = new D[3]; 45 int x = 0; 46 for (int i = 0; i < 3; i++) { 47 int y = 0; 48 result[i] = delegate { Console.WriteLine("{0} {1}", ++x, ++y); }; 49 } 50 return result; 51 } 52

the three delegates capture the same instance of x but separate instances of y, and the output is: 53

1 1 54 2 1 55 3 1 56

Page 204: C# Language Specification

C# LANGUAGE SPECIFICATION

184

end example] 1

Separate anonymous methods can capture the same instance of an outer variable. [Example: In the following 2 code: 3

using System; 4

delegate void Setter(int value); 5

delegate int Getter(); 6

class Test 7 { 8 static void Main() { 9 int x = 0; 10 Setter s = delegate(int value) { x = value; }; 11 Getter g = delegate { return x; }; 12 s(5); 13 Console.WriteLine(g()); 14 s(10); 15 Console.WriteLine(g()); 16 } 17 } 18

the two anonymous methods capture the same instance of the local variable x, and they can thus 19 “communicate” through that variable. The output of the example is: 20

5 21 10 22

end example] 23

14.5.14.4 Anonymous method evaluation 24

The run-time evaluation of an anonymous-method-expression produces a delegate instance which references 25 the anonymous method and the (possibly empty) set of captured outer variables that are active at the time of 26 the evaluation. When a delegate resulting from an anonymous-method-expression is invoked, the body of the 27 anonymous method is executed. The code in the body is executed using the set of captured outer variables 28 referenced by the delegate. 29

The invocation list of a delegate produced from an anonymous-method-expression contains a single entry. 30 The exact target object and target method of the delegate are unspecified. In particular, it is unspecified 31 whether the target object of the delegate is null, the this value of the enclosing function member, or some 32 other object. 33

Evaluation of sematically identical anonymous-method-expressions with the same (possibly empty) set of 34 captured outer variable instances is permitted (but not required) to return the same delegate instance. The 35 term “sematically identical” is used here to mean that execution of the anonymous methods will, in all cases, 36 produce the same effects given the same arguments. [Example: This rule permits code such as the following 37 to be optimized. 38

delegate double Function(double x); 39

class Test 40 { 41 static double[] Apply(double[] vector, Function func) { 42 double[] result = new double[vector.Length]; 43 foreach (int i = 0; i < vector.Length; i++) { 44 result[i] = func(vector[i]); 45 } 46 return result; 47 } 48

Page 205: C# Language Specification

Chapter 14 Expressions

185

static void F(double[] vx, double[] vy) { 1 double[] rx = Apply(vx, delegate(double x) { 2 return Math.Sin(x); 3 }); 4 double[] ry = Apply(vy, delegate(double y) { 5 return Math.Sin(y); 6 }); 7 … 8 } 9 } 10

Since the two anonymous method delegates have the same (empty) set of captured outer variables, and since 11 the anonymous methods are semantically identical, the compiler is permitted to have the delegates refer to 12 the same target method. Indeed, the compiler is permitted to return the very same delegate instance from 13 both anonymous method expressions. 14

end example] 15

14.5.14.5 Implementation example 16

[Note: This subclause describes a possible implementation of anonymous methods in terms of standard 17 C# constructs. The implementation described here is by no means a mandated implementation, nor is it the 18 only one possible. 19

The remainder of this subclause gives several examples of code that contains anonymous methods with 20 different characteristics. For each example, a corresponding translation to code that uses only standard C# 21 constructs is provided. In the examples, the identifier D is assumed to represent the following delegate type: 22

public delegate void D(); 23

The simplest form of an anonymous method is one that captures no outer variables: 24

class Test 25 { 26 static void F() { 27 D d = delegate { Console.WriteLine("test"); }; 28 } 29 } 30

This can be translated to a delegate instantiation that references a compiler-generated static method in which 31 the code of the anonymous method is placed: 32

class Test 33 { 34 static void F() { 35 D d = new D(__Method1); 36 } 37

static void __Method1() { 38 Console.WriteLine("test"); 39 } 40 } 41

In the following example, the anonymous method references instance members of this: 42

class Test 43 { 44 int x; 45

void F() { 46 D d = delegate { Console.WriteLine(x); }; 47 } 48 } 49

This can be translated to a compiler-generated instance method containing the code of the anonymous 50 method: 51

class Test 52 { 53 int x; 54

Page 206: C# Language Specification

C# LANGUAGE SPECIFICATION

186

void F() { 1 D d = new D(__Method1); 2 } 3

void __Method1() { 4 Console.WriteLine(x); 5 } 6 } 7

In this example, the anonymous method captures a local variable: 8

class Test 9 { 10 void F() { 11 int y = 123; 12 D d = delegate { Console.WriteLine(y); }; 13 } 14 } 15

The lifetime of the local variable shall now be extended to at least the lifetime of the anonymous method 16 delegate. This can be achieved by “lifting” the local variable into a field of a compiler-generated class. 17 Instantiation of the local variable (§14.5.14.3.2) then corresponds to creating an instance of the compiler-18 generated class, and accessing the local variable corresponds to accessing a field in the instance of the 19 compiler-generated class. Furthermore, the anonymous method becomes an instance method of the 20 compiler-generated class: 21

class Test 22 { 23 void F() { 24 __Locals1 __locals1 = new __Locals1(); 25 __locals1.y = 123; 26 D d = new D(__locals1.__Method1); 27 } 28

class __Locals1 29 { 30 public int y; 31

public void __Method1() { 32 Console.WriteLine(y); 33 } 34 } 35 } 36

Finally, the following anonymous method captures this as well as two local variables with different 37 lifetimes: 38

class Test 39 { 40 int x; 41

void F() { 42 int y = 123; 43 for (int i = 0; i < 10; i++) { 44 int z = i * 2; 45 D d = delegate { Console.WriteLine(x + y + z); }; 46 } 47 } 48 } 49

Here, a compiler-generated class is created for each statement block in which locals are captured such that 50 the locals in the different blocks can have independent lifetimes. An instance of __Locals2, the compiler-51 generated class for the inner statement block, contains the local variable z and a field that references an 52 instance of __Locals1. An instance of __Locals1, the compiler-generated class for the outer statement 53 block, contains the local variable y and a field that references this of the enclosing function member. With 54 these data structures it is possible to reach all captured outer variables through an instance of __Locals2, 55 and the code of the anonymous method can thus be implemented as an instance method of that class. 56

Page 207: C# Language Specification

Chapter 14 Expressions

187

class Test 1 { 2 void F() { 3 __Locals1 __locals1 = new __Locals1(); 4 __locals1.__this = this; 5 __locals1.y = 123; 6 for (int i = 0; i < 10; i++) { 7 __Locals2 __locals2 = new __Locals2(); 8 __locals2.__locals1 = __locals1; 9 __locals2.z = i * 2; 10 D d = new D(__locals2.__Method1); 11 } 12 } 13

class __Locals1 14 { 15 public Test __this; 16 public int y; 17 } 18

class __Locals2 19 { 20 public __Locals1 __locals1; 21 public int z; 22

public void __Method1() { 23 Console.WriteLine(__locals1.__this.x + __locals1.y + z); 24 } 25 } 26 } 27

end note] 28

14.6 Unary expressions 29

unary-expression: 30 primary-expression 31 + unary-expression 32 - unary-expression 33 ! unary-expression 34 ~ unary-expression 35 pre-increment-expression 36 pre-decrement-expression 37 cast-expression 38

14.6.1 Unary plus operator 39

For an operation of the form +x, unary operator overload resolution (§14.2.3) is applied to select a specific 40 operator implementation. The operand is converted to the parameter type of the selected operator, and the 41 type of the result is the return type of the operator. The predefined unary plus operators are: 42

int operator +(int x); 43 uint operator +(uint x); 44 long operator +(long x); 45 ulong operator +(ulong x); 46 float operator +(float x); 47 double operator +(double x); 48 decimal operator +(decimal x); 49

For each of these operators, the result is simply the value of the operand. 50

14.6.2 Unary minus operator 51

For an operation of the form –x, unary operator overload resolution (§14.2.3) is applied to select a specific 52 operator implementation. The operand is converted to the parameter type of the selected operator, and the 53 type of the result is the return type of the operator. The predefined negation operators are: 54

Page 208: C# Language Specification

C# LANGUAGE SPECIFICATION

188

• Integer negation: 1

int operator –(int x); 2 long operator –(long x); 3

The result is computed by subtracting x from zero. In a checked context, if the value of x is the 4 smallest int or long (−231 or −263, respectively), a System.OverflowException is thrown. In an 5 unchecked context, if the value of x is the smallest int or long, the result is that same value and the 6 overflow is not reported. 7

If the operand of the negation operator is of type uint, it is converted to type long, and the type of the 8 result is long. An exception is the rule that permits the int value −2147483648 (−231) to be written as a 9 decimal integer literal (§9.4.4.2). 10

• Negation of ulong is an error: 11

void operator –(ulong x); 12

Selection of this operator by unary operator overload resolution (§14.2.3) always results in a compile-13 time error. Consequently, if the operand of the negation operator is of type ulong, a compile-time error 14 occurs. An exception is the rule that permits the long value −9223372036854775808 (−263) to be 15 written as a decimal integer literal (§9.4.4.2). 16

• Floating-point negation: 17

float operator –(float x); 18 double operator –(double x); 19

The result is the value of x with its sign inverted. If x is NaN, the result is also NaN. 20

• Decimal negation: 21

decimal operator –(decimal x); 22

The result is computed by subtracting x from zero. 23

Decimal negation is equivalent to using the unary minus operator of type System.Decimal. 24

14.6.3 Logical negation operator 25

For an operation of the form !x, unary operator overload resolution (§14.2.3) is applied to select a specific 26 operator implementation. The operand is converted to the parameter type of the selected operator, and the 27 type of the result is the return type of the operator. Only one predefined logical negation operator exists: 28

bool operator !(bool x); 29

This operator computes the logical negation of the operand: If the operand is true, the result is false. If 30 the operand is false, the result is true. 31

14.6.4 Bitwise complement operator 32

For an operation of the form ~x, unary operator overload resolution (§14.2.3) is applied to select a specific 33 operator implementation. The operand is converted to the parameter type of the selected operator, and the 34 type of the result is the return type of the operator. The predefined bitwise complement operators are: 35

int operator ~(int x); 36 uint operator ~(uint x); 37 long operator ~(long x); 38 ulong operator ~(ulong x); 39

For each of these operators, the result of the operation is the bitwise complement of x. 40

Every enumeration type E implicitly provides the following bitwise complement operator: 41

E operator ~(E x); 42

The result of evaluating ~x, where x is an expression of an enumeration type E with an underlying type U, is 43 exactly the same as evaluating unchecked((E)(~(U)x)). This operator is only considered by unary 44 operator overload resolution when the operand type is the enum type E (§14.2.3). 45

Page 209: C# Language Specification

Chapter 14 Expressions

189

14.6.5 Prefix increment and decrement operators 1

pre-increment-expression: 2 ++ unary-expression 3

pre-decrement-expression: 4 -- unary-expression 5

The operand of a prefix increment or decrement operation shall be an expression classified as a variable, a 6 property access, or an indexer access. The result of the operation is a value of the same type as the operand. 7

If the operand of a prefix increment or decrement operation is a property or indexer access, the property or 8 indexer shall have both a get and a set accessor. If this is not the case, a compile-time error occurs. 9

Unary operator overload resolution (§14.2.3) is applied to select a specific operator implementation. 10 Predefined ++ and -- operators exist for the following operand types: sbyte, byte, short, ushort, int, 11 uint, long, ulong, char, float, double, decimal, and any enum type. The result type of each of these 12 predefined operators is the same as the operand type. The predefined ++ operators return the value produced 13 by adding 1 to the operand, and the predefined -- operators return the value produced by subtracting 1 from 14 the operand. In a checked context, if the result of this addition or subtraction is outside the range of the 15 result type, a System.OverflowException is thrown. 16

There shall be an implicit conversion from the return type of the selected unary operator to the type of the 17 primary-expression, otherwise a compile-time error occurs. 18

The run-time processing of a prefix increment or decrement operation of the form ++x or --x consists of the 19 following steps: 20

• If x is classified as a variable: 21

o x is evaluated to produce the variable. 22

o The value of x is converted to the operand type of the selected operator and the operator is invoked 23 with this value as its argument. 24

o The value returned by the operator is converted to the type of x. The resulting value is stored in the 25 location given by the evaluation of x and becomes the result of the operation. 26

• If x is classified as a property or indexer access: 27

o The instance expression (if x is not static) and the argument list (if x is an indexer access) 28 associated with x are evaluated, and the results are used in the subsequent get and set accessor 29 invocations. 30

o The get accessor of x is invoked. 31

o The value returned by the get accessor is converted to the operand type of the selected operator and 32 operator is invoked with this value as its argument. 33

o The value returned by the operator is converted to the type of x. The set accessor of x is invoked 34 with this value as its value argument. This value also becomes the result of the operation. 35

The ++ and -- operators also support postfix notation (§14.5.9). The result of x++ or x-- is the value of x 36 before the operation, whereas the result of ++x or --x is the value of x after the operation. In either case, x 37 itself has the same value after the operation. 38

An operator ++ or operator -- implementation can be invoked using either postfix or prefix notation. 39 It is not possible to have separate operator implementations for the two notations. 40

14.6.6 Cast expressions 41

A cast-expression is used to explicitly convert an expression to a given type. 42

cast-expression: 43 ( type ) unary-expression 44

Page 210: C# Language Specification

C# LANGUAGE SPECIFICATION

190

A cast-expression of the form (T)E, where T is a type and E is a unary-expression, performs an explicit 1 conversion (§13.2) of the value of E to type T. If no explicit conversion exists from the type of E to T, a 2 compile-time error occurs. Otherwise, the result is the value produced by the explicit conversion. The result 3 is always classified as a value, even if E denotes a variable. 4

The grammar for a cast-expression leads to certain syntactic ambiguities. [Example: The expression 5 (x)–y could either be interpreted as a cast-expression (a cast of –y to type x) or as an additive-6 expression combined with a parenthesized-expression (which computes the value x – y). end example] 7

To resolve cast-expression ambiguities, the following rule exists: A sequence of one or more tokens (§9.4) 8 enclosed in parentheses is considered the start of a cast-expression only if at least one of the following are 9 true: 10

• The sequence of tokens is correct grammar for a type, but not for an expression. 11

• The sequence of tokens is correct grammar for a type, and the token immediately following the closing 12 parentheses is the token “~”, the token “!”, the token “(”, an identifier (§9.4.1), a literal (§9.4.4), or any 13 keyword (§9.4.3) except as and is. 14

The term “correct grammar” above means only that the sequence of tokens shall conform to the 15 particular grammatical production. It specifically does not consider the actual meaning of any 16 constituent identifiers. [Example: If x and y are identifiers, then x.y is correct grammar for a type, even 17 if x.y doesn’t actually denote a type. end example] 18

[Note: From the disambiguation rule, it follows that, if x and y are identifiers, (x)y, (x)(y), and 19 (x)(-y) are cast-expressions, but (x)-y is not, even if x identifies a type. However, if x is a keyword 20 that identifies a predefined type (such as int), then all four forms are cast-expressions (because such a 21 keyword could not possibly be an expression by itself). end note] 22

14.7 Arithmetic operators 23

The *, /, %, +, and – operators are called the arithmetic operators. 24

multiplicative-expression: 25 unary-expression 26 multiplicative-expression * unary-expression 27 multiplicative-expression / unary-expression 28 multiplicative-expression % unary-expression 29

additive-expression: 30 multiplicative-expression 31 additive-expression + multiplicative-expression 32 additive-expression – multiplicative-expression 33

14.7.1 Multiplication operator 34

For an operation of the form x * y, binary operator overload resolution (§14.2.4) is applied to select a 35 specific operator implementation. The operands are converted to the parameter types of the selected 36 operator, and the type of the result is the return type of the operator. 37

The predefined multiplication operators are listed below. The operators all compute the product of x and y. 38

• Integer multiplication: 39

int operator *(int x, int y); 40 uint operator *(uint x, uint y); 41 long operator *(long x, long y); 42 ulong operator *(ulong x, ulong y); 43 void operator *(long x, ulong y); 44 void operator *(ulong x, long y); 45

The operators with void return type always produce a compile-time error. Consequently, it is an error 46 for one operand to be of type long and the other to be of type ulong. 47

Page 211: C# Language Specification

Chapter 14 Expressions

191

In a checked context, if the product is outside the range of the result type, a 1 System.OverflowException is thrown. In an unchecked context, overflows are not reported and 2 any significant high-order bits outside the range of the result type are discarded. 3

• Floating-point multiplication: 4

float operator *(float x, float y); 5 double operator *(double x, double y); 6

The product is computed according to the rules of IEC 60559 arithmetic. The following table lists the 7 results of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s. In the table, x 8 and y are positive finite values. z is the result of x * y, rounded to the nearest representable value. If 9 the magnitude of the result is too large for the destination type, z is infinity. Because of rounding, z may 10 be zero even though neither x nor y is zero. 11

12 +y –y +0 –0 +∞ –∞ NaN

+x +z –z +0 –0 +∞ –∞ NaN

–x –z +z –0 +0 –∞ +∞ NaN

+0 +0 –0 +0 –0 NaN NaN NaN

–0 –0 +0 –0 +0 NaN NaN NaN

+∞ +∞ –∞ NaN NaN +∞ –∞ NaN

–∞ –∞ +∞ NaN NaN –∞ +∞ NaN

NaN NaN NaN NaN NaN NaN NaN NaN

13

• Decimal multiplication: 14

decimal operator *(decimal x, decimal y); 15

If the magnitude of the resulting value is too large to represent in the decimal format, a 16 System.OverflowException is thrown. Because of rounding, the result may be zero even though 17 neither operand is zero. The scale of the result, before any rounding, is the sum of the scales of the two 18 operands. 19

Decimal multiplication is equivalent to using the multiplication operator of type System.Decimal. 20

14.7.2 Division operator 21

For an operation of the form x / y, binary operator overload resolution (§14.2.4) is applied to select a 22 specific operator implementation. The operands are converted to the parameter types of the selected 23 operator, and the type of the result is the return type of the operator. 24

The predefined division operators are listed below. The operators all compute the quotient of x and y. 25

• Integer division: 26

int operator /(int x, int y); 27 uint operator /(uint x, uint y); 28 long operator /(long x, long y); 29 ulong operator /(ulong x, ulong y); 30 void operator /(long x, ulong y); 31 void operator /(ulong x, long y); 32

The operators with void return type always produce a compile-time error. Consequently, it is an error 33 for one operand to be of type long and the other to be of type ulong. 34

If the value of the right operand is zero, a System.DivideByZeroException is thrown. 35

The division rounds the result towards zero, and the absolute value of the result is the largest possible 36 integer that is less than the absolute value of the quotient of the two operands. The result is zero or 37 positive when the two operands have the same sign and zero or negative when the two operands have 38 opposite signs. 39

Page 212: C# Language Specification

C# LANGUAGE SPECIFICATION

192

If the left operand is the smallest int or long value (−231 or −263, respectively) and the right operand is 1 –1, an overflow occurs. In a checked context, this causes a System.ArithmeticException (or a 2 subclass thereof) to be thrown. In an unchecked context, it is implementation-defined as to whether a 3 System.ArithmeticException (or a subclass thereof) is thrown or the overflow goes unreported 4 with the resulting value being that of the left operand. 5

• Floating-point division: 6

float operator /(float x, float y); 7 double operator /(double x, double y); 8

The quotient is computed according to the rules of IEC 60559 arithmetic. The following table lists the 9 results of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s. In the table, x 10 and y are positive finite values. z is the result of x / y, rounded to the nearest representable value. If 11 the magnitude of the result is too large for the destination type, z is infinity. Because of rounding, z may 12 still be zero even though x is not zero and y is not infinite. 13

14 +y –y +0 –0 +∞ –∞ NaN

+x +z –z +∞ –∞ +0 –0 NaN

–x –z +z –∞ +∞ –0 +0 NaN

+0 +0 –0 NaN NaN +0 –0 NaN

–0 –0 +0 NaN NaN –0 +0 NaN

+∞ +∞ –∞ +∞ –∞ NaN NaN NaN

–∞ –∞ +∞ –∞ +∞ NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN

15

• Decimal division: 16

decimal operator /(decimal x, decimal y); 17

If the value of the right operand is zero, a System.DivideByZeroException is thrown. If the 18 magnituded of the resulting value is too large to represent in the decimal format, a 19 System.OverflowException is thrown. Because of rounding, the result may be zero event though 20 the first operand is not zero. The scale of the result, before any rounding, is the closest scale to the 21 preferred scale which will preserve a result equal to the exact result. The preferred scale is the scale of x 22 less the scale of y. 23

Decimal division is equivalent to using the division operator of type System.Decimal. 24

14.7.3 Remainder operator 25

For an operation of the form x % y, binary operator overload resolution (§14.2.4) is applied to select a 26 specific operator implementation. The operands are converted to the parameter types of the selected 27 operator, and the type of the result is the return type of the operator. 28

The predefined remainder operators are listed below. The operators all compute the remainder of the 29 division between x and y. 30

• Integer remainder: 31

int operator %(int x, int y); 32 uint operator %(uint x, uint y); 33 long operator %(long x, long y); 34 ulong operator %(ulong x, ulong y); 35 void operator %(long x, ulong y); 36 void operator %(ulong x, long y); 37

The operators with void return type always produce a compile-time error. Consequently, it is an error 38 for one operand to be of type long and the other to be of type ulong. 39

Page 213: C# Language Specification

Chapter 14 Expressions

193

The result of x % y is the value produced by x – (x / y) * y. If y is zero, a 1 System.DivideByZeroException is thrown. 2

If the left operand is the smallest int or long value (−231 or −263, respectively) and the right operand is 3 –1, it is implementation-defined as to whether a System.ArithmeticException (or a subclass 4 thereof) is thrown. A conforming implementation shall not throw an exception for x % y in any case 5 where x / y does not throw an exception. 6

• Floating-point remainder: 7

float operator %(float x, float y); 8 double operator %(double x, double y); 9

The following table lists the results of all possible combinations of nonzero finite values, zeros, 10 infinities, and NaN’s. In the table, x and y are positive finite values. z is the result of x % y and is 11 computed as x – n * y, rounded to the nearest representable value, where n is the largest integer that is 12 less than or equal to x / y. This method of computing the remainder is analogous to that used for 13 integer operands, but differs from the IEC 60559 definition (in which n is the integer closest to x / y). 14

15 +y –y +0 –0 +∞ –∞ NaN

+x +z +z NaN NaN +x +x NaN

–x –z –z NaN NaN –x –x NaN

+0 +0 +0 NaN NaN +0 +0 NaN

–0 –0 –0 NaN NaN –0 –0 NaN

+∞ NaN NaN NaN NaN NaN NaN NaN

–∞ NaN NaN NaN NaN NaN NaN NaN

NaN NaN NaN NaN NaN NaN NaN NaN

16

• Decimal remainder: 17

decimal operator %(decimal x, decimal y); 18

If the value of the right operand is zero, a System.DivideByZeroException is thrown. It is 19 implementation-defined when a System.ArithmeticException (or a subclass thereof) is thrown. A 20 conforming implementation shall not throw an exception for x % y in any case where x / y does not 21 throw an exception. The scale of the result, before any rounding, is the larger of the scales of the two 22 operands, and the sign of the result, if non-zero, is the same as that of x. 23

Decimal remainder is equivalent to using the remainder operator of type System.Decimal. 24

14.7.4 Addition operator 25

For an operation of the form x + y, binary operator overload resolution (§14.2.4) is applied to select a 26 specific operator implementation. The operands are converted to the parameter types of the selected 27 operator, and the type of the result is the return type of the operator. 28

The predefined addition operators are listed below. For numeric and enumeration types, the predefined 29 addition operators compute the sum of the two operands. When one or both operands are of type string, 30 the predefined addition operators concatenate the string representation of the operands. 31

• Integer addition: 32

int operator +(int x, int y); 33 uint operator +(uint x, uint y); 34 long operator +(long x, long y); 35 ulong operator +(ulong x, ulong y); 36 void operator +(long x, ulong y); 37 void operator +(ulong x, long y); 38

Page 214: C# Language Specification

C# LANGUAGE SPECIFICATION

194

The operators with void return type always produce a compile-time error. Consequently, it is an error 1 for one operand to be of type long and the other to be of type ulong. 2

In a checked context, if the sum is outside the range of the result type, a 3 System.OverflowException is thrown. In an unchecked context, overflows are not reported and 4 any significant high-order bits outside the range of the result type are discarded. 5

• Floating-point addition: 6

float operator +(float x, float y); 7 double operator +(double x, double y); 8

The sum is computed according to the rules of IEC 60559 arithmetic. The following table lists the 9 results of all possible combinations of nonzero finite values, zeros, infinities, and NaN’s. In the table, x 10 and y are nonzero finite values, and z is the result of x + y, rounded to the nearest representable value. 11 If x and y have the same magnitude but opposite signs, z is positive zero. If the magnitude of x + y is 12 too large to represent in the destination type, z is an infinity with the same sign as x + y. 13

14 y +0 –0 +∞ –∞ NaN

x z x x +∞ –∞ NaN

+0 y +0 +0 +∞ –∞ NaN

–0 y +0 –0 +∞ –∞ NaN

+∞ +∞ +∞ +∞ +∞ NaN NaN

–∞ –∞ –∞ –∞ NaN –∞ NaN

NaN NaN NaN NaN NaN NaN NaN

15

• Decimal addition: 16

decimal operator +(decimal x, decimal y); 17

If the magnitude of the resulting value is too large to represent in the decimal format, a 18 System.OverflowException is thrown. The scale of the result, before any rounding, is the larger of 19 the scales of the two operands. 20

Decimal addition is equivalent to using the addition operator of type System.Decimal. 21

• Enumeration addition. Every enumeration type implicitly provides the following predefined operators, 22 where E is the enum type, and U is the underlying type of E: 23

E operator +(E x, U y); 24 E operator +(U x, E y); 25

The operators are evaluated exactly as (E)((U)x + (U)y). These operators are only considered by 26 overload resolution (§14.2.4) when one of the actual operands is of type E. 27

• String concatenation: 28

string operator +(string x, string y); 29 string operator +(string x, object y); 30 string operator +(object x, string y); 31

The binary + operator performs string concatenation when one or both operands are of type string. If 32 an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string 33 operand is converted to its string representation by invoking the virtual ToString method inherited 34 from type object. If ToString returns null, an empty string is substituted. [Example: 35

using System; 36

Page 215: C# Language Specification

Chapter 14 Expressions

195

class Test 1 { 2 static void Main() { 3 string s = null; 4 Console.WriteLine("s = >" + s + "<"); // displays s = >< 5 int i = 1; 6 Console.WriteLine("i = " + i); // displays i = 1 7 float f = 1.2300E+15F; 8 Console.WriteLine("f = " + f); // displays f = 1.23E+15 9 decimal d = 2.900m; 10 Console.WriteLine("d = " + d); // displays d = 2.900 11 } 12 } 13

end example] 14

The result of the string concatenation operator is a string that consists of the characters of the left 15 operand followed by the characters of the right operand. The string concatenation operator never returns 16 a null value. A System.OutOfMemoryException can be thrown if there is not enough memory 17 available to allocate the resulting string. 18

• Delegate combination. Every delegate type implicitly provides the following predefined operator, where 19 D is the delegate type: 20

D operator +(D x, D y); 21

The binary + operator performs delegate combination when both operands are convertible to some 22 delegate type D. (If the operands have different delegate types, overload resolution finds no applicable 23 operator and a compile-time error occurs.) This operator is only considered by overload resolution 24 (§14.2.4) when one of the actual operands is of type D. If the first operand is null, the result of the 25 operation is the value of the second operand (even if that is also null). Otherwise, if the second operand 26 is null, then the result of the operation is the value of the first operand. Otherwise, the result of the 27 operation is a new delegate instance whose invocation list consists of the elements in the invocation list 28 of the first operand, followed by the elements in the invocation list of the second operand. That is, the 29 invocation list of the resulting delegate is the concatenation of the invocation lists of the two operands. 30 [Note: For examples of delegate combination, see §14.7.5 and §22.3. Since System.Delegate is not a 31 delegate type, operator + is not defined for it. end note] 32

14.7.5 Subtraction operator 33

For an operation of the form x – y, binary operator overload resolution (§14.2.4) is applied to select a 34 specific operator implementation. The operands are converted to the parameter types of the selected 35 operator, and the type of the result is the return type of the operator. 36

The predefined subtraction operators are listed below. The operators all subtract y from x. 37

• Integer subtraction: 38

int operator –(int x, int y); 39 uint operator –(uint x, uint y); 40 long operator –(long x, long y); 41 ulong operator –(ulong x, ulong y); 42 void operator -(long x, ulong y); 43 void operator -(ulong x, long y); 44

The operators with void return type always produce a compile-time error. Consequently, it is an error 45 for one operand to be of type long and the other to be of type ulong. 46

In a checked context, if the difference is outside the range of the result type, a 47 System.OverflowException is thrown. In an unchecked context, overflows are not reported and 48 any significant high-order bits outside the range of the result type are discarded. 49

• Floating-point subtraction: 50

float operator –(float x, float y); 51 double operator –(double x, double y); 52

Page 216: C# Language Specification

C# LANGUAGE SPECIFICATION

196

The difference is computed according to the rules of IEC 60559 arithmetic. The following table lists the 1 results of all possible combinations of nonzero finite values, zeros, infinities, and NaNs. In the table, x 2 and y are nonzero finite values, and z is the result of x – y, rounded to the nearest representable value. 3 If x and y are equal, z is positive zero. If the magnitude of x – y is too large to represent in the 4 destination type, z is an infinity with the same sign as x – y. 5

6 y +0 –0 +∞ –∞ NaN

x +z +x +x –∞ +∞ NaN

+0 –y +0 +0 –∞ +∞ NaN

–0 –y –0 +0 –∞ +∞ NaN

+∞ +∞ +∞ +∞ NaN +∞ NaN

–∞ –∞ –∞ –∞ –∞ NaN NaN

NaN NaN NaN NaN NaN NaN NaN

7

• Decimal subtraction: 8

decimal operator –(decimal x, decimal y); 9

If the magnitude of the resulting value is too large to represent in the decimal format, a 10 System.OverflowException is thrown. The scale of the result, before any rounding, is the larger of 11 the scales of the two operands. 12

Decimal subtraction is equivalent to using the subtraction operator of type System.Decimal. 13

• Enumeration subtraction. Every enumeration type implicitly provides the following predefined operator, 14 where E is the enum type, and U is the underlying type of E: 15

U operator –(E x, E y); 16

This operator is evaluated exactly as (U)((U)x – (U)y). In other words, the operator computes the 17 difference between the ordinal values of x and y, and the type of the result is the underlying type of the 18 enumeration. 19

E operator –(E x, U y); 20

This operator is evaluated exactly as (E)((U)x – y). In other words, the operator subtracts a value 21 from the underlying type of the enumeration, yielding a value of the enumeration. 22

These operators are only considered by overload resolution (§14.2.4) when one of the actual operands is 23 of type E. 24

• Delegate removal. Every delegate type implicitly provides the following predefined operator, where D is 25 the delegate type: 26

D operator –(D x, D y); 27

The binary - operator performs delegate removal when both operands are convertible to some delegate 28 type D. (If the operands have different delegate types, overload resolution finds no applicable operator 29 and a compile-time error occurs.) This operator is only considered by overload resolution (§14.2.4) 30 when one of the actual operands is of type D. If the first operand is null, the result of the operation is 31 null. Otherwise, if the second operand is null, then the result of the operation is the value of the first 32 operand. Otherwise, both operands represent invocation lists (§22.1) having one or more entries, and the 33 result is a new invocation list consisting of the first operand’s list with the second operand’s entries 34 removed from it, provided the second operand’s list is a proper contiguous sublist of the first’s. (For 35 determining sublist equality, corresponding entries are compared as for the delegate equality operator 36 (§14.9.8).) Otherwise, the result is the value of the left operand. Neither of the operands’ lists is changed 37 in the process. If the second operand’s list matches multiple sublists of contiguous entries in the first 38 operand’s list, the right-most matching sublist of contiguous entries is removed. If removal results in an 39 empty list, the result is null. [Example: 40

Page 217: C# Language Specification

Chapter 14 Expressions

197

using System; 1

delegate void D(int x); 2 class Test 3 { 4 public static void M1(int i) { /* … */ } 5 public static void M2(int i) { /* … */ } 6 } 7

class Demo 8 { 9 static void Main() { 10 D cd1 = new D(Test.M1); 11 D cd2 = new D(Test.M2); 12

D cd3 = cd1 + cd2 + cd2 + cd1; // M1 + M2 + M2 + M1 13 cd3 -= cd1; // => M1 + M2 + M2 14

cd3 = cd1 + cd2 + cd2 + cd1; // M1 + M2 + M2 + M1 15 cd3 -= cd1 + cd2; // => M2 + M1 16

cd3 = cd1 + cd2 + cd2 + cd1; // M1 + M2 + M2 + M1 17 cd3 -= cd2 + cd2; // => M1 + M1 18

cd3 = cd1 + cd2 + cd2 + cd1; // M1 + M2 + M2 + M1 19 cd3 -= cd2 + cd1; // => M1 + M2 20

cd3 = cd1 + cd2 + cd2 + cd1; // M1 + M2 + M2 + M1 21 cd3 -= cd1 + cd1; // => M1 + M2 + M2 + M1 22 } 23 } 24

end example] 25

14.8 Shift operators 26

The << and >> operators are used to perform bit shifting operations. 27

shift-expression: 28 additive-expression 29 shift-expression << additive-expression 30 shift-expression right-shift additive-expression 31

For an operation of the form x << count or x >> count, binary operator overload resolution (§14.2.4) is 32 applied to select a specific operator implementation. The operands are converted to the parameter types of 33 the selected operator, and the type of the result is the return type of the operator. 34

When declaring an overloaded shift operator, the type of the first operand shall always be the class or struct 35 containing the operator declaration, and the type of the second operand shall always be int. 36

The predefined shift operators are listed below. 37

• Shift left: 38

int operator <<(int x, int count); 39 uint operator <<(uint x, int count); 40 long operator <<(long x, int count); 41 ulong operator <<(ulong x, int count); 42

The << operator shifts x left by a number of bits computed as described below. 43

The high-order bits outside the range of the result type of x are discarded, the remaining bits are shifted 44 left, and the low-order empty bit positions are set to zero. 45

• Shift right: 46

int operator >>(int x, int count); 47 uint operator >>(uint x, int count); 48 long operator >>(long x, int count); 49 ulong operator >>(ulong x, int count); 50

The >> operator shifts x right by a number of bits computed as described below. 51

Page 218: C# Language Specification

C# LANGUAGE SPECIFICATION

198

When x is of type int or long, the low-order bits of x are discarded, the remaining bits are shifted 1 right, and the high-order empty bit positions are set to zero if x is non-negative and set to one if x is 2 negative. 3

When x is of type uint or ulong, the low-order bits of x are discarded, the remaining bits are shifted 4 right, and the high-order empty bit positions are set to zero. 5

For the predefined operators, the number of bits to shift is computed as follows: 6

• When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other 7 words, the shift count is computed from count & 0x1F. 8

• When the type of x is long or ulong, the shift count is given by the low-order six bits of count. In 9 other words, the shift count is computed from count & 0x3F. 10

If the resulting shift count is zero, the shift operators simply return the value of x. 11

Shift operations never cause overflows and produce the same results in checked and unchecked contexts. 12

When the left operand of the >> operator is of a signed integral type, the operator performs an arithmetic 13 shift right wherein the value of the most significant bit (the sign bit) of the operand is propagated to the 14 high-order empty bit positions. When the left operand of the >> operator is of an unsigned integral type, the 15 operator performs a logical shift right wherein high-order empty bit positions are always set to zero. To 16 perform the opposite operation of that inferred from the operand type, explicit casts can be used. [Example: 17 If x is a variable of type int, the operation unchecked((int)((uint)x >> y)) performs a logical shift 18 right of x. end example] 19

14.9 Relational and type-testing operators 20

The ==, !=, <, >, <=, >=, is and as operators are called the relational and type-testing operators. 21

relational-expression: 22 shift-expression 23 relational-expression < shift-expression 24 relational-expression > shift-expression 25 relational-expression <= shift-expression 26 relational-expression >= shift-expression 27 relational-expression is type 28 relational-expression as type 29

equality-expression: 30 relational-expression 31 equality-expression == relational-expression 32 equality-expression != relational-expression 33

The is operator is described in §14.9.9 and the as operator is described in §14.9.10. 34

The ==, !=, <, >, <= and >= operators are comparison operators. For an operation of the form x op y, where 35 op is a comparison operator, overload resolution (§14.2.4) is applied to select a specific operator 36 implementation. The operands are converted to the parameter types of the selected operator, and the type of 37 the result is the return type of the operator. If both operands of an equality-expression have the null type 38 (and hence the null value as well), then overload resolution is not performed and the expression evaluates 39 to a contant value of true or false according to whether the operator is == or !=. 40

The predefined comparison operators are described in the following subclauses. All predefined comparison 41 operators return a result of type bool, as described in the following table. 42

43

Page 219: C# Language Specification

Chapter 14 Expressions

199

Operation Result x == y true if x is equal to y, false otherwise x != y true if x is not equal to y, false otherwise x < y true if x is less than y, false otherwise x > y true if x is greater than y, false otherwise x <= y true if x is less than or equal to y, false otherwise x >= y true if x is greater than or equal to y, false otherwise

1

14.9.1 Integer comparison operators 2

The predefined integer comparison operators are: 3

bool operator ==(int x, int y); 4 bool operator ==(uint x, uint y); 5 bool operator ==(long x, long y); 6 bool operator ==(ulong x, ulong y); 7 void operator ==(long x, ulong y); 8 void operator ==(ulong x, long y); 9

bool operator !=(int x, int y); 10 bool operator !=(uint x, uint y); 11 bool operator !=(long x, long y); 12 bool operator !=(ulong x, ulong y); 13 void operator !=(long x, ulong y); 14 void operator !=(ulong x, long y); 15

bool operator <(int x, int y); 16 bool operator <(uint x, uint y); 17 bool operator <(long x, long y); 18 bool operator <(ulong x, ulong y); 19 void operator <(long x, ulong y); 20 void operator >(ulong x, long y); 21

bool operator >(int x, int y); 22 bool operator >(uint x, uint y); 23 bool operator >(long x, long y); 24 bool operator >(ulong x, ulong y); 25 void operator >(long x, ulong y); 26 void operator >(ulong x, long y); 27

bool operator <=(int x, int y); 28 bool operator <=(uint x, uint y); 29 bool operator <=(long x, long y); 30 bool operator <=(ulong x, ulong y); 31 void operator <=(long x, ulong y); 32 void operator <=(ulong x, long y); 33

bool operator >=(int x, int y); 34 bool operator >=(uint x, uint y); 35 bool operator >=(long x, long y); 36 bool operator >=(ulong x, ulong y); 37 void operator >=(long x, ulong y); 38 void operator >=(ulong x, long y); 39

Each of these operators compares the numeric values of the two integer operands and returns a bool 40 value that indicates whether the particular relation is true or false. The operators with void return 41 type always produce a compile-time error. Consequently, it is an error for one operand to be of type 42 long and the other to be of type ulong. 43

14.9.2 Floating-point comparison operators 44

The predefined floating-point comparison operators are: 45

Page 220: C# Language Specification

C# LANGUAGE SPECIFICATION

200

bool operator ==(float x, float y); 1 bool operator ==(double x, double y); 2

bool operator !=(float x, float y); 3 bool operator !=(double x, double y); 4

bool operator <(float x, float y); 5 bool operator <(double x, double y); 6

bool operator >(float x, float y); 7 bool operator >(double x, double y); 8

bool operator <=(float x, float y); 9 bool operator <=(double x, double y); 10

bool operator >=(float x, float y); 11 bool operator >=(double x, double y); 12

The operators compare the operands according to the rules of the IEC 60559 standard: 13

• If either operand is NaN, the result is false for all operators except !=, for which the result is true. 14 For any two operands, x != y always produces the same result as !(x == y). However, when one or 15 both operands are NaN, the <, >, <=, and >= operators do not produce the same results as the logical 16 negation of the opposite operator. [Example: If either of x and y is NaN, then x < y is false, but 17 !(x >= y) is true. end example] 18

• When neither operand is NaN, the operators compare the values of the two floating-point operands with 19 respect to the ordering 20

–∞ < –max < … < –min < –0.0 == +0.0 < +min < … < +max < +∞ 21

where min and max are the smallest and largest positive finite values that can be represented in the given 22 floating-point format. Notable effects of this ordering are: 23

o Negative and positive zeros are considered equal. 24

o A negative infinity is considered less than all other values, but equal to another negative infinity. 25

o A positive infinity is considered greater than all other values, but equal to another positive infinity. 26

14.9.3 Decimal comparison operators 27

The predefined decimal comparison operators are: 28

bool operator ==(decimal x, decimal y); 29 bool operator !=(decimal x, decimal y); 30

bool operator <(decimal x, decimal y); 31 bool operator >(decimal x, decimal y); 32 bool operator <=(decimal x, decimal y); 33 bool operator >=(decimal x, decimal y); 34

Each of these operators compares the numeric values of the two decimal operands and returns a bool value 35 that indicates whether the particular relation is true or false. Each decimal comparison is equivalent to 36 using the corresponding relational or equality operator of type System.Decimal. 37

14.9.4 Boolean equality operators 38

The predefined boolean equality operators are: 39

bool operator ==(bool x, bool y); 40 bool operator !=(bool x, bool y); 41

The result of == is true if both x and y are true or if both x and y are false. Otherwise, the result is 42 false. 43

The result of != is false if both x and y are true or if both x and y are false. Otherwise, the result is 44 true. When the operands are of type bool, the != operator produces the same result as the ^ operator. 45

Page 221: C# Language Specification

Chapter 14 Expressions

201

14.9.5 Enumeration comparison operators 1

Every enumeration type implicitly provides the following predefined comparison operators: 2

bool operator ==(E x, E y); 3 bool operator !=(E x, E y); 4

bool operator <(E x, E y); 5 bool operator >(E x, E y); 6 bool operator <=(E x, E y); 7 bool operator >=(E x, E y); 8

The result of evaluating x op y, where x and y are expressions of an enumeration type E with an underlying 9 type U, and op is one of the comparison operators, is exactly the same as evaluating ((U)x) op ((U)y). In 10 other words, the enumeration type comparison operators simply compare the underlying integral values of 11 the two operands. These operators are only considered by overload resolution (§14.2.4) when one of the 12 actual operands is of type E. 13

14.9.6 Reference type equality operators 14

Every class type C implicitly provides the following predefined reference type equality operators: 15

bool operator ==(C x, C y); 16 bool operator !=(C x, C y); 17

unless predefined equality operators otherwise exists for C (for example, when C is string or 18 System.Delegate). 19

The operators return the result of comparing the two references for equality or non-equality. 20

There are special rules for determining when a reference type equality operator is applicable. For an 21 equality-expression with operands of type A and B, define A0 as follows: 22

• If A is a type parameter that does not have the value type constraint (§26.7), let A0 be the effective base 23 class of A. 24

• Otherwise, if A is an interface type, a delegate type, an array type, a class type, or the null type, let A0 25 be the same as A. 26

• Otherwise, no reference type equality operator is applicable. 27

Now define A1 as follows: 28

• If A0 is an interface type, a delegate type, System.Delegate, or string, let A1 be object. 29

• Otherwise, if A0 is an array type, let A1 be System.Array. 30

• Otherwise, A0 is the null type or a class type and let A1 be the same as A0. 31

Define B0 and B1 similarly. Now determine if any reference type equality operators are applicable as follows: 32

• If both of the types A and B are the null type, then overload resolution is not performed and the result is 33 a constant true or false, as specified in §14.9. 34

• Otherwise, if A is the null type, then the reference type equality operator for B1 is applicable. If B is a 35 type parameter and at run-time the type parameter is a value type, the result of the comparison is false. 36

• Otherwise, if B is the null type, then the reference type equality operator for A1 is applicable. If A is a 37 type parameter and at run-time the type parameter is a value type, the result of the comparison is false. 38

• Otherwise, if either A or B is a type parameter that is not known to be a reference type (§26.7), then no 39 reference type equality operator is applicable. 40

• Otherwise, if there is no identity or reference conversion (implicit or explicit) from A0 to B0 or no 41 identity or reference conversion (implicit or explicit) from B0 to A0, then no reference type equality 42 operator is applicable. 43

Page 222: C# Language Specification

C# LANGUAGE SPECIFICATION

202

• Otherwise, if there is an identity or implicit reference conversion from A1 to B1, then the reference type 1 equality operator for B1 is applicable. 2

• Otherwise, if there is an implicit reference conversion from B1 to A1, then the reference type equality 3 operator for A1 is applicable. 4

• Otherwise no reference type equality operator is applicable. 5

[Note: Notable implications of these rules are: 6

• The predefined reference type equality operators cannot be used to compare two references that are 7 known to be different at compile-time. [Example: If the compile-time types of the operands are two class 8 types A and B, and if neither A nor B derives from the other, then it would be impossible for the two 9 operands to reference the same object and no reference type equality operator is applicable. Similarly, if 10 A is a sealed class and B is an interface that A does not implement, then no reference type equality 11 operator is applicable. end example] 12

• The predefined reference type equality operators do not permit value type operands to be compared. 13

• The predefined reference type equality operators never cause boxing operations to occur for their 14 operands. It would be meaningless to perform such boxing operations, since references to the newly 15 allocated boxed instances would necessarily differ from all other references. 16

end note] 17

When the operator overload resolution (§14.2.4) rules would pick an equality operator other than a reference 18 type equality operator, selection of a reference type equality operator can be forced by explicitly casting one 19 or both operands to type object. [Example: The example 20

using System; 21

class Test 22 { 23 static void Main() { 24 string s = "Test"; 25 string t = string.Copy(s); 26 Console.WriteLine(s == t); 27 Console.WriteLine((object)s == t); 28 Console.WriteLine(s == (object)t); 29 Console.WriteLine((object)s == (object)t); 30 } 31 } 32

produces the output 33

True 34 False 35 False 36 False 37

The s and t variables refer to two distinct string instances containing the same characters. The first 38 comparison outputs True because the predefined string equality operator (§14.9.7) is selected when both 39 operands are of type string. The remaining comparisons all output False because a predefined reference 40 type equality operator is selected when one or both of the operands are of type object. 41

Note that the above technique is not meaningful for value types. The example 42

class Test 43 { 44 static void Main() { 45 int i = 123; 46 int j = 123; 47 System.Console.WriteLine((object)i == (object)j); 48 } 49 } 50

outputs False because the casts create references to two separate instances of boxed int values. end 51 example] 52

Page 223: C# Language Specification

Chapter 14 Expressions

203

14.9.7 String equality operators 1

The predefined string equality operators are: 2

bool operator ==(string x, string y); 3 bool operator !=(string x, string y); 4

Two string values are considered equal when one of the following is true: 5

• Both values are null. 6

• Both values are non-null references to string instances that have identical lengths and identical 7 characters in each character position. 8

The string equality operators compare string values rather than string references. When two separate string 9 instances contain the exact same sequence of characters, the values of the strings are equal, but the 10 references are different. [Note: As described in §14.9.6, the reference type equality operators can be used to 11 compare string references instead of string values. end note] 12

14.9.8 Delegate equality operators 13

Every delegate type implicitly provides the following predefined comparison operators, where D is the 14 delegate type : 15

bool operator ==(D x, D y); 16 bool operator !=(D x, D y); 17

These operators are only considered by overload resolution (§14.2.4) when one of the actual operands is of 18 type D. 19

The following predefined operators are available for comparing delegates when the delegate type is not 20 known at compile time: 21

bool operator ==(System.Delegate x, System.Delegate y); 22 bool operator !=(System.Delegate x, System.Delegate y); 23

These latter operators are not considered applicable if the types of the operands are distinct delegate types. 24 [Example: 25

delegate void D(); 26 delegate void E(); 27

bool Compare1(D d, E e) { 28 return d == e; // Error 29 } 30

bool Compare2(System.Delegate d, E e) { 31 return d == e; // Ok 32 } 33

The comparison in Compare1 produces an error since the types of the operands are distinct delegate types. 34 In this case, none of the delegate equality operators or reference equality operators are applicable since a 35 non-null instance of D could never be equal to an instance of E. end example] 36

Two delegate instances are considered equal as follows: 37

• If either of the delegate instances is null, they are equal if and only if both are null. 38

• If the delegate instances have different actual types, they are not equal. 39

• If either of the delegate instances has an invocation list (§22.1) containing one entry, they are equal if 40 and only if the other also has an invocation list containing one entry and the entries are equal (as defined 41 below). 42

• If both of the delegate instances has an invocation list containing two or more entries, those instances are 43 equal if and only if their invocation lists are the same length, and each entry in one’s invocation list is 44 equal (as defined below) to the corresponding entry, in order, in the other’s invocation list. 45

The following rules govern the equality of invocation list entries: 46

Page 224: C# Language Specification

C# LANGUAGE SPECIFICATION

204

• If two invocation list entries both refer to the same static method then the entries are equal. 1

• If two invocation list entries both refer to the same non-static method on the same target object (as 2 defined by the reference equality operators) then the entries are equal. 3

• Invocation list entries produced from evaluation of semantically identical anonymous-method-4 expressions with the same (possibly empty) set of captured outer variable instances are permitted (but 5 not required) to be equal. 6

• Invocation list entries produced from evaluation of semantically different anonymous-method-7 expressions or having different sets of captured outer variable instances are never equal. 8

• An invocation list entry produced from evaluation of an anonymous-method-expression is never equal to 9 an invocation list entry produced from a delegate-creation-expression on a method group. 10

14.9.9 The is operator 11

The is operator is used to dynamically check if the run-time type of an object is compatible with a given 12 type. The result of the operation e is T, where e is an expression and T is a type, is a boolean value 13 indicating whether e can successfully be converted to type T by a reference conversion, a boxing conversion, 14 or an unboxing conversion. The operation is evaluated as follows: 15

• If the compile-time type of e is the same as T, or if an implicit reference conversion (§13.1.4) or boxing 16 conversion (§13.1.5) exists from the compile-time type of e to T: 17

o If e is of a reference type, the result of the operation is equivalent to evaluating e != null. 18

o If e is of a value type, the result of the operation is true. 19

• Otherwise, if an explicit reference conversion (§13.2.3) or unboxing conversion (§13.2.4) exists from 20 the compile-time type of e to T, a dynamic type check is performed: 21

o If the value of e is null, the result is false. 22

o Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if 23 R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type 24 and T is an interface type that is implemented by R, the result is true. 25

o Otherwise, the result is false. 26

• Otherwise, if the compile-time type of e or T is an open type (§26.5.2), a dynamic type check is 27 performed. 28

• Otherwise, no reference or boxing conversion of e to type T is possible, and the result of the operation is 29 false. 30

The is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other 31 conversions, such as user defined conversions, are not considered by the is operator. 32

14.9.10 The as operator 33

The as operator is used to explicitly convert a value to a given reference type using a reference conversion 34 or a boxing conversion. Unlike a cast expression (§14.6.6), the as operator never throws an exception. 35 Instead, if the indicated conversion is not possible, the resulting value is null. 36

In an operation of the form e as T, e shall be an expression and T shall be a reference type or a type 37 parameter that is known to be a reference type (§26.7). The type of the result is T, and the result is always 38 classified as a value. The operation is evaluated as follows: 39

• If the compile-time type of e is the same as T, the result is simply the value of e. 40

• Otherwise, if an implicit reference conversion (§13.1.4) or boxing conversion (§13.1.5) exists from the 41 compile-time type of e to T, this conversion is performed and becomes the result of the operation. 42

Page 225: C# Language Specification

Chapter 14 Expressions

205

• Otherwise, if an explicit reference conversion (§13.2.3) exists from the compile-time type of e to T, a 1 dynamic type check is performed: 2

o If the value of e is null, the result is the value null with the compile-time type T. 3

o Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if 4 R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type 5 and T is an interface type that is implemented by R, the result is the reference given by e with the 6 compile-time type T. 7

o Otherwise, the result is the value null with the compile-time type T. 8

• Otherwise, if the compile-time type of e or T is an open type (§26.5.2), a dynamic type check is 9 performed. 10

• Otherwise, the indicated conversion is never possible, and a compile-time error occurs. 11

The as operator only performs reference conversions and boxing conversions. Other conversions, such as 12 user defined conversions, are not possible with the as operator and should instead be performed using cast 13 expressions. 14

The as operator can be used with a type parameter T (§26.1.1) as the right-hand side only if T is known to 15 be a reference type (§26.7). This restriction is required because the value null might be returned as a result 16 of the operator. [Example: 17

class X 18 { 19 public T F<T>(object o) where T: Attribute { 20 return o as T; // Ok, T has a class constraint 21 } 22

public T G<T>(object o) { 23 return o as T; // Error, unconstrained T 24 } 25 } 26

end example] 27

[Note: When constraints are factored in, there are situations where the compiler can conclude that the 28 operation e as T will never succeed, for example when e is a struct or sealed class that doesn’t implement 29 a particular interface, and T is constrained to require that interface. However, a complete specification of 30 those situations is complicated, and the rules given above seem to be the best compromise. end note] 31

14.10 Logical operators 32

The &, ^, and | operators are called the logical operators. 33

and-expression: 34 equality-expression 35 and-expression & equality-expression 36

exclusive-or-expression: 37 and-expression 38 exclusive-or-expression ^ and-expression 39

inclusive-or-expression: 40 exclusive-or-expression 41 inclusive-or-expression | exclusive-or-expression 42

For an operation of the form x op y, where op is one of the logical operators, overload resolution (§14.2.4) is 43 applied to select a specific operator implementation. The operands are converted to the parameter types of 44 the selected operator, and the type of the result is the return type of the operator. 45

The predefined logical operators are described in the following subclauses. 46

Page 226: C# Language Specification

C# LANGUAGE SPECIFICATION

206

14.10.1 Integer logical operators 1

The predefined integer logical operators are: 2

int operator &(int x, int y); 3 uint operator &(uint x, uint y); 4 long operator &(long x, long y); 5 ulong operator &(ulong x, ulong y); 6 void operator &(long x, ulong y); 7 void operator &(ulong x, long y); 8

int operator |(int x, int y); 9 uint operator |(uint x, uint y); 10 long operator |(long x, long y); 11 ulong operator |(ulong x, ulong y); 12 void operator |(long x, ulong y); 13 void operator |(ulong x, long y); 14

int operator ^(int x, int y); 15 uint operator ^(uint x, uint y); 16 long operator ^(long x, long y); 17 ulong operator ^(ulong x, ulong y); 18 void operator ^(long x, ulong y); 19 void operator ^(ulong x, long y); 20

The & operator computes the bitwise logical AND of the two operands, the | operator computes the bitwise 21 logical OR of the two operands, and the ^ operator computes the bitwise logical exclusive OR of the two 22 operands. No overflows are possible from these operations. The operators with void return type always 23 produce a compile-time error. Consequently, it is an error for one operand to be of type long and the other 24 to be of type ulong. 25

14.10.2 Enumeration logical operators 26

Every enumeration type E implicitly provides the following predefined logical operators: 27

E operator &(E x, E y); 28 E operator |(E x, E y); 29 E operator ^(E x, E y); 30

The result of evaluating x op y, where x and y are expressions of an enumeration type E with an underlying 31 type U, and op is one of the logical operators, is exactly the same as evaluating (E)((U)x op (U)y). In other 32 words, the enumeration type logical operators simply perform the logical operation on the underlying type of 33 the two operands. These operators are only considered by overload resolution (§14.2.4) when one of the 34 actual operands is of type E. 35

14.10.3 Boolean logical operators 36

The predefined boolean logical operators are: 37

bool operator &(bool x, bool y); 38 bool operator |(bool x, bool y); 39 bool operator ^(bool x, bool y); 40

The result of x & y is true if both x and y are true. Otherwise, the result is false. 41

The result of x | y is true if either x or y is true. Otherwise, the result is false. 42

The result of x ^ y is true if x is true and y is false, or x is false and y is true. Otherwise, the result 43 is false. When the operands are of type bool, the ^ operator computes the same result as the != operator. 44

14.11 Conditional logical operators 45

The && and || operators are called the conditional logical operators. They are also called the “short-46 circuiting” logical operators. 47

conditional-and-expression: 48 inclusive-or-expression 49 conditional-and-expression && inclusive-or-expression 50

Page 227: C# Language Specification

Chapter 14 Expressions

207

conditional-or-expression: 1 conditional-and-expression 2 conditional-or-expression || conditional-and-expression 3

The && and || operators are conditional versions of the & and | operators: 4

• The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true. 5

• The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is 6 false. 7

An operation of the form x && y or x || y is processed by applying overload resolution (§14.2.4) as if the 8 operation was written x & y or x | y. Then, 9

• If overload resolution fails to find a single best operator, or if overload resolution selects one of the 10 predefined integer logical operators, a compile-time error occurs. 11

• Otherwise, if the selected operator is one of the predefined boolean logical operators (§14.10.2), the 12 operation is processed as described in §14.11.1. 13

• Otherwise, the selected operator is a user-defined operator, and the operation is processed as described 14 in §14.11.2. 15

It is not possible to directly overload the conditional logical operators. However, because the conditional 16 logical operators are evaluated in terms of the regular logical operators, overloads of the regular logical 17 operators are, with certain restrictions, also considered overloads of the conditional logical operators. This is 18 described further in §14.11.2. 19

14.11.1 Boolean conditional logical operators 20

When overload resolution (§14.2.4) for x & y or x | y selects the predefined boolean & or | operator, then 21 x && y or x || y is processed as follows: 22

• The operation x && y is evaluated as (bool)x ? (bool)y : false. In other words, x is first 23 evaluated and converted to type bool. Then, if x is true, y is evaluated and converted to type bool, 24 and this becomes the result of the operation. Otherwise, the result of the operation is false. 25

• The operation x || y is evaluated as (bool)x ? true : (bool)y. In other words, x is first 26 evaluated and converted to type bool. Then, if x is true, the result of the operation is true. Otherwise, 27 y is evaluated and converted to type bool, and this becomes the result of the operation. 28

14.11.2 User-defined conditional logical operators 29

When overload resolution (§14.2.4) for x & y or x | y selects a user defined & or | operator, then 30 processing x && y or x || y requires that both of the following shall be true, where T is the type in which 31 the selected operator is declared: 32

• The return type and the type of each parameter of the selected operator shall be T. In other words, the 33 operator shall compute the logical AND or the logical OR of two operands of type T, and shall return a 34 result of type T. 35

• T shall contain declarations of operator true and operator false. 36

A compile-time error occurs if either of these requirements is not satisfied. Otherwise, the && or || 37 operation is evaluated by combining the user-defined operator true or operator false with the 38 selected user-defined operator: 39

• The operation x && y is evaluated as T.false((T)x) ? (T)x : T.&((T)x, y), where 40 T.false((T)x) is an invocation of the operator false declared in T, and T.&((T)x, y) is an 41 invocation of the selected operator &. In addition, the value (T)x shall only be evaluated once. In 42 other words, x is first evaluated and converted to type T and operator false is invoked on the result 43 to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value 44 previously computed for x converted to type T. Otherwise, y is evaluated, and the selected operator & 45

Page 228: C# Language Specification

C# LANGUAGE SPECIFICATION

208

is invoked on the value previously computed for x converted to type T and the value computed for y to 1 produce the result of the operation. 2

• The operation x || y is evaluated as T.true((T)x) ? (T)x : T.|((T)x, y), where 3 T.true((T)x) is an invocation of the operator true declared in T, and T.|((T)x, y) is an 4 invocation of the selected operator |. In addition, the value (T)x shall only be evaluated once. In 5 other words, x is first evaluated and converted to type T and operator true is invoked on the result to 6 determine if x is definitely true. Then, if x is definitely true, the result of the operation is the value 7 previously computed for x converted to type T. Otherwise, y is evaluated, and the selected operator | 8 is invoked on the value previously computed for x converted to type T and the value computed for y to 9 produce the result of the operation. 10

In either of these operations, the expression given by x is only evaluated once, and the expression given by y 11 is either not evaluated or evaluated exactly once. 12

For an example of a type that implements operator true and operator false, see §18.4.2. 13

14.12 Conditional operator 14

The ?: operator is called the conditional operator. It is at times also called the ternary operator. 15

conditional-expression: 16 conditional-or-expression 17 conditional-or-expression ? expression : expression 18

A conditional expression of the form b ? x : y first evaluates the condition b. Then, if b is true, x is 19 evaluated and becomes the result of the operation. Otherwise, y is evaluated and becomes the result of the 20 operation. A conditional expression never evaluates both x and y. 21

The conditional operator is right-associative, meaning that operations are grouped from right to left. 22 [Example: An expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e). end 23 example] 24

The first operand of the ?: operator shall be an expression of a type that can be implicitly converted to 25 bool, or an expression of a type that implements operator true. If neither of these requirements is 26 satisfied, a compile-time error occurs. 27

The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y 28 be the types of the second and third operands. Then, 29

• If X and Y are the same type, then this is the type of the conditional expression. 30

• Otherwise, if an implicit conversion (§13.1) exists from X to Y, but not from Y to X, then Y is the type of 31 the conditional expression. 32

• Otherwise, if an implicit conversion (§13.1) exists from Y to X, but not from X to Y, then X is the type of 33 the conditional expression. 34

• Otherwise, no expression type can be determined, and a compile-time error occurs. 35

The run-time processing of a conditional expression of the form b ? x : y consists of the following steps: 36

• First, b is evaluated, and the bool value of b is determined: 37

o If an implicit conversion from the type of b to bool exists, then this implicit conversion is 38 performed to produce a bool value. 39

o Otherwise, the operator true defined by the type of b is invoked to produce a bool value. 40

• If the bool value produced by the step above is true, then x is evaluated and converted to the type of 41 the conditional expression, and this becomes the result of the conditional expression. 42

• Otherwise, y is evaluated and converted to the type of the conditional expression, and this becomes the 43 result of the conditional expression. 44

Page 229: C# Language Specification

Chapter 14 Expressions

209

14.13 Assignment operators 1

The assignment operators assign a new value to a variable, a property, event, or an indexer element. 2

assignment: 3 unary-expression assignment-operator expression 4

assignment-operator: one of 5 = += -= *= /= %= &= |= ^= <<= right-shift-assignment 6

The left operand of an assignment shall be an expression classified as a variable, a property access, an 7 indexer access, or an event access. 8

The = operator is called the simple assignment operator. It assigns the value of the right operand to the 9 variable, property, or indexer element given by the left operand. The left operand of the simple assignment 10 operator shall not be an event access (except as described in §17.7.1). The simple assignment operator is 11 described in §14.13.1. 12

The operators formed by prefixing an = character with a binary operator are called the compound 13 assignment operators. These operators perform the indicated operation on the two operands, and then 14 assign the resulting value to the variable, property, or indexer element given by the left operand. The 15 compound assignment operators are described in §14.13.2. 16

The += and -= operators with an event access expression as the left operand are called the event 17 assignment operators. No other assignment operator is valid with an event access as the left operand. The 18 event assignment operators are described in §14.13.3. 19

The assignment operators are right-associative, meaning that operations are grouped from right to left. 20 [Example: An expression of the form a = b = c is evaluated as a = (b = c). end example] 21

14.13.1 Simple assignment 22

The = operator is called the simple assignment operator. In a simple assignment, the right operand shall be 23 an expression of a type that is implicitly convertible to the type of the left operand. The operation assigns the 24 value of the right operand to the variable, property, or indexer element given by the left operand. 25

The result of a simple assignment expression is the value assigned to the left operand. The result has the 26 same type as the left operand and is always classified as a value. 27

If the left operand is a property or indexer access, the property or indexer shall have a set accessor. If this is 28 not the case, a compile-time error occurs. 29

The run-time processing of a simple assignment of the form x = y consists of the following steps: 30

• If x is classified as a variable: 31

o x is evaluated to produce the variable. 32

o y is evaluated and, if required, converted to the type of x through an implicit conversion (§13.1). 33

o If the variable given by x is an array element of a reference-type, a run-time check is performed to 34 ensure that the value computed for y is compatible with the array instance of which x is an element. 35 The check succeeds if y is null, or if an implicit reference conversion (§13.1.4) exists from the 36 actual type of the instance referenced by y to the actual element type of the array instance containing 37 x. Otherwise, a System.ArrayTypeMismatchException is thrown. 38

o The value resulting from the evaluation and conversion of y is stored into the location given by the 39 evaluation of x. 40

• If x is classified as a property or indexer access: 41

o The instance expression (if x is not static) and the argument list (if x is an indexer access) 42 associated with x are evaluated, and the results are used in the subsequent set accessor invocation. 43

o y is evaluated and, if required, converted to the type of x through an implicit conversion (§13.1). 44

Page 230: C# Language Specification

C# LANGUAGE SPECIFICATION

210

o The set accessor of x is invoked with the value computed for y as its value argument. 1

[Note: The array covariance rules (§19.5) permit a value of an array type A[] to be a reference to an 2 instance of an array type B[], provided an implicit reference conversion exists from B to A. Because of 3 these rules, assignment to an array element of a reference-type requires a run-time check to ensure that the 4 value being assigned is compatible with the array instance. In the example 5

string[] sa = new string[10]; 6 object[] oa = sa; 7

oa[0] = null; // Ok 8 oa[1] = "Hello"; // Ok 9 oa[2] = new ArrayList(); // ArrayTypeMismatchException 10

the last assignment causes a System.ArrayTypeMismatchException to be thrown because an instance 11 of ArrayList cannot be stored in an element of a string[]. end note] 12

When a property or indexer declared in a struct-type is the target of an assignment, the instance expression 13 associated with the property or indexer access shall be classified as a variable. If the instance expression is 14 classified as a value, a compile-time error occurs. [Note: Because of §14.5.4, the same rule also applies to 15 fields. end note] 16

[Example: Given the declarations: 17

struct Point 18 { 19 int x, y; 20

public Point(int x, int y) { 21 this.x = x; 22 this.y = y; 23 } 24

public int X { 25 get { return x; } 26 set { x = value; } 27 } 28

public int Y { 29 get { return y; } 30 set { y = value; } 31 } 32 } 33

struct Rectangle 34 { 35 Point a, b; 36

public Rectangle(Point a, Point b) { 37 this.a = a; 38 this.b = b; 39 } 40

public Point A { 41 get { return a; } 42 set { a = value; } 43 } 44

public Point B { 45 get { return b; } 46 set { b = value; } 47 } 48 } 49

in the example 50

Point p = new Point(); 51 p.X = 100; 52 p.Y = 100; 53 Rectangle r = new Rectangle(); 54 r.A = new Point(10, 10); 55 r.B = p; 56

Page 231: C# Language Specification

Chapter 14 Expressions

211

the assignments to p.X, p.Y, r.A, and r.B are permitted because p and r are variables. However, in the 1 example 2

Rectangle r = new Rectangle(); 3 r.A.X = 10; 4 r.A.Y = 10; 5 r.B.X = 100; 6 r.B.Y = 100; 7

the assignments are all invalid, since r.A and r.B are not variables. end example] 8

14.13.2 Compound assignment 9

An operation of the form x op= y is processed by applying binary operator overload resolution (§14.2.4) as 10 if the operation was written x op y. Then, 11

• If the return type of the selected operator is implicitly convertible to the type of x, the operation is 12 evaluated as x = x op y, except that x is evaluated only once. 13

• Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is 14 explicitly convertible to the type of x, and if y is implicitly convertible to the type of x or the operator is 15 a shift operator, then the operation is evaluated as x = (T)(x op y), where T is the type of x, except 16 that x is evaluated only once. 17

• Otherwise, the compound assignment is invalid, and a compile-time error occurs. 18

The term “evaluated only once” means that in the evaluation of x op y, the results of any constituent 19 expressions of x are temporarily saved and then reused when performing the assignment to x. [Example: In 20 the assignment A()[B()] += C(), where A is a method returning int[], and B and C are methods returning 21 int, the methods are invoked only once, in the order A, B, C. end example] 22

When the left operand of a compound assignment is a property access or indexer access, the property or 23 indexer shall have both a get accessor and a set accessor. If this is not the case, a compile-time error 24 occurs. 25

The second rule above permits x op= y to be evaluated as x = (T)(x op y) in certain contexts. The rule 26 exists such that the predefined operators can be used as compound operators when the left operand is of type 27 sbyte, byte, short, ushort, or char. Even when both arguments are of one of those types, the 28 predefined operators produce a result of type int, as described in §14.2.6.2. Thus, without a cast it would 29 not be possible to assign the result to the left operand. 30

The intuitive effect of the rule for predefined operators is simply that x op= y is permitted if both of 31 x op y and x = y are permitted. [Example: In the following code 32

byte b = 0; 33 char ch = '\0'; 34 int i = 0; 35

b += 1; // Ok 36 b += 1000; // Error, b = 1000 not permitted 37 b += i; // Error, b = i not permitted 38 b += (byte)i; // Ok 39

ch += 1; // Error, ch = 1 not permitted 40 ch += (char)1; // Ok 41

the intuitive reason for each error is that a corresponding simple assignment would also have been an error. 42 end example] 43

14.13.3 Event assignment 44

If the left operand of a += or -= operator is classified as an event access, then the expression is evaluated as 45 follows: 46

• The instance expression, if any, of the event access is evaluated. 47

Page 232: C# Language Specification

C# LANGUAGE SPECIFICATION

212

• The right operand of the += or -= operator is evaluated, and, if required, converted to the type of the left 1 operand through an implicit conversion (§13.1). 2

• An event accessor of the event is invoked, with argument list consisting of the value computed in the 3 previous step. If the operator was +=, the add accessor is invoked; if the operator was -=, the remove 4 accessor is invoked. 5

An event assignment expression does not yield a value. Thus, an event assignment expression is valid only 6 in the context of a statement-expression (§15.6). 7

14.14 Expression 8

An expression is either a conditional-expression or an assignment. 9

expression: 10 conditional-expression 11 assignment 12

14.15 Constant expressions 13

A constant-expression is an expression that can be fully evaluated at compile-time. 14

constant-expression: 15 expression 16

The type of a constant expression can be one of the following: sbyte, byte, short, ushort, int, uint, 17 long, ulong, char, float, double, decimal, bool, string, any enumeration type, or the null type. 18 The following constructs are permitted in constant expressions: 19

• Literals (including the null literal). 20

• References to const members of class and struct types. 21

• References to members of enumeration types. 22

• Parenthesized sub-expressions, which are themselves constant expressions. 23

• Cast expressions, provided the target type is one of the types listed above. 24

• The predefined +, –, !, and ~ unary operators. 25

• The predefined +, –, *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <=, and >= binary operators, provided 26 each operand is of a type listed above. 27

• The ?: conditional operator. 28

Whenever an expression is of one of the types listed above and contains only the constructs listed above, the 29 expression is evaluated at compile-time. This is true even if the expression is a sub-expression of a larger 30 expression that contains non-constant constructs. 31

The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-32 constant expressions, except that where run-time evaluation would have thrown an exception, compile-time 33 evaluation causes a compile-time error to occur. 34

Unless a constant expression is explicitly placed in an unchecked context, overflows that occur in integral-35 type arithmetic operations and conversions during the compile-time evaluation of the expression always 36 cause compile-time errors (§14.5.12). 37

Constant expressions occur in the contexts listed below. In these contexts, a compile-time error occurs if an 38 expression cannot be fully evaluated at compile-time. 39

• Constant declarations (§17.3). 40

• Enumeration member declarations (§21.30). 41

Page 233: C# Language Specification

Chapter 14 Expressions

213

• case labels of a switch statement (§15.7.2). 1

• goto case statements (§15.9.3). 2

• Dimension lengths in an array creation expression (§14.5.10.2) that includes an initializer. 3

• Attributes (§24). 4

An implicit constant expression conversion (§13.1.7) permits a constant expression of type int to be 5 converted to sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant expression 6 is within the range of the destination type. 7

14.16 Boolean expressions 8

A boolean-expression is an expression that yields a result of type bool. 9

boolean-expression: 10 expression 11

The controlling conditional expression of an if-statement (§15.7.1), while-statement (§15.8.1), do-statement 12 (§15.8.2), or for-statement (§15.8.3) is a boolean-expression. The controlling conditional expression of the 13 ?: operator (§14.12) follows the same rules as a boolean-expression, but for reasons of operator precedence 14 is classified as a conditional-or-expression. 15

A boolean-expression is required to be of a type that can be implicitly converted to bool or of a type that 16 implements operator true. [Note: As required by §17.9.1, any type that implements operator true 17 shall also implement operator false. end note] If neither requirement is satisfied, a compile-time error 18 occurs. 19

When a boolean expression is of a type that cannot be implicitly converted to bool but does implement 20 operator true, then following evaluation of the expression, the operator true implementation 21 provided by that type is invoked to produce a bool value. 22

[Note: The DBBool struct type in §18.4.2 provides an example of a type that implements operator true 23 and operator false. end note] 24

Page 234: C# Language Specification
Page 235: C# Language Specification

Chapter 15 Statements

215

15. Statements 1

C# provides a variety of statements. [Note: Most of these statements will be familiar to developers who have 2 programmed in C and C++. end note] 3

statement: 4 labeled-statement 5 declaration-statement 6 embedded-statement 7

embedded-statement: 8 block 9 empty-statement 10 expression-statement 11 selection-statement 12 iteration-statement 13 jump-statement 14 try-statement 15 checked-statement 16 unchecked-statement 17 lock-statement 18 using-statement 19 yield-statement 20

The embedded-statement nonterminal is used for statements that appear within other statements. The use of 21 embedded-statement rather than statement excludes the use of declaration statements and labeled statements 22 in these contexts. [Example: The code 23

void F(bool b) { 24 if (b) 25 int i = 44; 26 } 27

results in a compile-time error because an if statement requires an embedded-statement rather than a 28 statement for its if branch. If this code were permitted, then the variable i would be declared, but it could 29 never be used. (Note, however, that by placing i’s declaration in a block, the example is valid.) end 30 example] 31

15.1 End points and reachability 32

Every statement has an end point. In intuitive terms, the end point of a statement is the location that 33 immediately follows the statement. The execution rules for composite statements (statements that contain 34 embedded statements) specify the action that is taken when control reaches the end point of an embedded 35 statement. [Example: When control reaches the end point of a statement in a block, control is transferred to 36 the next statement in the block. end example] 37

If a statement can possibly be reached by execution, the statement is said to be reachable. Conversely, if 38 there is no possibility that a statement will be executed, the statement is said to be unreachable. 39

[Example: In the following code 40

Page 236: C# Language Specification

C# LANGUAGE SPECIFICATION

216

void F() { 1 Console.WriteLine("reachable"); 2 goto Label; 3 Console.WriteLine("unreachable"); 4 Label: 5 Console.WriteLine("reachable"); 6 } 7

the second invocation of Console.WriteLine is unreachable because there is no possibility that the 8 statement will be executed. end example] 9

A warning is reported if the compiler determines that a statement is unreachable. It is specifically not an 10 error for a statement to be unreachable. 11

[Note: To determine whether a particular statement or end point is reachable, the compiler performs flow 12 analysis according to the reachability rules defined for each statement. The flow analysis takes into account 13 the values of constant expressions (§14.15) that control the behavior of statements, but the possible values of 14 non-constant expressions are not considered. In other words, for purposes of control flow analysis, a non-15 constant expression of a given type is considered to have any possible value of that type. 16

In the example 17

void F() { 18 const int i = 1; 19 if (i == 2) Console.WriteLine("unreachable"); 20 } 21

the boolean expression of the if statement is a constant expression because both operands of the 22 == operator are constants. As the constant expression is evaluated at compile-time, producing the value 23 false, the Console.WriteLine invocation is considered unreachable. However, if i is changed to be a 24 local variable 25

void F() { 26 int i = 1; 27 if (i == 2) Console.WriteLine("reachable"); 28 } 29

the Console.WriteLine invocation is considered reachable, even though, in reality, it will never be 30 executed. end note] 31

The block of a function member is always considered reachable. By successively evaluating the reachability 32 rules of each statement in a block, the reachability of any given statement can be determined. 33

[Example: In the following code 34

void F(int x) { 35 Console.WriteLine("start"); 36 if (x < 0) Console.WriteLine("negative"); 37 } 38

the reachability of the second Console.WriteLine is determined as follows: 39

• The first Console.WriteLine expression statement is reachable because the block of the F method is 40 reachable (§15.2). 41

• The end point of the first Console.WriteLine expression statement is reachable because that 42 statement is reachable (§15.6 and §15.2). 43

• The if statement is reachable because the end point of the first Console.WriteLine expression 44 statement is reachable (§15.6 and §15.2). 45

• The second Console.WriteLine expression statement is reachable because the boolean expression of 46 the if statement does not have the constant value false. 47

end example] 48

There are two situations in which it is a compile-time error for the end point of a statement to be reachable: 49

Page 237: C# Language Specification

Chapter 15 Statements

217

• Because the switch statement does not permit a switch section to “fall through” to the next switch 1 section, it is a compile-time error for the end point of the statement list of a switch section to be 2 reachable. If this error occurs, it is typically an indication that a break statement is missing. 3

• It is a compile-time error for the end point of the block of a function member that computes a value to be 4 reachable. If this error occurs, it typically is an indication that a return statement is missing. 5

15.2 Blocks 6

A block permits multiple statements to be written in contexts where a single statement is allowed. 7

block: 8 { statement-listopt } 9

A block consists of an optional statement-list (§15.2.1), enclosed in braces. If the statement list is omitted, 10 the block is said to be empty. 11

A block can contain declaration statements (§15.5). The scope of a local variable or constant declared in a 12 block is the block. 13

Within a block, the meaning of a name used in an expression or declarator context shall always be the same 14 (§14.5.2.1). 15

A block is executed as follows: 16

• If the block is empty, control is transferred to the end point of the block. 17

• If the block is not empty, control is transferred to the statement list. When and if control reaches the end 18 point of the statement list, control is transferred to the end point of the block. 19

The statement list of a block is reachable if the block itself is reachable. 20

The end point of a block is reachable if the block is empty or if the end point of the statement list is 21 reachable. 22

15.2.1 Statement lists 23

A statement list consists of one or more statements written in sequence. Statement lists occur in blocks 24 (§15.2) and in switch-blocks (§15.7.2). 25

statement-list: 26 statement 27 statement-list statement 28

A statement list is executed by transferring control to the first statement. When and if control reaches the end 29 point of a statement, control is transferred to the next statement. When and if control reaches the end point of 30 the last statement, control is transferred to the end point of the statement list. 31

A statement in a statement list is reachable if at least one of the following is true: 32

• The statement is the first statement and the statement list itself is reachable. 33

• The end point of the preceding statement is reachable. 34

• The statement is a labeled statement and the label is referenced by a reachable goto statement. 35

The end point of a statement list is reachable if the end point of the last statement in the list is reachable. 36

15.3 The empty statement 37

An empty-statement does nothing. 38

empty-statement: 39 ; 40

Page 238: C# Language Specification

C# LANGUAGE SPECIFICATION

218

An empty statement is used when there are no operations to perform in a context where a statement is 1 required. 2

Execution of an empty statement simply transfers control to the end point of the statement. Thus, the end 3 point of an empty statement is reachable if the empty statement is reachable. 4

[Example: An empty statement can be used when writing a while statement with a null body: 5

bool ProcessMessage() {…} 6

void ProcessMessages() { 7 while (ProcessMessage()) 8 ; 9 } 10

Also, an empty statement can be used to declare a label just before the closing “}” of a block: 11

void F() { 12 … 13

if (done) goto exit; 14 … 15

exit: ; 16 } 17

end example] 18

15.4 Labeled statements 19

A labeled-statement permits a statement to be prefixed by a label. Labeled statements are permitted in 20 blocks, but are not permitted as embedded statements. 21

labeled-statement: 22 identifier : statement 23

A labeled statement declares a label with the name given by the identifier. The scope of a label is the whole 24 block in which the label is declared, including any nested blocks. It is a compile-time error for two labels 25 with the same name to have overlapping scopes. 26

A label can be referenced from goto statements (§15.9.3) within the scope of the label. [Note: This means 27 that goto statements can transfer control within blocks and out of blocks, but never into blocks. end note] 28

Labels have their own declaration space and do not interfere with other identifiers. [Example: The example 29

int F(int x) { 30 if (x >= 0) goto x; 31 x = -x; 32 x: return x; 33 } 34

is valid and uses the name x as both a parameter and a label. end example] 35

Execution of a labeled statement corresponds exactly to execution of the statement following the label. 36

In addition to the reachability provided by normal flow of control, a labeled statement is reachable if the 37 label is referenced by a reachable goto statement, unless the goto statement is inside a try that includes a 38 finally block whose end point is unreachable, and the labeled statement is outside the try. 39

15.5 Declaration statements 40

A declaration-statement declares a local variable or constant. Declaration statements are permitted in blocks, 41 but are not permitted as embedded statements. 42

declaration-statement: 43 local-variable-declaration ; 44 local-constant-declaration ; 45

Page 239: C# Language Specification

Chapter 15 Statements

219

15.5.1 Local variable declarations 1

A local-variable-declaration declares one or more local variables. 2

local-variable-declaration: 3 type local-variable-declarators 4

local-variable-declarators: 5 local-variable-declarator 6 local-variable-declarators , local-variable-declarator 7

local-variable-declarator: 8 identifier 9 identifier = local-variable-initializer 10

local-variable-initializer: 11 expression 12 array-initializer 13

The type of a local-variable-declaration specifies the type of the variables introduced by the declaration. 14 The type is followed by a list of local-variable-declarators, each of which introduces a new variable. A 15 local-variable-declarator consists of an identifier that names the variable, optionally followed by an 16 “=” token and a local-variable-initializer that gives the initial value of the variable. 17

The value of a local variable is obtained in an expression using a simple-name (§14.5.2), and the value of a 18 local variable is modified using an assignment (§14.13). A local variable shall be definitely assigned (§12.3) 19 at each location where its value is obtained. 20

The scope of a local variable declared in a local-variable-declaration is the block in which the declaration 21 occurs. It is an error to refer to a local variable in a textual position that precedes the local-variable-22 declarator of the local variable. Within the scope of a local variable, it is a compile-time error to declare 23 another local variable or constant with the same name. 24

A local variable declaration that declares multiple variables is equivalent to multiple declarations of single 25 variables with the same type. Furthermore, a variable initializer in a local variable declaration corresponds 26 exactly to an assignment statement that is inserted immediately after the declaration. 27

[Example: The example 28

void F() { 29 int x = 1, y, z = x * 2; 30 } 31

corresponds exactly to 32

void F() { 33 int x; x = 1; 34 int y; 35 int z; z = x * 2; 36 } 37

end example] 38

15.5.2 Local constant declarations 39

A local-constant-declaration declares one or more local constants. 40

local-constant-declaration: 41 const type constant-declarators 42

constant-declarators: 43 constant-declarator 44 constant-declarators , constant-declarator 45

constant-declarator: 46 identifier = constant-expression 47

Page 240: C# Language Specification

C# LANGUAGE SPECIFICATION

220

The type of a local-constant-declaration specifies the type of the constants introduced by the declaration. 1 The type is followed by a list of constant-declarators, each of which introduces a new constant. A constant-2 declarator consists of an identifier that names the constant, followed by an “=” token, followed by a 3 constant-expression (§14.15) that gives the value of the constant. 4

The type and constant-expression of a local constant declaration shall follow the same rules as those of a 5 constant member declaration (§17.3). 6

The value of a local constant is obtained in an expression using a simple-name (§14.5.2). 7

The scope of a local constant is the block in which the declaration occurs. It is an error to refer to a local 8 constant in a textual position that precedes its constant-declarator. Within the scope of a local constant, it is 9 a compile-time error to declare another local variable or constant with the same name. 10

A local constant declaration that declares multiple constants is equivalent to multiple declarations of single 11 constants with the same type. 12

15.6 Expression statements 13

An expression-statement evaluates a given expression. The value computed by the expression, if any, is 14 discarded. 15

expression-statement: 16 statement-expression ; 17

statement-expression: 18 invocation-expression 19 object-creation-expression 20 assignment 21 post-increment-expression 22 post-decrement-expression 23 pre-increment-expression 24 pre-decrement-expression 25

Not all expressions are permitted as statements. [Note: In particular, expressions such as x + y and 26 x == 1, that merely compute a value (which will be discarded), are not permitted as statements. end note] 27

Execution of an expression statement evaluates the contained expression and then transfers control to the 28 end point of the expression statement. The end point of an expression-statement is reachable if that 29 expression-statement is reachable. 30

15.7 Selection statements 31

Selection statements select one of a number of possible statements for execution based on the value of some 32 expression. 33

selection-statement: 34 if-statement 35 switch-statement 36

15.7.1 The if statement 37

The if statement selects a statement for execution based on the value of a boolean expression. 38

if-statement: 39 if ( boolean-expression ) embedded-statement 40 if ( boolean-expression ) embedded-statement else embedded-statement 41

boolean-expression: 42 expression 43

An else part is associated with the lexically nearest preceding if that is allowed by the syntax. [Example: 44 Thus, an if statement of the form 45

Page 241: C# Language Specification

Chapter 15 Statements

221

if (x) if (y) F(); else G(); 1

is equivalent to 2

if (x) { 3 if (y) { 4 F(); 5 } 6 else { 7 G(); 8 } 9 } 10

end example] 11

An if statement is executed as follows: 12

• The boolean-expression (§14.16) is evaluated. 13

• If the boolean expression yields true, control is transferred to the first embedded statement. When and 14 if control reaches the end point of that statement, control is transferred to the end point of the if 15 statement. 16

• If the boolean expression yields false and if an else part is present, control is transferred to the 17 second embedded statement. When and if control reaches the end point of that statement, control is 18 transferred to the end point of the if statement. 19

• If the boolean expression yields false and if an else part is not present, control is transferred to the 20 end point of the if statement. 21

The first embedded statement of an if statement is reachable if the if statement is reachable and the 22 boolean expression does not have the constant value false. 23

The second embedded statement of an if statement, if present, is reachable if the if statement is reachable 24 and the boolean expression does not have the constant value true. 25

The end point of an if statement is reachable if the end point of at least one of its embedded statements is 26 reachable. In addition, the end point of an if statement with no else part is reachable if the if statement is 27 reachable and the boolean expression does not have the constant value true. 28

15.7.2 The switch statement 29

The switch statement selects for execution a statement list having an associated switch label that corresponds 30 to the value of the switch expression. 31

switch-statement: 32 switch ( expression ) switch-block 33

switch-block: 34 { switch-sectionsopt } 35

switch-sections: 36 switch-section 37 switch-sections switch-section 38

switch-section: 39 switch-labels statement-list 40

switch-labels: 41 switch-label 42 switch-labels switch-label 43

switch-label: 44 case constant-expression : 45 default : 46

Page 242: C# Language Specification

C# LANGUAGE SPECIFICATION

222

A switch-statement consists of the keyword switch, followed by a parenthesized expression (called the 1 switch expression), followed by a switch-block. The switch-block consists of zero or more switch-sections, 2 enclosed in braces. Each switch-section consists of one or more switch-labels followed by a statement-list 3 (§15.2.1). 4

The governing type of a switch statement is established by the switch expression. If the type of the switch 5 expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type, 6 then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit 7 conversion operator (§13.4) shall exist from the type of the switch expression or a base type of this type to 8 one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, 9 char, string. If no such implicit conversion operator exists, or if more than one such implicit conversion 10 operator exists, a compile-time error occurs. 11

The constant expression of each case label shall denote a value of a type that is implicitly convertible 12 (§13.1) to the governing type of the switch statement. A compile-time error occurs if two or more case 13 labels in the same switch statement specify the same constant value. 14

There can be at most one default label in a switch statement. 15

A switch statement is executed as follows: 16

• The switch expression is evaluated and converted to the governing type. 17

• If one of the constants specified in a case label in the same switch statement is equal to the value of 18 the switch expression, control is transferred to the statement list following the matched case label. 19

• If none of the constants specified in case labels in the same switch statement is equal to the value of 20 the switch expression, and if a default label is present, control is transferred to the statement list 21 following the default label. 22

• If none of the constants specified in case labels in the same switch statement is equal to the value of 23 the switch expression, and if no default label is present, control is transferred to the end point of the 24 switch statement. 25

If the end point of the statement list of a switch section is reachable, a compile-time error occurs. This is 26 known as the “no fall through” rule. [Example: The example 27

switch (i) { 28 case 0: 29 CaseZero(); 30 break; 31 case 1: 32 CaseOne(); 33 break; 34 default: 35 CaseOthers(); 36 break; 37 } 38

is valid because no switch section has a reachable end point. Unlike C and C++, execution of a switch 39 section is not permitted to “fall through” to the next switch section, and the example 40

switch (i) { 41 case 0: 42 CaseZero(); 43 case 1: 44 CaseZeroOrOne(); 45 default: 46 CaseAny(); 47 } 48

results in a compile-time error. When execution of a switch section is to be followed by execution of another 49 switch section, an explicit goto case or goto default statement shall be used: 50

Page 243: C# Language Specification

Chapter 15 Statements

223

switch (i) { 1 case 0: 2 CaseZero(); 3 goto case 1; 4 case 1: 5 CaseZeroOrOne(); 6 goto default; 7 default: 8 CaseAny(); 9 break; 10 } 11

end example] 12

Multiple labels are permitted in a switch-section. [Example: The example 13

switch (i) { 14 case 0: 15 CaseZero(); 16 break; 17 case 1: 18 CaseOne(); 19 break; 20 case 2: 21 default: 22 CaseTwo(); 23 break; 24 } 25

is valid. The example does not violate the “no fall through” rule because the labels case 2: and default: 26 are part of the same switch-section. end example] 27

[Note: The “no fall through” rule prevents a common class of bugs that occur in C and C++ when break 28 statements are accidentally omitted. In addition, because of this rule, the switch sections of a switch 29 statement can be arbitrarily rearranged without affecting the behavior of the statement. For example, the 30 sections of the switch statement above can be reversed without affecting the behavior of the statement: 31

switch (i) { 32 default: 33 CaseAny(); 34 break; 35 case 1: 36 CaseZeroOrOne(); 37 goto default; 38 case 0: 39 CaseZero(); 40 goto case 1; 41 } 42

end note] 43

[Note: The statement list of a switch section typically ends in a break, goto case, or goto default 44 statement, but any construct that renders the end point of the statement list unreachable is permitted. For 45 example, a while statement controlled by the boolean expression true is known to never reach its end 46 point. Likewise, a throw or return statement always transfers control elsewhere and never reaches its end 47 point. Thus, the following example is valid: 48

switch (i) { 49 case 0: 50 while (true) F(); 51 case 1: 52 throw new ArgumentException(); 53 case 2: 54 return; 55 } 56

end note] 57

[Example: The governing type of a switch statement can be the type string. For example: 58

Page 244: C# Language Specification

C# LANGUAGE SPECIFICATION

224

void DoCommand(string command) { 1 switch (command.ToLower()) { 2 case "run": 3 DoRun(); 4 break; 5 case "save": 6 DoSave(); 7 break; 8 case "quit": 9 DoQuit(); 10 break; 11 default: 12 InvalidCommand(command); 13 break; 14 } 15 } 16

end example] 17

[Note: Like the string equality operators (§14.9.7), the switch statement is case sensitive and will execute a 18 given switch section only if the switch expression string exactly matches a case label constant. end note] 19

When the governing type of a switch statement is string, the value null is permitted as a case label 20 constant. 21

The statement-lists of a switch-block can contain declaration statements (§15.5). The scope of a local 22 variable or constant declared in a switch block is the switch block. 23

Within a switch block, the meaning of a name used in an expression context shall always be the same 24 (§14.5.2.1). 25

The statement list of a given switch section is reachable if the switch statement is reachable and at least 26 one of the following is true: 27

• The switch expression is a non-constant value. 28

• The switch expression is a constant value that matches a case label in the switch section. 29

• The switch expression is a constant value that doesn’t match any case label, and the switch section 30 contains the default label. 31

• A switch label of the switch section is referenced by a reachable goto case or goto default 32 statement. 33

The end point of a switch statement is reachable if at least one of the following is true: 34

• The switch statement contains a reachable break statement that exits the switch statement. 35

• The switch statement is reachable, the switch expression is a non-constant value, and no default 36 label is present. 37

• The switch statement is reachable, the switch expression is a constant value that doesn’t match any 38 case label, and no default label is present. 39

15.8 Iteration statements 40

Iteration statements repeatedly execute an embedded statement. 41

iteration-statement: 42 while-statement 43 do-statement 44 for-statement 45 foreach-statement 46

15.8.1 The while statement 47

The while statement conditionally executes an embedded statement zero or more times. 48

Page 245: C# Language Specification

Chapter 15 Statements

225

while-statement: 1 while ( boolean-expression ) embedded-statement 2

A while statement is executed as follows: 3

• The boolean-expression (§14.16) is evaluated. 4

• If the boolean expression yields true, control is transferred to the embedded statement. When and if 5 control reaches the end point of the embedded statement (possibly from execution of a continue 6 statement), control is transferred to the beginning of the while statement. 7

• If the boolean expression yields false, control is transferred to the end point of the while statement. 8

Within the embedded statement of a while statement, a break statement (§15.9.1) can be used to transfer 9 control to the end point of the while statement (thus ending iteration of the embedded statement), and a 10 continue statement (§15.9.2) can be used to transfer control to the end point of the embedded statement 11 (thus performing another iteration of the while statement). 12

The embedded statement of a while statement is reachable if the while statement is reachable and the 13 boolean expression does not have the constant value false. 14

The end point of a while statement is reachable if at least one of the following is true: 15

• The while statement contains a reachable break statement that exits the while statement. 16

• The while statement is reachable and the boolean expression does not have the constant value true. 17

15.8.2 The do statement 18

The do statement conditionally executes an embedded statement one or more times. 19

do-statement: 20 do embedded-statement while ( boolean-expression ) ; 21

A do statement is executed as follows: 22

• Control is transferred to the embedded statement. 23

• When and if control reaches the end point of the embedded statement (possibly from execution of a 24 continue statement), the boolean-expression (§14.16) is evaluated. If the boolean expression yields 25 true, control is transferred to the beginning of the do statement. Otherwise, control is transferred to the 26 end point of the do statement. 27

Within the embedded statement of a do statement, a break statement (§15.9.1) can be used to transfer 28 control to the end point of the do statement (thus ending iteration of the embedded statement), and a 29 continue statement (§15.9.2) can be used to transfer control to the end point of the embedded statement 30 (thus performing another iteration of the do statement). 31

The embedded statement of a do statement is reachable if the do statement is reachable. 32

The end point of a do statement is reachable if at least one of the following is true: 33

• The do statement contains a reachable break statement that exits the do statement. 34

• The end point of the embedded statement is reachable and the boolean expression does not have the 35 constant value true. 36

15.8.3 The for statement 37

The for statement evaluates a sequence of initialization expressions and then, while a condition is true, 38 repeatedly executes an embedded statement and evaluates a sequence of iteration expressions. 39

for-statement: 40 for ( for-initializeropt ; for-conditionopt ; for-iteratoropt ) embedded-statement 41

Page 246: C# Language Specification

C# LANGUAGE SPECIFICATION

226

for-initializer: 1 local-variable-declaration 2 statement-expression-list 3

for-condition: 4 boolean-expression 5

for-iterator: 6 statement-expression-list 7

statement-expression-list: 8 statement-expression 9 statement-expression-list , statement-expression 10

The for-initializer, if present, consists of either a local-variable-declaration (§15.5.1) or a list of statement-11 expressions (§15.6) separated by commas. The scope of a local variable declared by a for-initializer starts at 12 the local-variable-declarator for the variable and extends to the end of the embedded statement. The scope 13 includes the for-condition and the for-iterator. 14

The for-condition, if present, shall be a boolean-expression (§14.16). 15

The for-iterator, if present, consists of a list of statement-expressions (§15.6) separated by commas. 16

A for statement is executed as follows: 17

• If a for-initializer is present, the variable initializers or statement expressions are executed in the order 18 they are written. This step is only performed once. 19

• If a for-condition is present, it is evaluated. 20

• If the for-condition is not present or if the evaluation yields true, control is transferred to the embedded 21 statement. When and if control reaches the end point of the embedded statement (possibly from 22 execution of a continue statement), the expressions of the for-iterator, if any, are evaluated in 23 sequence, and then another iteration is performed, starting with evaluation of the for-condition in the 24 step above. 25

• If the for-condition is present and the evaluation yields false, control is transferred to the end point of 26 the for statement. 27

Within the embedded statement of a for statement, a break statement (§15.9.1) can be used to transfer 28 control to the end point of the for statement (thus ending iteration of the embedded statement), and a 29 continue statement (§15.9.2) can be used to transfer control to the end point of the embedded statement 30 (thus executing another iteration of the for statement). 31

The embedded statement of a for statement is reachable if one of the following is true: 32

• The for statement is reachable and no for-condition is present. 33

• The for statement is reachable and a for-condition is present and does not have the constant value 34 false. 35

The end point of a for statement is reachable if at least one of the following is true: 36

• The for statement contains a reachable break statement that exits the for statement. 37

• The for statement is reachable and a for-condition is present and does not have the constant value 38 true. 39

15.8.4 The foreach statement 40

The foreach statement enumerates the elements of a collection, executing an embedded statement for each 41 element of the collection. 42

foreach-statement: 43 foreach ( type identifier in expression ) embedded-statement 44

Page 247: C# Language Specification

Chapter 15 Statements

227

The type and identifier of a foreach statement declare the iteration variable of the statement. The iteration 1 variable corresponds to a read-only local variable with a scope that extends over the embedded statement. 2 During execution of a foreach statement, the iteration variable represents the collection element for which 3 an iteration is currently being performed. A compile-time error occurs if the embedded statement attempts to 4 modify the iteration variable (via assignment or the ++ and -- operators) or pass the iteration variable as a 5 ref or out parameter. 6

The compile-time processing of a foreach statement first determines the collection type, enumerator type 7 and element type of the expression. This determination proceeds as follows: 8

• If the type X of expression is an array type then there is an implicit reference conversion from X to the 9 System.Collections.IEnumerable interface (since System.Array implements this interface). 10 The collection type is the System.Collections.IEnumerable interface, the enumerator type is the 11 System.Collections.IEnumerator interface and the element type is the element type of the array 12 type X. 13

• Otherwise, determine whether the type X has an appropriate GetEnumerator method: 14

o Perform member lookup on the type X with identifier GetEnumerator and no type arguments. If 15 the member lookup does not produce a match or produces an ambiguity or produces a match that is 16 not a method group, check for an enumerable interface as described below. It is recommended that a 17 warning be issued if member lookup produces anything except a method group or no match. 18

o Perform overload resolution using the resulting method group and an empty argument list. If 19 overload resolution results in no applicable methods or results in an ambiguity or results in a single 20 best method but that method is either static or not public, check for an enumerable interface as 21 described below. It is recommended that a warning be issued if overload resolution produces 22 anything except an unambiguous public instance method or no applicable methods. 23

o If the return type E of the GetEnumerator method is not a class, struct or interface type, an error is 24 produced and no further steps are taken. 25

o Member lookup is performed on E with the identifier Current and no type arguments. If the 26 member lookup produces no match or the result is an error or the result is anything except a public 27 instance property that permits reading, an error is produced and no further steps are taken. 28

o Member lookup is performed on E with the identifier MoveNext and no type arguments. If the 29 member lookup produces no match or the result is an error or the result is anything except a method 30 group, an error is produced and no further steps are taken. 31

o Overload resolution is performed on the method group with an empty argument list. If overload 32 resolution results in no applicable methods or results in an ambiguity or results in a single best 33 method but that method is either static or not public, or its return type is not bool, an error is 34 produced and no further steps are taken. 35

o The collection type is X, the enumerator type is E, and the element type is the type of the Current 36 property. 37

• Otherwise, check for an enumerable interface: 38

o If there is exactly one type T such that there is an implicit conversion from X to the interface 39 System.Collections.Generic.IEnumerable<T>, then the collection type is this interface, 40 the enumerator type is the interface System.Collections.Generic.IEnumerator<T>, and 41 the element type is T. 42

o Otherwise, if there is more than one such type T, then an error is produced and no further steps are 43 taken. 44

o Otherwise, if there is an implicit conversion from X to the System.Collections.IEnumerable 45 interface, then the collection type is this interface, the enumerator type is the interface 46 System.Collections.IEnumerator, and the element type is object. 47

Page 248: C# Language Specification

C# LANGUAGE SPECIFICATION

228

o Otherwise, an error is produced and no further steps are taken. 1

The above steps, if successful, unambiguously produce a collection type C, enumerator type E and element 2 type T. A foreach statement of the form 3

foreach (V v in x) embedded-statement 4

is then expanded to: 5

{ 6 E e = ((C)(x)).GetEnumerator(); 7 try { 8 while (e.MoveNext()) { 9 V v = (V)(T)e.Current; 10 embedded-statement 11 } 12 } 13 finally { 14 … // Dispose e 15 } 16 } 17

The variable e is not visible to or accessible to the expression x or the embedded statement or any other 18 souce code of the program. The variable v is read-only in the embedded statement. If there is not an explicit 19 conversion (§13.2) from T (the element type) to V (the type in the foreach statement), an error is produced 20 and no further steps are taken. [Note: If x has the value null, a System.NullReferenceException is 21 thrown at run-time. end note] 22

The body of the finally block is constructed according to the following steps: 23

• First determine whether the enumerator type E has an appropriate Dispose method: 24

o Perform member lookup on the type E with identifier Dispose and no type arguments. If the 25 member lookup does not produce a match or produces an ambiguity or produces a match that is not 26 a method group, check for the System.IDisposable interface as described below. It is 27 recommended that a warning be issued if member lookup produces anything except a method group 28 or no match. 29

o Perform overload resolution using the resulting method group and an empty argument list. If 30 overload resolution results in no applicable methods or results in an ambiguity or results in a single 31 best method but that method is either static or not public or has a return type other than void, check 32 for the System.IDisposable interface as described below. It is recommended that a warning be 33 issued if overload resolution produces anything except an unambiguous public instance method with 34 void return type or no applicable methods. 35

o The finally clause is expanded to: 36

finally { 37 e.Dispose(); 38 } 39

• Otherwise, check for the System.IDisposable interface: 40

o If there is an implicit conversion from E to the System.IDisposable interface, then the finally 41 clause is expanded to: 42

finally { 43 ((System.IDisposable)e).Dispose(); 44 } 45

o Otherwise, if E is a sealed type, the finally clause is expanded to an empty block: 46

finally { 47 } 48

o Otherwise, the finally clause is expanded to: 49

Page 249: C# Language Specification

Chapter 15 Statements

229

finally { 1 System.IDisposable d = e as System.IDisposable; 2 if (d != null) d.Dispose(); 3 } 4

The local d is not visible to or accessible to any user code. In particular, it does not conflict with any 5 other variable whose scope includes the finally block. 6

The order in which foreach traverses the elements of an array is as follows: For single-dimensional arrays 7 elements are traversed in increasing index order, starting with index 0 and ending with index Length – 1. 8 For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are 9 increased first, then the next left dimension, and so on to the left. 10

[Example: The following example prints out each value in a two-dimensional array, in element order: 11

using System; 12

class Test 13 { 14 static void Main() { 15 int[,] values = { 16 {1, 2, 3, 4}, 17 {5, 6, 7, 8} 18 }; 19

20

foreach (int elementValue in values) 21 Console.Write("{0} ", elementValue); 22 Console.WriteLine(); 23 } 24 } 25

The output produced is as follows: 26

1 2 3 4 5 6 7 8 27

end example] 28

15.9 Jump statements 29

Jump statements unconditionally transfer control. 30

jump-statement: 31 break-statement 32 continue-statement 33 goto-statement 34 return-statement 35 throw-statement 36

The location to which a jump statement transfers control is called the target of the jump statement. 37

When a jump statement occurs within a block, and the target of that jump statement is outside that block, the 38 jump statement is said to exit the block. While a jump statement can transfer control out of a block, it can 39 never transfer control into a block. 40

Execution of jump statements is complicated by the presence of intervening try statements. In the absence 41 of such try statements, a jump statement unconditionally transfers control from the jump statement to its 42 target. In the presence of such intervening try statements, execution is more complex. If the jump statement 43 exits one or more try blocks with associated finally blocks, control is initially transferred to the 44 finally block of the innermost try statement. When and if control reaches the end point of a finally 45 block, control is transferred to the finally block of the next enclosing try statement. This process is 46 repeated until the finally blocks of all intervening try statements have been executed. 47

[Example: In the following code 48

using System; 49

Page 250: C# Language Specification

C# LANGUAGE SPECIFICATION

230

class Test 1 { 2 static void Main() { 3 while (true) { 4 try { 5 try { 6 Console.WriteLine("Before break"); 7 break; 8 } 9 finally { 10 Console.WriteLine("Innermost finally block"); 11 } 12 } 13 finally { 14 Console.WriteLine("Outermost finally block"); 15 } 16 } 17 Console.WriteLine("After break"); 18 } 19 } 20

the finally blocks associated with two try statements are executed before control is transferred to the target 21 of the jump statement. 22

The output produced is as follows: 23

Before break 24 Innermost finally block 25 Outermost finally block 26 After break 27

end example] 28

15.9.1 The break statement 29

The break statement exits the nearest enclosing switch, while, do, for, or foreach statement. 30

break-statement: 31 break ; 32

The target of a break statement is the end point of the nearest enclosing switch, while, do, for, or 33 foreach statement. If a break statement is not enclosed by a switch, while, do, for, or foreach 34 statement, a compile-time error occurs. 35

When multiple switch, while, do, for, or foreach statements are nested within each other, a break 36 statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto 37 statement (§15.9.3) shall be used. 38

A break statement cannot exit a finally block (§15.10). When a break statement occurs within a 39 finally block, the target of the break statement shall be within the same finally block; otherwise a 40 compile-time error occurs. 41

A break statement is executed as follows: 42

• If the break statement exits one or more try blocks with associated finally blocks, control is 43 initially transferred to the finally block of the innermost try statement. When and if control reaches 44 the end point of a finally block, control is transferred to the finally block of the next enclosing try 45 statement. This process is repeated until the finally blocks of all intervening try statements have 46 been executed. 47

• Control is transferred to the target of the break statement. 48

Because a break statement unconditionally transfers control elsewhere, the end point of a break statement 49 is never reachable. 50

Page 251: C# Language Specification

Chapter 15 Statements

231

15.9.2 The continue statement 1

The continue statement starts a new iteration of the nearest enclosing while, do, for, or foreach 2 statement. 3

continue-statement: 4 continue ; 5

The target of a continue statement is the end point of the embedded statement of the nearest enclosing 6 while, do, for, or foreach statement. If a continue statement is not enclosed by a while, do, for, or 7 foreach statement, a compile-time error occurs. 8

When multiple while, do, for, or foreach statements are nested within each other, a continue 9 statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto 10 statement (§15.9.3) shall be used. 11

A continue statement cannot exit a finally block (§15.10). When a continue statement occurs within 12 a finally block, the target of the continue statement shall be within the same finally block; 13 otherwise a compile-time error occurs. 14

A continue statement is executed as follows: 15

• If the continue statement exits one or more try blocks with associated finally blocks, control is 16 initially transferred to the finally block of the innermost try statement. When and if control reaches 17 the end point of a finally block, control is transferred to the finally block of the next enclosing try 18 statement. This process is repeated until the finally blocks of all intervening try statements have 19 been executed. 20

• Control is transferred to the target of the continue statement. 21

Because a continue statement unconditionally transfers control elsewhere, the end point of a continue 22 statement is never reachable. 23

15.9.3 The goto statement 24

The goto statement transfers control to a statement that is marked by a label. 25

goto-statement: 26 goto identifier ; 27 goto case constant-expression ; 28 goto default ; 29

The target of a goto identifier statement is the labeled statement with the given label. If a label with the 30 given name does not exist in the current function member, or if the goto statement is not within the scope of 31 the label, a compile-time error occurs. [Note: This rule permits the use of a goto statement to transfer 32 control out of a nested scope, but not into a nested scope. In the example 33

using System; 34

class Test 35 { 36 static void Main(string[] args) { 37 string[,] table = { 38 {"red", "blue", "green"}, 39 {"Monday", "Wednesday", "Friday"} 40 }; 41

foreach (string str in args) { 42 int row, colm; 43 for (row = 0; row <= 1; ++row) 44 for (colm = 0; colm <= 2; ++colm) 45 if (str == table[row,colm]) 46 goto done; 47

Page 252: C# Language Specification

C# LANGUAGE SPECIFICATION

232

Console.WriteLine("{0} not found", str); 1 continue; 2 done: 3 Console.WriteLine("Found {0} at [{1}][{2}]", str, row, colm); 4 } 5 } 6 } 7

a goto statement is used to transfer control out of a nested scope. end note] 8

The target of a goto case statement is the statement list in the immediately enclosing switch statement 9 (§15.7.2) which contains a case label with the given constant value. If the goto case statement is not 10 enclosed by a switch statement, if the constant-expression is not implicitly convertible (§13.1) to the 11 governing type of the nearest enclosing switch statement, or if the nearest enclosing switch statement 12 does not contain a case label with the given constant value, a compile-time error occurs. 13

The target of a goto default statement is the statement list in the immediately enclosing switch statement 14 (§15.7.2), which contains a default label. If the goto default statement is not enclosed by a switch 15 statement, or if the nearest enclosing switch statement does not contain a default label, a compile-time 16 error occurs. 17

A goto statement cannot exit a finally block (§15.10). When a goto statement occurs within a finally 18 block, the target of the goto statement shall be within the same finally block, or otherwise a compile-19 time error occurs. 20

A goto statement is executed as follows: 21

• If the goto statement exits one or more try blocks with associated finally blocks, control is initially 22 transferred to the finally block of the innermost try statement. When and if control reaches the end 23 point of a finally block, control is transferred to the finally block of the next enclosing try 24 statement. This process is repeated until the finally blocks of all intervening try statements have 25 been executed. 26

• Control is transferred to the target of the goto statement. 27

Because a goto statement unconditionally transfers control elsewhere, the end point of a goto statement is 28 never reachable. 29

15.9.4 The return statement 30

The return statement returns control to the caller of the function member in which the return statement 31 appears. 32

return-statement: 33 return expressionopt ; 34

A return statement with no expression can be used only in a function member that does not compute a 35 value; that is, a method with the return type void, the set accessor of a property or indexer, the add and 36 remove accessors of an event, an instance constructor, static constructor, or a destructor. 37

A return statement with an expression can only be used in a function member that computes a value, that 38 is, a method with a non-void return type, the get accessor of a property or indexer, or a user-defined 39 operator. An implicit conversion (§13.1) shall exist from the type of the expression to the return type of the 40 containing function member. 41

It is a compile-time error for a return statement to appear in a finally block (§15.10). 42

A return statement is executed as follows: 43

• If the return statement specifies an expression, the expression is evaluated and the resulting value is 44 converted to the return type of the containing function member by an implicit conversion. The result of 45 the conversion becomes the value returned to the caller. 46

Page 253: C# Language Specification

Chapter 15 Statements

233

• If the return statement is enclosed by one or more try blocks with associated finally blocks, 1 control is initially transferred to the finally block of the innermost try statement. When and if 2 control reaches the end point of a finally block, control is transferred to the finally block of the 3 next enclosing try statement. This process is repeated until the finally blocks of all enclosing try 4 statements have been executed. 5

• Control is returned to the caller of the containing function member. 6

Because a return statement unconditionally transfers control elsewhere, the end point of a return 7 statement is never reachable. 8

15.9.5 The throw statement 9

The throw statement throws an exception. 10

throw-statement: 11 throw expressionopt ; 12

A throw statement with an expression throws the value produced by evaluating the expression. The 13 expression shall denote a value of the class type System.Exception or of a class type that derives from 14 System.Exception. If evaluation of the expression produces null, a 15 System.NullReferenceException is thrown instead. 16

The throw statement can be used with an expression whose type is given by a type parameter (§26.1.1) only 17 if that type parameter has System.Exception (or a subclass thereof) as its effective base class (§26.7). 18

A throw statement with no expression can be used only in a catch block, in which case, that statement re-19 throws the exception that is currently being handled by that catch block. 20

Because a throw statement unconditionally transfers control elsewhere, the end point of a throw statement 21 is never reachable. 22

When an exception is thrown, control is transferred to the first catch clause in an enclosing try statement 23 that can handle the exception. The process that takes place from the point of the exception being thrown to 24 the point of transferring control to a suitable exception handler is known as exception propagation. 25 Propagation of an exception consists of repeatedly evaluating the following steps until a catch clause that 26 matches the exception is found. In this description, the throw point is initially the location at which the 27 exception is thrown. 28

• In the current function member, each try statement that encloses the throw point is examined. For each 29 statement S, starting with the innermost try statement and ending with the outermost try statement, the 30 following steps are evaluated: 31

o If the try block of S encloses the throw point and if S has one or more catch clauses, the catch 32 clauses are examined in order of appearance to locate a suitable handler for the exception. The first 33 catch clause that specifies the exception type or a base type of the exception type is considered a 34 match. A general catch (§15.10) clause is considered a match for any exception type. If a matching 35 catch clause is located, the exception propagation is completed by transferring control to the block 36 of that catch clause. 37

o Otherwise, if the try block or a catch block of S encloses the throw point and if S has a finally 38 block, control is transferred to the finally block. If the finally block throws another exception, 39 processing of the current exception is terminated. Otherwise, when control reaches the end point of 40 the finally block, processing of the current exception is continued. 41

• If an exception handler was not located in the current function member invocation, the function member 42 invocation is terminated. The steps above are then repeated for the caller of the function member with a 43 throw point corresponding to the statement from which the function member was invoked. 44

• If the exception processing terminates all function member invocations in the current thread, indicating 45 that the thread has no handler for the exception, then the thread is itself terminated. The impact of such 46 termination is implementation-defined. 47

Page 254: C# Language Specification

C# LANGUAGE SPECIFICATION

234

15.10 The try statement 1

The try statement provides a mechanism for catching exceptions that occur during execution of a block. 2 Furthermore, the try statement provides the ability to specify a block of code that is always executed when 3 control leaves the try statement. 4

try-statement: 5 try block catch-clauses 6 try block catch-clausesopt finally-clause 7

catch-clauses: 8 specific-catch-clauses 9 specific-catch-clausesopt general-catch-clause 10

specific-catch-clauses: 11 specific-catch-clause 12 specific-catch-clauses specific-catch-clause 13

specific-catch-clause: 14 catch ( class-type identifieropt ) block 15

general-catch-clause: 16 catch block 17

finally-clause: 18 finally block 19

There are three possible forms of try statements: 20

• A try block followed by one or more catch blocks. 21

• A try block followed by a finally block. 22

• A try block followed by one or more catch blocks followed by a finally block. 23

When a catch clause specifies a class-type, the type shall be System.Exception or a type that derives 24 from System.Exception. 25

When a catch clause specifies both a class-type and an identifier, an exception variable of the given name 26 and type is declared. The exception variable corresponds to a local variable with a scope that extends over 27 the catch block. During execution of the catch block, the exception variable represents the exception 28 currently being handled. For purposes of definite assignment checking, the exception variable is considered 29 definitely assigned in its entire scope. 30

Unless a catch clause includes an exception variable name, it is impossible to access the exception object 31 in the catch block. 32

The type named in a catch clause can be a type parameter only if that type parameter (§26.1.1) has 33 System.Exception (or a subclass thereof) as its effective base class (§26.7). 34

A catch clause that specifies neither an exception type nor an exception variable name is called a general 35 catch clause. A try statement can only have one general catch clause, and if one is present it shall be the 36 last catch clause. 37

[Note: Some environments, especially those supporting multiple languages, might support exceptions that 38 are not representable as an object derived from System.Exception, although such an exception could 39 never be generated by C# code. In such an environment, a general catch clause might be used to catch such 40 an exception. Thus, a general catch clause is semantically different from one that specifies the type 41 System.Exception, in that the former might also catch exceptions from other languages. end note] 42

In order to locate a handler for an exception, catch clauses are examined in lexical order. A compile-time 43 error occurs if a catch clause specifies a type that is the same as, or is derived from, a type that was 44 specified in an earlier catch clause for the same try. [Note: Without this restriction, it would be possible to 45 write unreachable catch clauses. end note] 46

Page 255: C# Language Specification

Chapter 15 Statements

235

Within a catch block, a throw statement (§15.9.5) with no expression can be used to re-throw the 1 exception that was caught by the catch block. Assignments to an exception variable do not alter the 2 exception that is re-thrown. 3

[Example: In the following code 4

using System; 5

class Test 6 { 7 static void F() { 8 try { 9 G(); 10 } 11 catch (Exception e) { 12 Console.WriteLine("Exception in F: " + e.Message); 13 e = new Exception("F"); 14 throw; // re-throw 15 } 16 } 17

static void G() { 18 throw new Exception("G"); 19 } 20

static void Main() { 21 try { 22 F(); 23 } 24 catch (Exception e) { 25 Console.WriteLine("Exception in Main: " + e.Message); 26 } 27 } 28 } 29

the method F catches an exception, writes some diagnostic information to the console, alters the exception 30 variable, and re-throws the exception. The exception that is re-thrown is the original exception, so the output 31 produced is: 32

Exception in F: G 33 Exception in Main: G 34

If the first catch block had thrown e instead of rethrowing the current exception, the output produced would 35 be as follows: 36

Exception in F: G 37 Exception in Main: F 38

end example] 39

It is a compile-time error for a break, continue, or goto statement to transfer control out of a finally 40 block. When a break, continue, or goto statement occurs in a finally block, the target of the statement 41 shall be within the same finally block, or otherwise a compile-time error occurs. 42

It is a compile-time error for a return statement to occur in a finally block. 43

A try statement is executed as follows: 44

• Control is transferred to the try block. 45

• When and if control reaches the end point of the try block: 46

o If the try statement has a finally block, the finally block is executed. 47

o Control is transferred to the end point of the try statement. 48

• If an exception is propagated to the try statement during execution of the try block: 49

o The catch clauses, if any, are examined in order of appearance to locate a suitable handler for the 50 exception. The first catch clause that specifies the exception type or a base type of the exception 51

Page 256: C# Language Specification

C# LANGUAGE SPECIFICATION

236

type is considered a match. A general catch clause is considered a match for any exception type. If 1 a matching catch clause is located: 2

• If the matching catch clause declares an exception variable, the exception object is assigned to 3 the exception variable. 4

• Control is transferred to the matching catch block. 5

• When and if control reaches the end point of the catch block: 6

o If the try statement has a finally block, the finally block is executed. 7

o Control is transferred to the end point of the try statement. 8

• If an exception is propagated to the try statement during execution of the catch block: 9

o If the try statement has a finally block, the finally block is executed. 10

o The exception is propagated to the next enclosing try statement. 11

o If the try statement has no catch clauses or if no catch clause matches the exception: 12

• If the try statement has a finally block, the finally block is executed. 13

• The exception is propagated to the next enclosing try statement. 14

The statements of a finally block are always executed when control leaves a try statement. This is true 15 whether the control transfer occurs as a result of normal execution, as a result of executing a break, 16 continue, goto, or return statement, or as a result of propagating an exception out of the try statement. 17

If an exception is thrown during execution of a finally block, the exception is propagated to the next 18 enclosing try statement. If another exception was in the process of being propagated, that exception is lost. 19 The process of propagating an exception is discussed further in the description of the throw statement 20 (§15.9.5). 21

The try block of a try statement is reachable if the try statement is reachable. 22

A catch block of a try statement is reachable if the try statement is reachable. 23

The finally block of a try statement is reachable if the try statement is reachable. 24

The end point of a try statement is reachable if both of the following are true: 25

• The end point of the try block is reachable or the end point of at least one catch block is reachable. 26

• If a finally block is present, the end point of the finally block is reachable. 27

15.11 The checked and unchecked statements 28

The checked and unchecked statements are used to control the overflow checking context for integral-29 type arithmetic operations and conversions. 30

checked-statement: 31 checked block 32

unchecked-statement: 33 unchecked block 34

The checked statement causes all expressions in the block to be evaluated in a checked context, and the 35 unchecked statement causes all expressions in the block to be evaluated in an unchecked context. 36

The checked and unchecked statements are precisely equivalent to the checked and unchecked 37 operators (§14.5.12), except that they operate on blocks instead of expressions. 38

Page 257: C# Language Specification

Chapter 15 Statements

237

15.12 The lock statement 1

The lock statement obtains the mutual-exclusion lock for a given object, executes a statement, and then 2 releases the lock. 3

lock-statement: 4 lock ( expression ) embedded-statement 5

The compile time type of the expression of a lock statement shall be a reference-type or a type parameter 6 (§26.1.1). It is a compile-time error for the compile time type of the expression to denote a value-type. When 7 the compile time type of expression is a type parameter (§26.1.1) and the run-time type of the expression is a 8 value type, the value of the expression is boxed and the mutual exclusion lock is obtained on the boxed 9 value. In this case the locking will have no effect since the boxed value could not have any other references 10 to it. 11

A lock statement of the form 12

lock (x) … 13

is precisely equivalent to: 14

object obj = x; 15

System.Threading.Monitor.Enter(obj); 16 try { 17 … 18 } 19 finally { 20 System.Threading.Monitor.Exit(obj); 21 } 22

[Example: 23

class Cache 24 { 25 public void Add(object x) { 26 lock (key) { 27 … 28 } 29 } 30

public void Remove(object x) { 31 lock (key) { 32 … 33 } 34 } 35

private readonly object key = new object(); 36 } 37

end example] 38

15.13 The using statement 39

The using statement obtains one or more resources, executes a statement, and then disposes of the resource. 40

using-statement: 41 using ( resource-acquisition ) embedded-statement 42

resource-acquisition: 43 local-variable-declaration 44 expression 45

A resource is a class or struct that either has a suitable Dispose method or implements the 46 System.IDisposable interface. The System.IDisposable interface includes a single parameterless 47 method named Dispose. Code that is using a resource can call Dispose to indicate that the resource is no 48 longer needed. If Dispose is not called, then automatic disposal eventually occurs as a consequence of 49 garbage collection (assuming the destructor of the resource type calls Dispose or otherwise performs the 50 equivalent actions). 51

Page 258: C# Language Specification

C# LANGUAGE SPECIFICATION

238

Local variables declared in a resource-acquisition are read-only, and shall include an initializer. A compile-1 time error occurs if the embedded statement attempts to modify these local variables (via assignment or the 2 ++ and -- operators) or pass them as ref or out parameters. 3

A using statement is translated into three parts: acquisition, usage, and disposal. Usage of the resource is 4 implicitly enclosed in a try statement that includes a finally clause. This finally clause disposes of the 5 resource. If a null resource is acquired, then no call to Dispose is made, and no exception is thrown. 6

A using statement of the form 7

using (expression) embedded-statement 8

is equivalent to a using statement of the form: 9

using (ResourceType resource = expression) embedded-statement 10

where ResourceType is the type of the expression and the resource variable is not visible to or 11 accessible from any source code of the program. 12

A using statement of the form 13

using (ResourceType resource = expression) embedded-statement 14

corresponds to the expansion: 15

{ 16 ResourceType resource = expression; 17 try { 18 embedded-statement 19 } 20 finally { 21 … // Dispose of resource 22 } 23 } 24

The resource variable is read-only in the embedded statement. 25

The precise form of the finally block is determined as follows: 26

• First determine whether the resource type R has an appropriate Dispose method: 27

o Perform member lookup on the type R with identifier Dispose and no type arguments. If the 28 member lookup does not produce a match or produces an ambiguity or produces a match that is not 29 a method group, check for the System.IDisposable interface as described below. It is 30 recommended that a warning be issued if member lookup produces anything except a method group 31 or no match. 32

o Perform overload resolution using the resulting method group and an empty argument list. If 33 overload resolution results in no applicable methods or results in an ambiguity or results in a single 34 best method but that method is either static or not public or has a return type other than void, check 35 for the System.IDisposable interface as described below. It is recommended that a warning be 36 issued if overload resolution produces anything except an unambiguous public instance method with 37 void return type or no applicable methods. 38

o If ResourceType is a value type or a type parameter with the value type constraint (§26.7), the 39 finally clause is expanded to: 40

finally { 41 resource.Dispose(); 42 } 43

o Otherwise, the finally clause is expanded to: 44

finally { 45 if (resource != null) resource.Dispose(); 46 } 47

• Otherwise, check for the System.IDisposable interface: 48

Page 259: C# Language Specification

Chapter 15 Statements

239

o If there is not an implicit conversion from R to the System.IDisposable interface, then a 1 compile-time error is produced. 2

o Otherwise, if ResourceType is a value type or a type parameter with the value type constraint 3 (§26.7), the finally clause is expanded to: 4

finally { 5 ((System.IDisposable)resource).Dispose(); 6 } 7

o Otherwise, the finally clause is expanded to: 8

finally { 9 if (resource != null) ((System.IDisposable)resource).Dispose(); 10 } 11

When a resource-acquisition takes the form of a local-variable-declaration, it is possible to acquire multiple 12 resources of a given type. A using statement of the form 13

using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) statement 14

is precisely equivalent to a sequence of nested using statements: 15

using (ResourceType r1 = e1) 16 using (ResourceType r2 = e2) 17 ... 18 using (ResourceType rN = eN) 19 statement 20

[Example: The example below creates a file named log.txt and writes two lines of text to the file. The 21 example then opens that same file for reading and copies the contained lines of text to the console. 22

using System; 23 using System.IO; 24

class Test 25 { 26 static void Main() { 27 using (TextWriter w = File.CreateText("log.txt")) { 28 w.WriteLine("This is line one"); 29 w.WriteLine("This is line two"); 30 } 31

using (TextReader r = File.OpenText("log.txt")) { 32 string s; 33 while ((s = r.ReadLine()) != null) { 34 Console.WriteLine(s); 35 } 36

} 37 } 38 } 39

Since the TextWriter and TextReader classes implement the IDisposable interface, the example can 40 use using statements to ensure that the underlying file is properly closed following the write or read 41 operations. end example] 42

15.14 The yield statement 43

The yield statement is used in an iterator block (§27.1) to yield a value to the enumerator object or to 44 signal the end of the iteration. 45

yield-statement: 46 yield return expression ; 47 yield break ; 48

To ensure compatibility with existing programs, yield is not a keyword; yield has special meaning only 49 when it is used immediately before a return or break keyword. In other contexts, yield can be used as 50 an identifier. 51

Page 260: C# Language Specification

C# LANGUAGE SPECIFICATION

240

The are several restrictions on where a yield statement can appear, as described in the following. 1

• It is a compile-time error for a yield statement (of either form) to appear outside a method-body, 2 operator-body, or accessor-body. 3

• It is a compile-time error for a yield statement (of either form) to appear inside an anonymous method. 4

• It is a compile-time error for a yield statement (of either form) to appear in the finally clause of a 5 try statement. 6

• It is a compile-time error for a yield return statement to appear anywhere in a try statement that 7 contains catch-clauses. 8

[Example: The following example shows some valid and invalid uses of yield statements. 9

delegate IEnumerable<int> D(); 10

IEnumerator<int> GetEnumerator() { 11 try { 12 yield return 1; // Ok 13 yield break; // Ok 14 } 15 finally { 16 yield return 2; // Error, yield in finally 17 yield break; // Error, yield in finally 18 } 19

try { 20 yield return 3; // Error, yield return in try...catch 21 yield break; // Ok 22 } 23 catch { 24 yield return 4; // Error, yield return in try...catch 25 yield break; // Ok 26 } 27

D d = delegate { 28 yield return 5; // Error, yield in an anonymous method 29 }; 30 } 31

int MyMethod() { 32 yield return 1; // Error, wrong return type for an iterator 33 } 34

end example] 35

An implicit conversion (§13.1) shall exist from the type of the expression in the yield return statement to 36 the yield type (§27.1.3) of the iterator block. 37

A yield return statement is executed as follows: 38

• The expression given in the statement is evaluated, implicitly converted to the yield type, and assigned 39 to the Current property of the enumerator object. 40

• Execution of the iterator block is suspended. If the yield return statement is within one or more try 41 blocks, the associated finally blocks are not executed at this time. 42

• The MoveNext method of the enumerator object returns true to its caller, indicating that the 43 enumerator object successfully advanced to the next item. 44

The next call to the enumerator object’s MoveNext method resumes execution of the iterator block from 45 where it was last suspended, namely at the end point of the yield return statement that caused the 46 suspension. 47

A yield break statement is executed as follows: 48

• If the yield break statement is enclosed by one or more try blocks with associated finally blocks, 49 control is initially transferred to the finally block of the innermost try statement. When and if 50

Page 261: C# Language Specification

Chapter 15 Statements

241

control reaches the end point of a finally block, control is transferred to the finally block of the 1 next enclosing try statement. This process is repeated until the finally blocks of all enclosing try 2 statements have been executed. 3

• Control is returned to the caller of the iterator block. This is either the MoveNext method or Dispose 4 method of the enumerator object. 5

Because a yield break statement unconditionally transfers control elsewhere, the end point of a yield 6 break statement is never reachable. 7

Page 262: C# Language Specification
Page 263: C# Language Specification

Chapter 16 Namespaces

243

16. Namespaces 1

C# programs are organized using namespaces. Namespaces are used both as an “internal” organization 2 system for a program, and as an “external” organization system—a way of presenting program elements that 3 are exposed to other programs. 4

Using directives (§16.3) are provided to facilitate the use of namespaces. 5

16.1 Compilation units 6

A compilation-unit defines the overall structure of a source file. A compilation unit consists of zero or more 7 extern-alias-directives followed by zero or more using-directives followed by zero or more global-attributes 8 followed by zero or more namespace-member-declarations. 9

compilation-unit: 10 extern-alias-directivesopt using-directivesopt global-attributesopt 11 namespace-member-declarationsopt 12

A C# program consists of one or more compilation units, each contained in a separate source file. When a 13 C# program is compiled, all of the compilation units are processed together. Thus, compilation units can 14 depend on each other, possibly in a circular fashion. 15

The extern-alias-directives of a compilation unit affect the using-directives, global-attributes and 16 namespace-member-declarations of that compilation unit, but have no effect on other compilation units. 17

The using-directives of a compilation unit affect the global-attributes and namespace-member-declarations 18 of that compilation unit, but have no effect on other compilation units. 19

The global-attributes (§24) of a compilation unit permit the specification of attributes for the target 20 assembly. Assemblies act as physical containers for types. 21

The namespace-member-declarations of each compilation unit of a program contribute members to a single 22 declaration space called the global namespace. [Example: 23

File A.cs: 24

class A {} 25

File B.cs: 26

class B {} 27

The two compilation units contribute to the single global namespace, in this case declaring two classes with 28 the fully qualified names A and B. Because the two compilation units contribute to the same declaration 29 space, it would have been an error if each contained a declaration of a member with the same name. end 30 example] 31

16.2 Namespace declarations 32

A namespace-declaration consists of the keyword namespace, followed by a namespace name and body, 33 optionally followed by a semicolon. 34

namespace-declaration: 35 namespace qualified-identifier namespace-body ;opt 36

qualified-identifier: 37 identifier 38 qualified-identifier . identifier 39

Page 264: C# Language Specification

C# LANGUAGE SPECIFICATION

244

namespace-body: 1 { extern-alias-directivesopt using-directivesopt namespace-member-declarationsopt } 2

A namespace-declaration can occur as a top-level declaration in a compilation-unit or as a member 3 declaration within another namespace-declaration. When a namespace-declaration occurs as a top-level 4 declaration in a compilation-unit, the namespace becomes a member of the global namespace. When a 5 namespace-declaration occurs within another namespace-declaration, the inner namespace becomes a 6 member of the outer namespace. In either case, the name of a namespace shall be unique within the 7 containing namespace. 8

Namespaces are implicitly public and the declaration of a namespace cannot include any access modifiers. 9

Within a namespace-body, the optional using-directives import the names of other namespaces and types, 10 allowing them to be referenced directly instead of through qualified names. The optional namespace-11 member-declarations contribute members to the declaration space of the namespace. All extern-alias-12 directives shall appear before any using-directives and all extern-alias-directives and using-directives shall 13 appear before any member declarations. 14

The qualified-identifier of a namespace-declaration can be a single identifier or a sequence of identifiers 15 separated by “.” tokens. The latter form permits a program to define a nested namespace without lexically 16 nesting several namespace declarations. [Example: 17

namespace N1.N2 18 { 19 class A {} 20

class B {} 21 } 22

is semantically equivalent to 23

namespace N1 24 { 25 namespace N2 26 { 27 class A {} 28

class B {} 29 } 30 } 31

end example] 32

Namespaces are open-ended, and two namespace declarations with the same fully qualified name (§10.8.2) 33 contribute to the same declaration space (§10.3). [Example: In the following code 34

namespace N1.N2 35 { 36 class A {} 37 } 38

namespace N1.N2 39 { 40 class B {} 41 } 42

the two namespace declarations above contribute to the same declaration space, in this case declaring two 43 classes with the fully qualified names N1.N2.A and N1.N2.B. Because the two declarations contribute to 44 the same declaration space, it would have been an error if each contained a declaration of a member with the 45 same name. end example] 46

Namespaces whose names begin with System. are reserved for use by the Standard Library (§Annex D). 47

16.3 Extern alias directives 48

An extern-alias-directive introduces an identifier that serves as an alias for an externally defined namespace. 49 The specification of the aliased namespace is external to the source code of the program. 50

Page 265: C# Language Specification

Chapter 16 Namespaces

245

extern-alias-directives: 1 extern-alias-directive 2 extern-alias-directives extern-alias-directive 3

extern-alias-directive: 4 extern alias identifier ; 5

The scope of an extern-alias-directive extends over the using-directives, global-attributes and namespace-6 member-declarations of its immediately containing compilation-unit or namespace-body. An extern-alias-7 directive contributes its name to the alias declaration space of the containing compilation unit or namespace 8 body (§10.3) and not to the declaration space of the containing namespace. 9

Within a compilation unit or namespace body that contains an extern-alias-directive, the identifier 10 introduced by the extern-alias-directive can be used to reference the aliased namespace. It is a compile-time 11 error for the identifier to be the word global. 12

Within C# source code, a type is declared to be a member of a single namespace. However, a namespace 13 hierarchy referenced by an extern alias may contain types that are also members of other namespaces. For 14 example, if A and B are extern aliases, the names A::X, B::C.Y and global::D.Z may, depending on the 15 external specification supported by the particular compiler, all refer to the same type. 16

The alias introduced by an extern-alias-directive is very similar to the alias introduced by a using-alias-17 directive. See §16.4.1 for more detailed discussion of extern-alias-directives and using-alias-directives. 18

Like get and set in property accessors, alias is not a keyword (§9.4.3). The word alias only has special 19 meaning when it immediately follows the extern keyword in an extern-alias-directive. [Example: In fact 20 an extern alias could use the identifier alias as its name: 21

extern alias alias; 22

end example] 23

16.4 Using directives 24

Using-directives facilitate the use of namespaces and types defined in other namespaces. Using-directives 25 impact the name resolution process of namespace-or-type-names (§10.8) and simple-names (§14.5.2), but 26 unlike declarations, using-directives do not contribute new members to the underlying declaration spaces of 27 the compilation units or namespaces within which they are used. 28

using-directives: 29 using-directive 30 using-directives using-directive 31

using-directive: 32 using-alias-directive 33 using-namespace-directive 34

A using-alias-directive (§16.4.1) introduces an alias for a namespace or type. 35

A using-namespace-directive (§16.4.2) imports the type members of a namespace. 36

The scope of a using-directive extends over the namespace-member-declarations of its immediately 37 containing compilation unit or namespace body. The scope of a using-directive specifically does not include 38 its peer using-directives. Thus, peer using-directives do not affect each other, and the order in which they are 39 written is insignificant. In contrast, the scope of an extern-alias-directive includes the using-directives 40 defined in the same compilation unit or namespace body. 41

16.4.1 Using alias directives 42

A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the 43 immediately enclosing compilation unit or namespace body. 44

Page 266: C# Language Specification

C# LANGUAGE SPECIFICATION

246

using-alias-directive: 1 using identifier = namespace-or-type-name ; 2

Within global attributes and member declarations in a compilation unit or namespace body that contains a 3 using-alias-directive, the identifier introduced by the using-alias-directive can be used to reference the given 4 namespace or type. [Example: 5

namespace N1.N2 6 { 7 class A {} 8 } 9

namespace N3 10 { 11 using A = N1.N2.A; 12

class B: A {} 13 } 14

Above, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B 15 derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then 16 referencing R.A: 17

namespace N3 18 { 19 using R = N1.N2; 20

class B: R.A {} 21 } 22

end example] 23

Within using directives, global attributes and member declarations in a compilation unit or namespace body 24 that contains an extern-alias-directive, the identifier introduced by the extern-alias-directive can be used to 25 reference the associated namespace. [Example: For example: 26

namespace N1 27 { 28 extern alias N2; 29

class B: N2::A {} 30 } 31

Above, within member declarations in the N1 namespace, N2 is an alias for some namespace whose 32 definition is external to the source code of the program. Class N1.B derives from class N2.A. The same 33 effect can be obtained by creating an alias A for N2.A and then referencing A: 34

namespace N1 35 { 36 extern alias N2; 37 using A = N2::A; 38

class B: A {} 39 } 40

end example] 41

An extern-alias-directive or using-alias-directive makes an alias available within a particular compilation 42 unit or namespace body, but it does not contribute any new members to the underlying declaration space. In 43 other words, an alias directive is not transitive, but, rather, affects only the compilation unit or namespace 44 body in which it occurs. [Example: In the following code 45

namespace N3 46 { 47 extern alias R1; 48 using R2 = N1.N2; 49 } 50

Page 267: C# Language Specification

Chapter 16 Namespaces

247

namespace N3 1 { 2 class B: R1::A, R2.I {} // Error, R1 and R2 unknown 3 } 4

the scopes of the alias directives that introduce R1 and R2 only extend to member declarations in the 5 namespace body in which they are contained, so R1 and R2 are unknown in the second namespace 6 declaration. However, placing the alias directives in the containing compilation unit causes the alias to 7 become available within both namespace declarations: 8

extern alias R1; 9 using R2 = N1.N2; 10

namespace N3 11 { 12 class B: R1::A, R2.I {} 13 } 14

namespace N3 15 { 16 class C: R1::A, R2.I {} 17 } 18

end example] 19

Each extern-alias-directive or using-alias-directive in a compilation-unit or namespace-body contributes a 20 name to the alias declaration space (§10.3) of the immediately enclosing compilation-unit or namespace-21 body. The identifier of the alias directive shall be unique within the corresponding alias declaration space. 22 The alias identifier need not be unique within the global declaration space or the declaration space of the 23 corresponding namespace. [Example: 24

extern alias A; 25 extern alias B; 26

using A = N1.N2; // Error: alias A already exists 27

class B {} // Ok 28

The using alias named A causes an error since there is already an alias named A in the same compilation unit. 29 The class named B does not conflict with the extern alias named B since these names are added to distinct 30 declaration spaces. The former is added to the global declaration space and the latter is added to the alias 31 declaration space for this compilation unit. 32

When an alias name matches the name of a member of a namespace, usage of either must be appropriately 33 qualified: 34

namespace N1.N2 35 { 36 class B {} 37 } 38

namespace N3 39 { 40 class A {} 41 class B : A {} 42 } 43

namespace N3 44 { 45 using A = N1.N2; 46 using B = N1.N2.B; 47

class W : B {} // Error: B is ambiguous 48 class X : A.B {} // Error: A is ambiguous 49 class Y : A::B {} // Ok: uses N1.N2.B 50 class Z : N3.B {} // Ok: uses N3.B 51 } 52

In the second namespace body for N3, unqualified use of B results in an error, since N3 contains a member 53 named B and the namespace body also declares an alias with name B. Similarly for A. The class N3.B can be 54 referenced as N3.B or global::N3.B. The alias A can be used in a qualified-alias-member (§16.7), such as 55

Page 268: C# Language Specification

C# LANGUAGE SPECIFICATION

248

A::B. The alias B is essentially useless. It cannot be used in a qualified-alias-member since only namespace 1 aliases can be used in a qualified-alias-member and B aliases a type. end example] 2

Just like regular members, names introduced by alias directives are hidden by similarly named members in 3 nested scopes. [Example: In the following code 4

using R = N1.N2; 5

namespace N3 6 { 7 class R {} 8

class B: R.A {} // Error, R has no member A 9 } 10

the reference to R.A in the declaration of B causes a compile-time error because R refers to N3.R, not 11 N1.N2. end example] 12

The order in which extern-alias-directives are written has no significance. Likewise, the order in which 13 using-alias-directives are written has no significance, but all using-alias-directives must come after all 14 extern-alias-directives in the same compilation unit or namespace body. Resolution of the namespace-or-15 type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other 16 using-directives in the immediately containing compilation unit or namespace body, but may be affected by 17 extern-alias-directives in the immediately containing compilation unit or namespace body. In other words, 18 the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing 19 compilation unit or namespace body had no using-directives but has the correct set of extern-alias-20 directives. [Example: In the following code 21

namespace N1.N2 {} 22

namespace N3 23 { 24 extern alias E; 25

using R1 = E::N; // OK 26

using R2 = N1; // OK 27

using R3 = N1.N2; // OK 28

using R4 = R2.N2; // Error, R2 unknown 29 } 30

the last using-alias-directive results in a compile-time error because it is not affected by the previous using-31 alias-directive. The first using-alias-directive does not result in an error since the scope of the extern alias E 32 includes the using-alias-directive. end example] 33

A using-alias-directive can create an alias for any namespace or type, including the namespace within which 34 it appears and any namespace or type nested within that namespace. 35

Accessing a namespace or type through an alias yields exactly the same result as accessing that namespace 36 or type through its declared name. [Example: Given 37

namespace N1.N2 38 { 39 class A {} 40 } 41

namespace N3 42 { 43 using R1 = N1; 44 using R2 = N1.N2; 45

class B 46 { 47 N1.N2.A a; // refers to N1.N2.A 48 R1.N2.A b; // refers to N1.N2.A 49 R2.A c; // refers to N1.N2.A 50 } 51 } 52

Page 269: C# Language Specification

Chapter 16 Namespaces

249

the names N1.N2.A, R1.N2.A, and R2.A are equivalent and all refer to the class whose fully qualified 1 name is N1.N2.A. end example] 2

Although each part of a partial type (§17.1.4) is declared within the same namespace, the parts are typically 3 written within different namespace declarations. Thus, different extern alias directives and using 4 directives can be present for each part. When interpreting simple names (§14.5.2) within one part, only the 5 extern alias directives and using directives of the namespace bodies and compilation unit enclosing that 6 part are considered. This may result in the same identifier having different meanings in different parts. 7 [Example: 8

namespace N 9 { 10 using List = System.Collections.ArrayList; 11

partial class A 12 { 13 List x; // x has type System.Collections.ArrayList 14 } 15 } 16

namespace N 17 { 18 using List = Widgets.LinkedList; 19

partial class A 20 { 21 List y; // y has type Widgets.LinkedList 22 } 23 } 24

end example] 25

16.4.2 Using namespace directives 26

A using-namespace-directive imports the types contained in a namespace into the immediately enclosing 27 compilation unit or namespace body, enabling the identifier of each type to be used without qualification. 28

using-namespace-directive: 29 using namespace-name ; 30

Within member declarations in a compilation unit or namespace body that contains a using-namespace-31 directive, the types contained in the given namespace can be referenced directly. [Example: 32

namespace N1.N2 33 { 34 class A {} 35 } 36

namespace N3 37 { 38 using N1.N2; 39

class B: A {} 40 } 41

Above, within member declarations in the N3 namespace, the type members of N1.N2 are directly 42 available, and thus class N3.B derives from class N1.N2.A. end example] 43

A using-namespace-directive imports the types contained in the given namespace, but specifically does not 44 import nested namespaces. [Example: In the following code 45

namespace N1.N2 46 { 47 class A {} 48 } 49

namespace N3 50 { 51 using N1; 52

Page 270: C# Language Specification

C# LANGUAGE SPECIFICATION

250

class B: N2.A {} // Error, N2 unknown 1 } 2

the using-namespace-directive imports the types contained in N1, but not the namespaces nested in N1. Thus, 3 the reference to N2.A in the declaration of B results in a compile-time error because no members named N2 4 are in scope. end example] 5

Unlike a using-alias-directive, a using-namespace-directive can import types whose identifiers are already 6 defined within the enclosing compilation unit or namespace body. In effect, names imported by a using-7 namespace-directive are hidden by similarly named members in the enclosing compilation unit or 8 namespace body. [Example: 9

namespace N1.N2 10 { 11 class A {} 12

class B {} 13 } 14

namespace N3 15 { 16 using N1.N2; 17

class A {} 18 } 19

Here, within member declarations in the N3 namespace, A refers to N3.A rather than N1.N2.A. end example] 20

When more than one namespace imported by using-namespace-directives in the same compilation unit or 21 namespace body contain types by the same name, references to that name are considered ambiguous. 22 [Example: In the following code 23

namespace N1 24 { 25 class A {} 26 } 27

namespace N2 28 { 29 class A {} 30 } 31

namespace N3 32 { 33 using N1; 34

using N2; 35

class B: A {} // Error, A is ambiguous 36 } 37

both N1 and N2 contain a member A, and because N3 imports both, referencing A in N3 is a compile-time 38 error. In this situation, the conflict can be resolved either through qualification of references to A, or by 39 introducing a using-alias-directive that picks a particular A. For example: 40

namespace N3 41 { 42 using N1; 43

using N2; 44

using A = N1.A; 45

class B: A {} // A means N1.A 46 } 47

end example] 48

Like a using-alias-directive, a using-namespace-directive does not contribute any new members to the 49 underlying declaration space of the namespace, but, rather, affects only the compilation unit or namespace 50 body in which it appears. 51

Page 271: C# Language Specification

Chapter 16 Namespaces

251

The namespace-name referenced by a using-namespace-directive is resolved in the same way as the 1 namespace-or-type-name referenced by a using-alias-directive. Thus, using-namespace-directives in the 2 same compilation unit or namespace body do not affect each other and can be written in any order. 3

16.5 Namespace members 4

A namespace-member-declaration is either a namespace-declaration (§16.2) or a type-declaration (§16.6). 5

namespace-member-declarations: 6 namespace-member-declaration 7 namespace-member-declarations namespace-member-declaration 8

namespace-member-declaration: 9 namespace-declaration 10 type-declaration 11

A compilation unit or a namespace body can contain namespace-member-declarations, and such 12 declarations contribute new members to the underlying declaration space of the containing compilation unit 13 or namespace body. 14

16.6 Type declarations 15

A type-declaration is a class-declaration (§17.1), a struct-declaration (§18.1), an interface-declaration 16 (§20.1), an enum-declaration (§21.1), or a delegate-declaration (§22.1). 17

type-declaration: 18 class-declaration 19 struct-declaration 20 interface-declaration 21 enum-declaration 22 delegate-declaration 23

A type-declaration can occur as a top-level declaration in a compilation unit or as a member declaration 24 within a namespace, class, or struct. 25

When a type declaration for a type T occurs as a top-level declaration in a compilation unit, the fully 26 qualified name (§10.8.2) of the type declaration is the same as the unqualified name of the declaration 27 (§10.8.1). When a type declaration for a type T occurs within a namespace, class, or struct, the fully 28 qualified name (§10.8.2) of the type declaration is S.N, where S is the fully qualified name of the containing 29 namespace or type declaration, and N is the unqualified name of the declaration (§10.8.1). 30

A type declared within a class or struct is called a nested type (§17.2.6). 31

The permitted access modifiers and the default access for a type declaration depend on the context in which 32 the declaration takes place (§10.5.1): 33

• Types declared in compilation units or namespace declarations can have public or internal access. 34 The default is internal access. 35

• Types declared in classes can have public, protected internal, protected, internal, or 36 private access. The default is private access. 37

• Types declared in structs can have public, internal, or private access. The default is private 38 access. 39

16.7 Qualified alias member 40

A qualified-alias-member provides explicit access to the global namespace and to extern or using aliases 41 that are potentially hidden by other entities. 42

qualified-alias-member: 43 identifier :: identifier type-argument-listopt 44

Page 272: C# Language Specification

C# LANGUAGE SPECIFICATION

252

A qualified-alias-member can be used as a namespace-or-type-name (§10.8) or as the left operand in a 1 member-access (§14.5.4). 2

A qualified-alias-member consists of two identifiers, referred to as the left-hand and right-hand identifiers, 3 seperated by the :: token and optionally followed by a type-argument-list. When the left-hand identifier is 4 global then the global namespace is searched for the right-hand identifier. For any other left-hand 5 identifier, that identifier is looked up as an extern or using alias (§16.3 and §16.4.1). A compile-time error 6 occurs if there is no such alias or the alias references a type. If the alias references a namespace then that 7 namespace is searched for the right-hand identifier. 8

9

A qualified-alias-member has one of two forms: 10

• A::B<G1, ..., GN>, where A and B represent identifiers, and <G1, ..., GN> is a type argument list. (N is 11 always at least one.) 12

• A::B, where A and B represent identifiers. (In this case, N is considered to be zero.) 13

Using this notation, the meaning of a qualified-alias-member is determined as follows: 14

• If A is the identifier global, then the global namespace is searched for B: 15

o If the global namespace contains a namespace named B and N is zero, then the qualified-alias-16 member refers to that namespace. 17

o Otherwise, if the global namespace contains a non-generic type named B and N is zero, then the 18 qualified-alias-member refers to that type. 19

o Otherwise, if the global namespace contains a type named B that has N type parameters, then the 20 qualified-alias-member refers to that type constructed with the given type arguments. 21

o Otherwise, the qualified-alias-member is undefined and a compile-time error occurs. 22

• Otherwise, starting with the namespace declaration (§16.2) immediately containing the qualified-alias-23 member (if any), continuing with each enclosing namespace declaration (if any), and ending with the 24 compilation unit containing the qualified-alias-member, the following steps are evaluated until an entity 25 is located: 26

o If the namespace declaration or compilation unit contains a using-alias-directive that associates A 27 with a type, then the qualified-alias-member is undefined and a compile-time error occurs. 28

o Otherwise, if the namespace declaration or compilation unit contains an extern-alias-directive or 29 using-alias-directive that associates A with a namespace, then: 30

• If the namespace associated with A contains a namespace named B and N is zero, then the 31 qualified-alias-member refers to that namespace. 32

• Otherwise, if the namespace associated with A contains a non-generic type named B and N is 33 zero, then the qualified-alias-member refers to that type. 34

• Otherwise, if the namespace associated with A contains a type named B that has N type 35 parameters, then the qualified-alias-member refers to that type constructed with the given type 36 arguments. 37

• Otherwise, the qualified-alias-member is undefined and a compile-time error occurs. 38

• Otherwise, the qualified-alias-member is undefined and a compile-time error occurs. 39

[Example: In the code: 40

using S = System.Net.Sockets; 41

class A { 42 public static int x; 43 } 44

Page 273: C# Language Specification

Chapter 16 Namespaces

253

class C { 1 public void F(int A, object S) { 2 // Use global::A.x instead of A.x 3 global::A.x += A; 4

// Use S::Socket instead of S.Socket 5 S::Socket s = S as S::Socket; 6 } 7 } 8

the class A is referenced with global::A and the type System.Net.Sockets.Socket is referenced with 9 S::Socket. Using A.x and S.Socket instead would have caused compile-time errors because A and S 10 would have resolved to the parameters. end example] 11

[Note: The identifier global has special meaning only when used as the left-hand identifier of a qualified-12 alias-name. It is not a keyword and it is not itself an alias. In the code: 13

class A { } 14

class C { 15 global.A x; // Error: global is not defined 16 global::A y; // Valid: References A in the global namespace 17 } 18

using global.A causes a compile-time error since there is no entity named global in scope. If some entity 19 named global were in scope, then global in global.A would have resolved to that entity. 20

Using global as the left-hand identifier of a qualified-alias-member always causes a lookup in the global 21 namespace, even if there is a using alias named global. [Example: In the code: 22

using global = MyGlobalTypes; 23

class A { } 24

class C { 25 global.A x; // Valid: References MyGlobalTypes.A 26 global::A y; // Valid: References A in the global namespace 27 } 28

global.A resolves to MyGlobalTypes.A and global::A resolves to class A in the global namespace. 29 end note] 30

Page 274: C# Language Specification
Page 275: C# Language Specification

Chapter 17 Classes

255

17. Classes 1

A class is a data structure that can contain data members (constants and fields), function members (methods, 2 properties, events, indexers, operators, instance constructors, destructors, and static constructors), and nested 3 types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a 4 base class. 5

17.1 Class declarations 6

A class-declaration is a type-declaration (§16.6) that declares a new class. 7

class-declaration: 8 attributesopt class-modifiersopt partialopt class identifier type-parameter-listopt 9 class-baseopt type-parameter-constraints-clausesopt class-body ;opt 10

A class-declaration consists of an optional set of attributes (§24), followed by an optional set of class-11 modifiers (§17.1.1), followed by an optional partial modifier (§17.1.4), followed by the keyword class 12 and an identifier that names the class, followed by an optional type-parameter-list (§26.1.1), followed by an 13 optional class-base specification (§17.1.1.3), followed by an optional type-parameter-constraints-clauses 14 (§26.7), followed by a class-body (§17.1.3), optionally followed by a semicolon. 15

A class declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-16 parameter-list. 17

A class declaration that supplies a type-parameter-list is a generic class declaration (§26.1). 18

17.1.1 Class modifiers 19

A class-declaration can optionally include a sequence of class modifiers: 20

class-modifiers: 21 class-modifier 22 class-modifiers class-modifier 23

class-modifier: 24 new 25 public 26 protected 27 internal 28 private 29 abstract 30 sealed 31 static 32

It is a compile-time error for the same modifier to appear multiple times in a class declaration. 33

The new modifier is permitted on nested classes. It specifies that the class hides an inherited member by the 34 same name, as described in §17.2.2. It is a compile-time error for the new modifier to appear on a class 35 declaration that is not a nested class declaration. 36

The public, protected, internal, and private modifiers control the accessibility of the class. 37 Depending on the context in which the class declaration occurs, some of these modifiers might not be 38 permitted (§10.5.1). 39

When a partial type declaration (§17.1.4) includes an accessibility specification (via the public, 40 protected, internal, and private modifiers), that specification shall agree with all other parts that 41

Page 276: C# Language Specification

C# LANGUAGE SPECIFICATION

256

include an accessibility specification. If no part of a partial type includes an accessibility specification, the 1 type is given the appropriate default accessibility (§10.5.1). 2

The abstract, sealed, and static modifiers are discussed in the following subclauses. 3

17.1.1.1 Abstract classes 4

The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only 5 as a base class. An abstract class differs from a non-abstract class in the following ways: 6

• An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on 7 an abstract class. While it is possible to have variables and values whose compile-time types are 8 abstract, such variables and values will necessarily either be null or contain references to instances of 9 non-abstract classes derived from the abstract types. 10

• An abstract class is permitted (but not required) to contain abstract members. 11

• An abstract class cannot be sealed. 12

When a non-abstract class is derived from an abstract class, the non-abstract class shall include actual 13 implementations of all inherited abstract members, thereby overriding those abstract members. [Example: In 14 the following code 15

abstract class A 16 { 17 public abstract void F(); 18 } 19

abstract class B: A 20 { 21 public void G() {} 22 } 23

class C: B 24 { 25 public override void F() { 26 // actual implementation of F 27 } 28 } 29

the abstract class A introduces an abstract method F. Class B introduces an additional method G, but since it 30 doesn’t provide an implementation of F, B shall also be declared abstract. Class C overrides F and provides 31 an actual implementation. Since there are no abstract members in C, C is permitted (but not required) to be 32 non-abstract. end example] 33

If one or more parts of a partial type declaration (§17.1.4) of a class include the abstract modifier, the 34 class is abstract. Otherwise, the class is non-abstract. 35

17.1.1.2 Sealed classes 36

The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed 37 class is specified as the base class of another class. 38

A sealed class cannot also be an abstract class. 39

[Note: The sealed modifier is primarily used to prevent unintended derivation, but it also enables certain 40 run-time optimizations. In particular, because a sealed class is known to never have any derived classes, it is 41 possible to transform virtual function member invocations on sealed class instances into non-virtual 42 invocations. end note] 43

If one or more parts of a partial type declaration (§17.1.4) of a class include the sealed modifier, the class 44 is sealed. Otherwise, the class is unsealed. 45

17.1.1.3 Static classes 46

When a class declaration includes a static modifier, the class being declared is said to be a static class. 47

Page 277: C# Language Specification

Chapter 17 Classes

257

A static class declaration is subject to the following restrictions: 1

• A static class shall not include a sealed or abstract modifier. (However, since a static class cannot 2 be instantiated or derived from, it behaves as if it were both sealed and abstract.) 3

• A static class shall not include a class-base specification (§17.1.2) and cannot explicitly specify a base 4 class or a list of implemented interfaces. A static class implicitly inherits from type object. 5

• A static class shall not contain any operators. 6

• A static class shall not have members with protected or protected internal declared 7 accessibility. 8

• A static class shall only contain static members (§17.2.5). [Note: constants and nested types are 9 classified as static members. end note] 10

[Example: 11

using System.Diagnostics; 12 public static class Precondition 13 { 14 public sealed class Exception: System.ApplicationException 15 { 16

public Exception(string message) : base(message) {} 17 } 18

[Conditional("CHECK_PRECONDITIONS")] 19 public static void Required(bool condition, string description) { 20 if (!condition) { 21 throw new Exception(description); 22 } 23 } 24 } 25

end example] 26

A static class has no instance constructors. It is not possible to declare an instance constructor in a static 27 class, and no default instance constructor (§17.10.4) is provided for a static class. 28

The members of a static class are not implicitly made static; except for constants and nested types, member 29 declarations that are intended to be static shall explicitly include a static modifier. When a class is nested 30 within a static outer class, the nested class is not a static class unless it explicitly includes a static 31 modifier. 32

A namespace-or-type-name (§10.8) is permitted to reference a static class if 33

• The namespace-or-type-name is the T in a namespace-or-type-name of the form T.I, or 34

• The namespace-or-type-name is the T in a typeof-expression (§14.5.11) of the form typeof(T). 35

A primary-expression (§14.5) is permitted to reference a static class if 36

• The primary-expression is the E in a member-access (§14.5.4) of the form E.I. 37

In any other context it is a compile-time error to reference a static class. [Note: For example, it is an error for 38 a static class to be used as a base class, a constituent type (§17.2.4) of a member, a generic type argument, or 39 a type parameter constraint. Likewise, a static class cannot be used in an array type, a pointer type, a new 40 expression, a cast expression, an is expression, an as expression, a sizeof expression, or a default value 41 expression. end note] 42

If one or more parts of a partial type declaration (§17.1.4) of a class include the static modifier, the class 43 is static. Otherwise, the class is not static. 44

17.1.2 Class base specification 45

A class declaration can include a class-base specification, which defines the direct base class of the class 46 and the interfaces (§20) implemented by the class. 47

Page 278: C# Language Specification

C# LANGUAGE SPECIFICATION

258

class-base: 1 : class-type 2 : interface-type-list 3 : class-type , interface-type-list 4

interface-type-list: 5 interface-type 6 interface-type-list , interface-type 7

17.1.2.1 Base classes 8

When a class-type is included in the class-base, it specifies the direct base class of the class being declared. 9 If a non-partial class declaration has no class-base, or if the class-base lists only interface types, the direct 10 base class is object. When a partial class declaration includes a base class specification, that base class 11 specification shall reference the same type as all other parts of that partial type that include a base class 12 specification. If no part of a partial class includes a base class specification, the base class is object. A 13 class inherits members from its direct base class, as described in §17.2.1. 14

[Example: In the following code 15

class A {} 16

class B: A {} 17

class A is said to be the direct base class of B, and B is said to be derived from A. Since A does not explicitly 18 specify a direct base class, its direct base class is implicitly object. end example] 19

The base class specified in a class declaration can be a constructed class type (§26.5). A base class cannot be 20 a type parameter on its own, though it can involve the type parameters that are in scope. [Example: 21

class Extend<V>: V {} // Error, type parameter used as base class 22

class C<V>: Base<V[]> {} // Ok 23

end example] 24

The direct base class (and any type arguments) of a class type shall be at least as accessible as the class type 25 itself (§10.5.4). For example, it is a compile-time error for a public class to derive from a private or 26 internal class. 27

The direct base class of a class type shall not be any of the following types: System.Array, 28 System.Delegate, System.Enum, or System.ValueType. 29

The base classes of a class are the direct base class and its base classes (after substituting type arguments for 30 type parameters in constructed generic types). In other words, the set of base classes is the transitive closure 31 of the direct base class relationship. [Note: Referring to the example above, the base classes of B are A and 32 object. end note] 33

Except for class object, every class has exactly one direct base class. The object class has no direct base 34 class and is the ultimate base class of all other classes. 35

When a class B derives from a class A, it is a compile-time error for A to depend on B. A class directly 36 depends on its direct base class (if any) and directly depends on the class within which it is immediately 37 nested (if any). Given this definition, the complete set of classes upon which a class depends is the transitive 38 closure of the directly depends on relationship. 39

[Example: The example 40

class A: B {} 41

class B: C {} 42

class C: A {} 43

is in error because the classes circularly depend on themselves. Likewise, the example 44

class A: B.C {} 45

Page 279: C# Language Specification

Chapter 17 Classes

259

class B: A 1 { 2 public class C {} 3 } 4

results in a compile-time error because A depends on B.C (its direct base class), which depends on B (its 5 immediately enclosing class), which circularly depends on A. end example] 6

A class does not depend on the classes that are nested within it. [Example: In the following code 7

class A 8 { 9 class B: A {} 10 } 11

B depends on A (because A is both its direct base class and its immediately enclosing class), but A does not 12 depend on B (since B is neither a base class nor an enclosing class of A). Thus, the example is valid. end 13 example] 14

It is not possible to derive from a sealed class. [Example: In the following code 15

sealed class A {} 16

class B: A {} // Error, cannot derive from a sealed class 17

class B results in a compile-time error because it attempts to derive from the sealed class A. end example] 18

17.1.2.2 Interface implementations 19

A class-base specification can include a list of interface types, in which case the class is said to implement 20 the given interface types. 21

The set of interfaces for a type declared in multiple parts (§17.1.4) is the union of the interfaces specified on 22 each part. A particular interface can only be named once on each part, but multiple parts can name the same 23 base interface(s). There shall only be one implementation of each member of any given interface. [Example: 24 In the following: 25

partial class C: IA, IB {...} 26

partial class C: IC {...} 27

partial class C: IA, IB {...} 28

the set of base interfaces for class C is IA, IB, and IC. end example] 29

Typically, each part provides an implementation of the interface(s) declared on that part; however, this is not 30 a requirement. A part can provide the implementation for an interface declared on a different part. [Example: 31

partial class X 32 { 33 int IComparable.CompareTo(object o) {...} 34 } 35

partial class X: IComparable 36 { 37 ... 38 } 39

end example] 40

The base interfaces specified in a class declaration can be constructed interface types (§26.5). A base 41 interface cannot be a type parameter on its own, though it can involve the type parameters that are in scope. 42 [Example: The following code illustrates how a class can implement and extend constructed types: 43

class C<U, V> {} 44

interface I1<V> {} 45

class D: C<string, int>, I1<string> {} 46

class E<T>: C<int, T>, I1<T> {} 47

end example] 48

Page 280: C# Language Specification

C# LANGUAGE SPECIFICATION

260

Interface implementations are discussed further in §20.4. 1

17.1.3 Class body 2

The class-body of a class defines the members of that class. 3

class-body: 4 { class-member-declarationsopt } 5

17.1.4 Partial declarations 6

The modifier partial is used when defining a class, struct, or interface type in multiple parts. Like get 7 and set in property accessors, partial is not a keyword. partial shall appear immediately before one of 8 the keywords class, struct, or interface. 9

Each part of a partial type declaration shall include a partial modifier and shall be declared in the same 10 namespace or containing type as the other parts. The partial modifier indicates that additional parts of the 11 type declaration might exist elsewhere, but the existence of such additional parts is not a requirement; it is 12 valid for the only declaration of a type to include the partial modifier. 13

All parts of a partial type shall be compiled together such that the parts can be merged at compile-time. 14 Partial types specifically do not allow already compiled types to be extended. 15

Nested types can be declared in multiple parts by using the partial modifier. Typically, the containing 16 type is declared using partial as well, and each part of the nested type is declared in a different part of the 17 containing type. 18

[Example: The following partial class is implemented in two parts, which reside in different source files. The 19 first part is machine generated by a database mapping tool while the second part is manually authored: 20

public partial class Customer 21 { 22 private int id; 23 private string name; 24 private string address; 25 private List<Order> orders; 26

public Customer() { 27 ... 28 } 29 } 30

public partial class Customer 31 { 32 public void SubmitOrder(Order orderSubmitted) { 33 orders.Add(orderSubmitted); 34 } 35

public bool HasOutstandingOrders() { 36 return orders.Count > 0; 37 } 38 } 39

When the two parts above are compiled together, the resulting code behaves as if the class had been written 40 as a single unit, as follows: 41

public class Customer 42 { 43 private int id; 44 private string name; 45 private string address; 46 private List<Order> orders; 47

public Customer() { 48 ... 49 } 50

Page 281: C# Language Specification

Chapter 17 Classes

261

public void SubmitOrder(Order orderSubmitted) { 1 orders.Add(orderSubmitted); 2 } 3

public bool HasOutstandingOrders() { 4 return orders.Count > 0; 5 } 6 } 7

end example] 8

The handling of attributes specified on the type or type parameters of different parts of a partial declaration 9 is discussed in §24.2 10

17.2 Class members 11

The members of a class consist of the members introduced by its class-member-declarations and the 12 members inherited from the direct base class. 13

class-member-declarations: 14 class-member-declaration 15 class-member-declarations class-member-declaration 16

class-member-declaration: 17 constant-declaration 18 field-declaration 19 method-declaration 20 property-declaration 21 event-declaration 22 indexer-declaration 23 operator-declaration 24 constructor-declaration 25 destructor-declaration 26 static-constructor-declaration 27 type-declaration 28

The members of a class are divided into the following categories: 29

• Constants, which represent constant values associated with that class (§17.3). 30

• Fields, which are the variables of that class (§17.4). 31

• Methods, both non-generic and generic, which implement the computations and actions that can be 32 performed by that class (§17.5, §26.6). 33

• Properties, which define named characteristics and the actions associated with reading and writing those 34 characteristics (§17.6). 35

• Events, which define notifications that can be generated by that class (§17.7). 36

• Indexers, which permit instances of that class to be indexed in the same way as arrays (§17.8). 37

• Operators, which define the expression operators that can be applied to instances of that class (§17.9). 38

• Instance constructors, which implement the actions required to initialize instances of that class (§17.10) 39

• Destructors, which implement the actions to be performed before instances of that class are permanently 40 discarded (§17.12). 41

• Static constructors, which implement the actions required to initialize that class itself (§17.11). 42

• Types, which represent the types that are local to that class (§16.6). 43

Members that can contain executable code are collectively known as the function members of the class. The 44 function members of a class are the methods, properties, events, indexers, operators, instance constructors, 45 destructors, and static constructors of that class. 46

Page 282: C# Language Specification

C# LANGUAGE SPECIFICATION

262

A class-declaration creates a new declaration space (§10.3), and the type-parameters and the class-member-1 declarations immediately contained by the class-declaration introduce new members into this declaration 2 space. The following rules apply to class-member-declarations: 3

• Instance constructors, destructors, and static constructors shall have the same name as the immediately 4 enclosing class. All other members shall have names that differ from the name of the immediately 5 enclosing class. 6

• The name of a type parameter in the type-parameter-list of a class declaration shall differ from the 7 names of all other type parameters in the same type-parameter-list and shall differ from the name of the 8 class and the names of all members of the class. 9

• The name of a type must differ from the names of all non-type members declared in the same class. If 10 two or more type declarations share the same fully qualified name, the declarations must have the partial 11 modifier (§17.1.4) and these declarations combine to define a single type. [Note: Since the fully 12 qualified name of a type declaration encodes the number of type parameters, two distinct types may 13 share the same name as long as they have different number of type parameters. end note] 14

• The name of a constant, field, property, or event shall differ from the names of all other members 15 declared in the same class. 16

• The name of a method shall differ from the names of all other non-methods declared in the same class. 17 In addition, the signature (§10.6) of a method shall differ from the signatures of all other methods 18 declared in the same class, and two methods declared in the same class shall not have signatures that 19 differ solely by ref and out. 20

• The signature of an instance constructor shall differ from the signatures of all other instance constructors 21 declared in the same class, and two constructors declared in the same class shall not have signatures that 22 differ solely by ref and out. 23

• The signature of an indexer shall differ from the signatures of all other indexers declared in the same 24 class. 25

• The signature of an operator shall differ from the signatures of all other operators declared in the same 26 class. 27

The inherited members of a class (§17.2.1) are not part of the declaration space of a class. [Note: Thus, a 28 derived class is allowed to declare a member with the same name or signature as an inherited member 29 (which in effect hides the inherited member). end note] 30

The set of members of a type declared in multiple parts (§17.1.4) is the union of the members declared in 31 each part. The bodies of all parts of the type declaration share the same declaration space (§10.3), and the 32 scope of each member (§10.7) extends to the bodies of all the parts. The accessibility domain of any member 33 always includes all the parts of the enclosing type; a private member declared in one part is freely 34 accessible from another part. It is a compile-time error to declare the same member in more than one part of 35 the type, unless that member is a type having the partial modifier. [Example: 36

partial class A 37 { 38 int x; // Error, cannot declare x more than once 39

partial class Inner // Ok, Inner is a partial type 40 { 41 int y; 42 } 43 } 44

partial class A 45 { 46 int x; // Error, cannot declare x more than once 47

Page 283: C# Language Specification

Chapter 17 Classes

263

partial class Inner // Ok, Inner is a partial type 1 { 2 int z; 3 } 4 } 5

end example] 6

The ordering of members within a type declared in multiple parts is undefined. [Note: Although the ordering 7 of members within a type is not significant to C# code, it may be significant when interfacing with other 8 languages and environments. end note] 9

17.2.1 Inheritance 10

A class inherits the members of its direct base class. Inheritance means that a class implicitly contains all 11 members of its direct base class, except for the instance constructors, destructors, and static constructors of 12 the base class. Some important aspects of inheritance are: 13

• Inheritance is transitive. If C is derived from B, and B is derived from A, then C inherits the members 14 declared in A as well as the members declared in B. 15

• A derived class extends its direct base class. A derived class can add new members to those it inherits, 16 but it cannot remove the definition of an inherited member. 17

• Instance constructors, destructors, and static constructors are not inherited, but all other members are, 18 regardless of their declared accessibility (§10.5). However, depending on their declared accessibility, 19 inherited members might not be accessible in a derived class. 20

• A derived class can hide (§10.7.1.2) inherited members by declaring new members with the same name 21 or signature. However, hiding an inherited member does not remove that member—it merely means that 22 member will not be found by member lookup within the derived class. 23

• An instance of a class contains a set of all instance fields declared in the class and its base classes, and 24 an implicit conversion (§13.1.4) exists from a derived class type to any of its base class types. Thus, a 25 reference to an instance of some derived class can be treated as a reference to an instance of any of its 26 base classes. 27

• A class can declare virtual methods, properties, indexers, and events, and derived classes can override 28 the implementation of these function members. This enables classes to exhibit polymorphic behavior 29 wherein the actions performed by a function member invocation varies depending on the run-time type 30 of the instance through which that function member is invoked. 31

• Members inherited from a constructed generic type are inherited after type substitution. That is, any 32 constituent types in the member have the base class declaration’s type parameters replaced with the 33 corresponding type arguments used in the class-base specification. 34

[Example: In the example: 35

class A<S> 36 { 37 public S field; 38 } 39

class B<T> : A<T[]> 40 { 41 // inherited: public T[] x; 42 public A<T> Meth(T t) { … } 43 } 44

class C : B<string> 45 { 46 // inherited: public string[] x; 47 // inherited: public A<string> Meth(string t); 48 } 49

Page 284: C# Language Specification

C# LANGUAGE SPECIFICATION

264

class B<T> inherits member field from A<S> after type parameter S is replaced with the type argument 1 T[]. Similarly class C inherits the members of B<T> (including the inherited member field) after type 2 parameter T is replaced with the type argument string. end example] 3

17.2.2 The new modifier 4

A class-member-declaration is permitted to declare a member with the same name or signature as an 5 inherited member. When this occurs, the derived class member is said to hide the base class member. See 6 §10.7.1.2 for a precise specification of when a member hides an inherited member. 7

An inherited member M is considered to be available if M is accessible and there is no other inherited 8 accessible member N that already hides M. 9

Hiding an available inherited member is not considered an error, but it does cause the compiler to issue a 10 warning. To suppress the warning, the declaration of the derived class member can include a new modifier 11 to indicate that the derived member is intended to hide the base member. If one or more parts of a partial 12 declaration §17.1.4) of a nested type include the new modifier, no warning is issued if the nested type hides 13 an available inherited member. 14

If a new modifier is included in a declaration that doesn’t hide an available inherited member, a warning to 15 that effect is issued. 16

17.2.3 Access modifiers 17

A class-member-declaration can have any one of the five possible kinds of declared accessibility (§10.5.1): 18 public, protected internal, protected, internal, or private. Except for the protected 19 internal combination, it is a compile-time error to specify more than one access modifier. When a class-20 member-declaration does not include any access modifiers, private is assumed. 21

17.2.4 Constituent types 22

Types that are used in the declaration of a member are called the constituent types of that member. Possible 23 constituent types are the type of a constant, field, property, event, or indexer, the return type of a method or 24 operator, and the parameter types of a method, indexer, operator, or instance constructor. The constituent 25 types of a member shall be at least as accessible as that member itself (§10.5.4). 26

17.2.5 Static and instance members 27

Members of a class are either static members or instance members. [Note: Generally speaking, it is useful 28 to think of static members as belonging to classes and instance members as belonging to objects (instances 29 of classes). end note] 30

When a field, method, property, event, operator, or constructor declaration includes a static modifier, it 31 declares a static member. In addition, a constant or type declaration implicitly declares a static member. 32 Static members have the following characteristics: 33

• When a static member is referenced in a member-access (§14.5.4) of the form E.M, E shall denote a type 34 that has a member M. It is a compile-time error for E to denote an instance. 35

• A static field in a non-generic class identifies exactly one storage location. No matter how many 36 instances of a non-generic class are created, there is only ever one copy of a static field. Each distinct 37 closed constructed type (§26.5.2) has its own set of static fields, regardless of the number of instances of 38 the closed constructed type. 39

• A static function member (method, property, event, operator, or constructor) does not operate on a 40 specific instance, and it is a compile-time error to refer to this in such a function member. 41

When a field, method, property, event, indexer, constructor, or destructor declaration does not include a 42 static modifier, it declares an instance member. (An instance member is sometimes called a non-static 43 member.) Instance members have the following characteristics: 44

Page 285: C# Language Specification

Chapter 17 Classes

265

• When an instance member is referenced in a member-access (§14.5.4) of the form E.M, E shall denote an 1 instance of a type that has a member M. It is a compile-time error for E to denote a type. 2

• Every instance of a class contains a separate set of all instance fields of the class. 3

• An instance function member (method, property, indexer, event, instance constructor, or destructor) 4 operates on a given instance of the class, and this instance can be accessed as this (§14.5.7). 5

[Example: The following example illustrates the rules for accessing static and instance members: 6

class Test 7 { 8 int x; 9 static int y; 10

void F() { 11 x = 1; // Ok, same as this.x = 1 12 y = 1; // Ok, same as Test.y = 1 13 } 14

static void G() { 15 x = 1; // Error, cannot access this.x 16 y = 1; // Ok, same as Test.y = 1 17 } 18

static void Main() { 19 Test t = new Test(); 20 t.x = 1; // Ok 21 t.y = 1; // Error, cannot access static member through instance 22 Test.x = 1; // Error, cannot access instance member through type 23 Test.y = 1; // Ok 24 } 25 } 26

The F method shows that in an instance function member, a simple-name (§14.5.2) can be used to access 27 both instance members and static members. The G method shows that in a static function member, it is a 28 compile-time error to access an instance member through a simple-name. The Main method shows that in a 29 member-access (§14.5.4), instance members shall be accessed through instances, and static members shall 30 be accessed through types. end example] 31

17.2.6 Nested types 32

A type declared within a class or struct is called a nested type. A type that is declared within a compilation 33 unit or namespace is called a non-nested type. [Example: In the following example: 34

using System; 35

class A 36 { 37 class B 38 { 39 static void F() { 40 Console.WriteLine("A.B.F"); 41 } 42 } 43 } 44

class B is a nested type because it is declared within class A, and class A is a non-nested type because it is 45 declared within a compilation unit. end example] 46

17.2.6.1 Fully qualified name 47

The fully qualified name (§10.8.2) for a nested type declaration is S.N where S is the fully qualified name of 48 the type declaration in which type N is declared and N is the unqualified name (§10.8.1) of the nested type 49 declaration (including any generic dimensionality indication). 50

Page 286: C# Language Specification

C# LANGUAGE SPECIFICATION

266

17.2.6.2 Declared accessibility 1

Non-nested types can have public or internal declared accessibility and they have internal declared 2 accessibility by default. Nested types can have these forms of declared accessibility too, plus one or more 3 additional forms of declared accessibility, depending on whether the containing type is a class or struct: 4

• A nested type that is declared in a class can have any of five forms of declared accessibility (public, 5 protected internal, protected, internal, or private) and, like other class members, defaults to private 6 declared accessibility. 7

• A nested type that is declared in a struct can have any of three forms of declared accessibility (public, 8 internal, or private) and, like other struct members, defaults to private declared accessibility. 9

[Example: The example 10

public class List 11 { 12 // Private data structure 13 private class Node 14 { 15 public object Data; 16 public Node Next; 17 public Node(object data, Node next) { 18 this.Data = data; 19 this.Next = next; 20 } 21 } 22

private Node first = null; 23 private Node last = null; 24

// Public interface 25 public void AddToFront(object o) {…} 26 public void AddToBack(object o) {…} 27 public object RemoveFromFront() {…} 28 public object AddToFront() {…} 29 public int Count { get {…} } 30 } 31

declares a private nested class Node. end example] 32

17.2.6.3 Hiding 33

A nested type can hide (§10.7.1.1) a base member. The new modifier (§17.2.2) is permitted on nested type 34 declarations so that hiding can be expressed explicitly. [Example: The example 35

using System; 36

class Base 37 { 38 public static void M() { 39 Console.WriteLine("Base.M"); 40 } 41 } 42

class Derived: Base 43 { 44 new public class M 45 { 46 public static void F() { 47 Console.WriteLine("Derived.M.F"); 48 } 49 } 50 } 51

class Test 52 { 53 static void Main() { 54 Derived.M.F(); 55 } 56 } 57

Page 287: C# Language Specification

Chapter 17 Classes

267

shows a nested class M that hides the method M defined in Base. end example] 1

17.2.6.4 this access 2

A nested type and its containing type do not have a special relationship with regard to this-access (§14.5.7). 3 Specifically, this within a nested type cannot be used to refer to instance members of the containing type. 4 In cases where a nested type needs access to the instance members of its containing type, access can be 5 provided by providing the this for the instance of the containing type as a constructor argument for the 6 nested type. [Example: The following example 7

using System; 8

class C 9 { 10 int i = 123; 11 public void F() { 12 Nested n = new Nested(this); 13 n.G(); 14 } 15

public class Nested 16 { 17 C this_c; 18 public Nested(C c) { 19 this_c = c; 20 } 21 public void G() { 22 Console.WriteLine(this_c.i); 23 } 24 } 25 } 26

class Test 27 { 28 static void Main() { 29 C c = new C(); 30 c.F(); 31 } 32 } 33

shows this technique. An instance of C creates an instance of Nested, and passes its own this to Nested's 34 constructor in order to provide subsequent access to C's instance members. end example] 35

17.2.6.5 Access to private and protected members of the containing type 36

A nested type has access to all of the members that are accessible to its containing type, including members 37 of the containing type that have private and protected declared accessibility. [Example: The example 38

using System; 39

class C 40 { 41 private static void F() { 42 Console.WriteLine("C.F"); 43 } 44 public class Nested 45 { 46 public static void G() { 47 F(); 48 } 49 } 50 } 51

class Test 52 { 53 static void Main() { 54 C.Nested.G(); 55 } 56 } 57

Page 288: C# Language Specification

C# LANGUAGE SPECIFICATION

268

shows a class C that contains a nested class Nested. Within Nested, the method G calls the static method F 1 defined in C, and F has private declared accessibility. end example] 2

A nested type also can access protected members defined in a base type of its containing type. [Example: In 3 the following code 4

using System; 5

class Base 6 { 7 protected void F() { 8 Console.WriteLine("Base.F"); 9 } 10 } 11

class Derived: Base 12 { 13 public class Nested 14 { 15 public void G() { 16 Derived d = new Derived(); 17 d.F(); // ok 18 } 19 } 20 } 21

class Test 22 { 23 static void Main() { 24 Derived.Nested n = new Derived.Nested(); 25 n.G(); 26 } 27 } 28

the nested class Derived.Nested accesses the protected method F defined in Derived's base class, Base, 29 by calling through an instance of Derived. end example] 30

17.2.7 Reserved member names 31

To facilitate the underlying C# runtime implementation, for each source member declaration that is a 32 property, event, or indexer, the implementation shall reserve two method signatures based on the kind of the 33 member declaration, its name, and its type (§17.2.7.1, §17.2.7.2, §17.2.7.3). It is a compile-time error for a 34 program to declare a member whose signature matches one of these reserved signatures, even if the 35 underlying runtime implementation does not make use of these reservations. 36

The reserved names do not introduce declarations, thus they do not participate in member lookup. However, 37 a declaration’s associated reserved method signatures do participate in inheritance (§17.2.1), and can be 38 hidden with the new modifier (§17.2.2). 39

[Note: The reservation of these names serves three purposes: 40

1. To allow the underlying implementation to use an ordinary identifier as a method name for get or set 41 access to the C# language feature. 42

2. To allow other languages to interoperate using an ordinary identifier as a method name for get or set 43 access to the C# language feature. 44

3. To help ensure that the source accepted by one conforming compiler is accepted by another, by making 45 the specifics of reserved member names consistent across all C# implementations. 46

end note] 47

The declaration of a destructor (§17.12) also causes a signature to be reserved (§17.2.7.4). 48

17.2.7.1 Member names reserved for properties 49

For a property P (§17.6) of type T, the following signatures are reserved: 50

Page 289: C# Language Specification

Chapter 17 Classes

269

T get_P(); 1 void set_P(T value); 2

Both signatures are reserved, even if the property is read-only or write-only. 3

[Example: In the following code 4

using System; 5

class A 6 { 7 public int P { 8 get { return 123; } 9 } 10 } 11

class B: A 12 { 13 new public int get_P() { 14 return 456; 15 } 16 new public void set_P(int value) { 17 } 18 } 19

class Test 20 { 21 static void Main() { 22 B b = new B(); 23 A a = b; 24 Console.WriteLine(a.P); 25 Console.WriteLine(b.P); 26 Console.WriteLine(b.get_P()); 27 } 28 } 29

a class A defines a read-only property P, thus reserving signatures for get_P and set_P methods. A class B 30 derives from A and hides both of these reserved signatures. The example produces the output: 31

123 32 123 33 456 34

end example] 35

17.2.7.2 Member names reserved for events 36

For an event E (§17.7) of delegate type T, the following signatures are reserved: 37

void add_E(T handler); 38 void remove_E(T handler); 39

17.2.7.3 Member names reserved for indexers 40

For an indexer (§17.8) of type T with parameter-list L, the following signatures are reserved: 41

T get_Item(L); 42 void set_Item(L, T value); 43

Both signatures are reserved, even if the indexer is read-only or write-only. 44

17.2.7.4 Member names reserved for destructors 45

For a class containing a destructor (§17.12), the following signature is reserved: 46

void Finalize(); 47

17.3 Constants 48

A constant is a class member that represents a constant value: a value that can be computed at compile-time. 49 A constant-declaration introduces one or more constants of a given type. 50

Page 290: C# Language Specification

C# LANGUAGE SPECIFICATION

270

constant-declaration: 1 attributesopt constant-modifiersopt const type constant-declarators ; 2

constant-modifiers: 3 constant-modifier 4 constant-modifiers constant-modifier 5

constant-modifier: 6 new 7 public 8 protected 9 internal 10 private 11

constant-declarators: 12 constant-declarator 13 constant-declarators , constant-declarator 14

constant-declarator: 15 identifier = constant-expression 16

A constant-declaration can include a set of attributes (§24), a new modifier (§17.2.2), and a valid 17 combination of the four access modifiers (§17.2.3). The attributes and modifiers apply to all of the members 18 declared by the constant-declaration. Even though constants are considered static members, a constant-19 declaration neither requires nor allows a static modifier. It is an error for the same modifier to appear 20 multiple times in a constant declaration. 21

The type of a constant-declaration specifies the type of the members introduced by the declaration. The type 22 is followed by a list of constant-declarators, each of which introduces a new member. A constant-declarator 23 consists of an identifier that names the member, followed by an “=” token, followed by a constant-24 expression (§14.15) that gives the value of the member. 25

The type specified in a constant declaration shall be sbyte, byte, short, ushort, int, uint, long, 26 ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. Each constant-27 expression shall yield a value of the target type or of a type that can be converted to the target type by an 28 implicit conversion (§13.1). 29

The type of a constant shall be at least as accessible as the constant itself (§10.5.4). 30

The value of a constant is obtained in an expression using a simple-name (§14.5.2) or a member-access 31 (§14.5.4). 32

A constant can itself participate in a constant-expression. Thus, a constant can be used in any construct that 33 requires a constant-expression. [Note: Examples of such constructs include case labels, goto case 34 statements, enum member declarations, attributes, and other constant declarations. end note] 35

[Note: As described in §14.15, a constant-expression is an expression that can be fully evaluated at compile-36 time. Since the only way to create a non-null value of a reference-type other than string is to apply the 37 new operator, and since the new operator is not permitted in a constant-expression, the only possible value 38 for constants of reference-types other than string is null. end note] 39

When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a 40 constant declaration, or when the value cannot be computed at compile-time by a constant-expression, a 41 readonly field (§17.4.2) can be used instead. [Note: The versioning semantics of const and readonly 42 differ (§17.4.2.2). end-note] 43

A constant declaration that declares multiple constants is equivalent to multiple declarations of single 44 constants with the same attributes, modifiers, and type. [Example: 45

class A 46 { 47 public const double X = 1.0, Y = 2.0, Z = 3.0; 48 } 49

Page 291: C# Language Specification

Chapter 17 Classes

271

is equivalent to 1

class A 2 { 3 public const double X = 1.0; 4 public const double Y = 2.0; 5 public const double Z = 3.0; 6 } 7

end example] 8

Constants are permitted to depend on other constants within the same program as long as the dependencies 9 are not of a circular nature. The compiler automatically arranges to evaluate the constant declarations in the 10 appropriate order. [Example: In the following code 11

class A 12 { 13 public const int X = B.Z + 1; 14 public const int Y = 10; 15 } 16

class B 17 { 18 public const int Z = A.Y + 1; 19 } 20

the compiler first evaluates A.Y, then evaluates B.Z, and finally evaluates A.X, producing the values 10, 11, 21 and 12. end example] Constant declarations can depend on constants from other programs, but such 22 dependencies are only possible in one direction. [Example: Referring to the example above, if A and B were 23 declared in separate programs, it would be possible for A.X to depend on B.Z, but B.Z could then not 24 simultaneously depend on A.Y. end example] 25

17.4 Fields 26

A field is a member that represents a variable associated with an object or class. A field-declaration 27 introduces one or more fields of a given type. 28

field-declaration: 29 attributesopt field-modifiersopt type variable-declarators ; 30

field-modifiers: 31 field-modifier 32 field-modifiers field-modifier 33

field-modifier: 34 new 35 public 36 protected 37 internal 38 private 39 static 40 readonly 41 volatile 42

variable-declarators: 43 variable-declarator 44 variable-declarators , variable-declarator 45

variable-declarator: 46 identifier 47 identifier = variable-initializer 48

variable-initializer: 49 expression 50 array-initializer 51

Page 292: C# Language Specification

C# LANGUAGE SPECIFICATION

272

A field-declaration can include a set of attributes (§24), a new modifier (§17.2.2), a valid combination of the 1 four access modifiers (§17.2.3), and a static modifier (§17.4.1). In addition, a field-declaration can 2 include a readonly modifier (§17.4.2) or a volatile modifier (§17.4.3), but not both. The attributes and 3 modifiers apply to all of the members declared by the field-declaration. It is an error for the same modifier 4 to appear multiple times in a field declaration. 5

The type of a field-declaration specifies the type of the members introduced by the declaration. The type is 6 followed by a list of variable-declarators, each of which introduces a new member. A variable-declarator 7 consists of an identifier that names that member, optionally followed by an “=” token and a variable-8 initializer (§17.4.5) that gives the initial value of that member. 9

The type of a field shall be at least as accessible as the field itself (§10.5.4). 10

The value of a field is obtained in an expression using a simple-name (§14.5.2) or a member-access 11 (§14.5.4). The value of a non-readonly field is modified using an assignment (§14.13). The value of a non-12 readonly field can be both obtained and modified using postfix increment and decrement operators (§14.5.9) 13 and prefix increment and decrement operators (§14.6.5). 14

A field declaration that declares multiple fields is equivalent to multiple declarations of single fields with the 15 same attributes, modifiers, and type. [Example: 16

class A 17 { 18 public static int X = 1, Y, Z = 100; 19 } 20

is equivalent to 21

class A 22 { 23 public static int X = 1; 24 public static int Y; 25 public static int Z = 100; 26 } 27

end example] 28

17.4.1 Static and instance fields 29

When a field declaration includes a static modifier, the fields introduced by the declaration are static 30 fields. When no static modifier is present, the fields introduced by the declaration are instance fields. 31 Static fields and instance fields are two of the several kinds of variables (§12) supported by C#, and at times 32 they are referred to as static variables and instance variables, respectively. 33

As explained in §17.2.5, each instance of a class contains a complete set of the instance fields of the class, 34 while there is only one set of static fields for each non-generic class or closed constructed type, regardless of 35 the number of instances of the class or closed constructed type. 36

17.4.2 Readonly fields 37

When a field-declaration includes a readonly modifier, the fields introduced by the declaration are 38 readonly fields. Direct assignments to readonly fields can only occur as part of that declaration or in an 39 instance constructor or static constructor in the same class. (A readonly field can be assigned to multiple 40 times in these contexts.) Specifically, direct assignments to a readonly field are permitted only in the 41 following contexts: 42

• In the variable-declarator that introduces the field (by including a variable-initializer in the 43 declaration). 44

• For an instance field, in the instance constructors of the class that contains the field declaration; for a 45 static field, in the static constructor of the class that contains the field declaration. These are also the 46 only contexts in which it is valid to pass a readonly field as an out or ref parameter. 47

Page 293: C# Language Specification

Chapter 17 Classes

273

Attempting to assign to a readonly field or pass it as an out or ref parameter in any other context is a 1 compile-time error. 2

17.4.2.1 Using static readonly fields for constants 3

A static readonly field is useful when a symbolic name for a constant value is desired, but when the 4 type of the value is not permitted in a const declaration, or when the value cannot be computed at compile-5 time. [Example: In the following code 6

public class Color 7 { 8 public static readonly Color Black = new Color(0, 0, 0); 9 public static readonly Color White = new Color(255, 255, 255); 10 public static readonly Color Red = new Color(255, 0, 0); 11 public static readonly Color Green = new Color(0, 255, 0); 12 public static readonly Color Blue = new Color(0, 0, 255); 13

private byte red, green, blue; 14

public Color(byte r, byte g, byte b) { 15 red = r; 16 green = g; 17 blue = b; 18 } 19 } 20

the Black, White, Red, Green, and Blue members cannot be declared as const members because their 21 values cannot be computed at compile-time. However, declaring them static readonly instead has much 22 the same effect. end example] 23

17.4.2.2 Versioning of constants and static readonly fields 24

Constants and readonly fields have different binary versioning semantics. When an expression references a 25 constant, the value of the constant is obtained at compile-time, but when an expression references a readonly 26 field, the value of the field is not obtained until run-time. [Example: Consider an application that consists of 27 two separate programs: 28

namespace Program1 29 { 30 public class Utils 31 { 32 public static readonly int X = 1; 33 } 34 } 35

and 36

using System; 37

namespace Program2 38 { 39 class Test 40 { 41 static void Main() { 42 Console.WriteLine(Program1.Utils.X); 43 } 44 } 45 } 46

The Program1 and Program2 namespaces denote two programs that are compiled separately. Because 47 Program1.Utils.X is declared as a static readonly field, the value output by the Console.WriteLine 48 statement is not known at compile-time, but rather is obtained at run-time. Thus, if the value of X is changed 49 and Program1 is recompiled, the Console.WriteLine statement will output the new value even if 50 Program2 isn’t recompiled. However, had X been a constant, the value of X would have been obtained at 51 the time Program2 was compiled, and would remain unaffected by changes in Program1 until Program2 52 is recompiled. end example] 53

Page 294: C# Language Specification

C# LANGUAGE SPECIFICATION

274

17.4.3 Volatile fields 1

When a field-declaration includes a volatile modifier, the fields introduced by that declaration are 2 volatile fields. For non-volatile fields, optimization techniques that reorder instructions can lead to 3 unexpected and unpredictable results in multi-threaded programs that access fields without synchronization 4 such as that provided by the lock-statement (§15.12). These optimizations can be performed by the compiler, 5 by the runtime system, or by hardware. For volatile fields, such reordering optimizations are restricted: 6

• A read of a volatile field is called a volatile read. A volatile read has “acquire semantics”; that is, it is 7 guaranteed to occur prior to any references to memory that occur after it in the instruction sequence. 8

• A write of a volatile field is called a volatile write. A volatile write has “release semantics”; that is, it is 9 guaranteed to happen after any memory references prior to the write instruction in the instruction 10 sequence. 11

These restrictions ensure that all threads will observe volatile writes performed by any other thread in the 12 order in which they were performed. A conforming implementation is not required to provide a single total 13 ordering of volatile writes as seen from all threads of execution. The type of a volatile field shall be one of 14 the following: 15

• A reference-type. 16

• A type-parameter that is known to be a reference type (§26.7). 17

• The type byte, sbyte, short, ushort, int, uint, char, float, or bool. 18

• An enum-type having an enum base type of byte, sbyte, short, ushort, int, or uint. 19

[Example: The example 20

using System; 21 using System.Threading; 22

class Test 23 { 24 public static int result; 25 public static volatile bool finished; 26

static void Thread2() { 27 result = 143; 28 finished = true; 29 } 30

static void Main() { 31 finished = false; 32 // Run Thread2() in a new thread 33 new Thread(new ThreadStart(Thread2)).Start(); 34 // Wait for Thread2 to signal that it has a result by setting 35 // finished to true. 36 for (;;) { 37 if (finished) { 38 Console.WriteLine("result = {0}", result); 39 return; 40 } 41 } 42 } 43 } 44

produces the output: 45

result = 143 46

In this example, the method Main starts a new thread that runs the method Thread2. This method stores a 47 value into a non-volatile field called result, then stores true in the volatile field finished. The main 48 thread waits for the field finished to be set to true, then reads the field result. Since finished has 49 been declared volatile, the main thread shall read the value 143 from the field result. If the field 50 finished had not been declared volatile, then it would be permissible for the store to result to be 51 visible to the main thread after the store to finished, and hence for the main thread to read the value 0 52

Page 295: C# Language Specification

Chapter 17 Classes

275

from the field result. Declaring finished as a volatile field prevents any such inconsistency. end 1 example] 2

17.4.4 Field initialization 3

The initial value of a field, whether it be a static field or an instance field, is the default value (§12.2) of the 4 field’s type. It is not possible to observe the value of a field before this default initialization has occurred, 5 and a field is thus never “uninitialized”. [Example: The example 6

using System; 7

class Test 8 { 9 static bool b; 10 int i; 11

static void Main() { 12 Test t = new Test(); 13 Console.WriteLine("b = {0}, i = {1}", b, t.i); 14 } 15 } 16

produces the output 17

b = False, i = 0 18

because b and i are both automatically initialized to default values. end example] 19

17.4.5 Variable initializers 20

Field declarations can include variable-initializers. For static fields, variable initializers correspond to 21 assignment statements that are executed during class initialization. For instance fields, variable initializers 22 correspond to assignment statements that are executed when an instance of the class is created. 23

[Example: The example 24

using System; 25

class Test 26 { 27 static double x = Math.Sqrt(2.0); 28 int i = 100; 29 string s = "Hello"; 30

static void Main() { 31 Test a = new Test(); 32 Console.WriteLine("x = {0}, i = {1}, s = {2}", x, a.i, a.s); 33 } 34 } 35

produces the output 36

x = 1.4142135623731, i = 100, s = Hello 37

because an assignment to x occurs when static field initializers execute and assignments to i and s occur 38 when the instance field initializers execute. end example] 39

The default value initialization described in §17.4.3 occurs for all fields, including fields that have variable 40 initializers. Thus, when a class is initialized, all static fields in that class are first initialized to their default 41 values, and then the static field initializers are executed in textual order. Likewise, when an instance of a 42 class is created, all instance fields in that instance are first initialized to their default values, and then the 43 instance field initializers are executed in textual order. When there are field declarations in multiple partial 44 type declarations for the same type, the order of the parts is unspecified. However, within each part the field 45 initializers are executed in order. 46

It is possible for static fields with variable initializers to be observed in their default value state. [Example: 47 However, this is strongly discouraged as a matter of style. The example 48

using System; 49

Page 296: C# Language Specification

C# LANGUAGE SPECIFICATION

276

class Test 1 { 2 static int a = b + 1; 3 static int b = a + 1; 4

static void Main() { 5 Console.WriteLine("a = {0}, b = {1}", a, b); 6 } 7 } 8

exhibits this behavior. Despite the circular definitions of a and b, the program is valid. It results in the output 9

a = 1, b = 2 10

because the static fields a and b are initialized to 0 (the default value for int) before their initializers are 11 executed. When the initializer for a runs, the value of b is zero, and so a is initialized to 1. When the 12 initializer for b runs, the value of a is already 1, and so b is initialized to 2. end example] 13

17.4.5.1 Static field initialization 14

The static field variable initializers of a class declaration correspond to a sequence of assignments that are 15 executed in the textual order in which they appear in the class declaration. If a static constructor (§17.11) 16 exists in the class, execution of the static field initializers occurs immediately prior to executing that static 17 constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to 18 the first use of a static field of that class. [Example: The example 19

using System; 20

class Test 21 { 22 static void Main() { 23 Console.WriteLine("{0} {1}", B.Y, A.X); 24 } 25 public static int f(string s) { 26 Console.WriteLine(s); 27 return 1; 28 } 29 } 30

class A 31 { 32 public static int X = Test.f("Init A"); 33 } 34

class B 35 { 36 public static int Y = Test.f("Init B"); 37 } 38

might produce either the output: 39

Init A 40 Init B 41 1 1 42

or the output: 43

Init B 44 Init A 45 1 1 46

because the execution of X's initializer and Y's initializer could occur in either order; they are only 47 constrained to occur before the references to those fields. However, in the example: 48

using System; 49

Page 297: C# Language Specification

Chapter 17 Classes

277

class Test 1 { 2 static void Main() { 3 Console.WriteLine("{0} {1}", B.Y, A.X); 4 } 5 public static int f(string s) { 6 Console.WriteLine(s); 7 return 1; 8 } 9 } 10

class A 11 { 12 static A() {} 13 public static int X = Test.f("Init A"); 14 } 15

class B 16 { 17 static B() {} 18 public static int Y = Test.f("Init B"); 19 } 20

the output shall be: 21

Init B 22 Init A 23 1 1 24

because the rules for when static constructors execute provide that B's static constructor (and hence B's static 25 field initializers) shall run before A's static constructor and field initializers. end example] 26

17.4.5.2 Instance field initialization 27

The instance field variable initializers of a class correspond to a sequence of assignments that are executed 28 immediately upon entry to any one of the instance constructors (§17.10.2) of that class. The variable 29 initializers are executed in the textual order in which they appear in the class declaration. The class instance 30 creation and initialization process is described further in §17.10. 31

A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-32 time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to 33 reference any instance member through a simple-name. [Example: In the following code 34

class A 35 { 36 int x = 1; 37 int y = x + 1; // Error, reference to instance member of this 38 } 39

the variable initializer for y results in a compile-time error because it references a member of the instance 40 being created. end example] 41

17.5 Methods 42

A method is a member that implements a computation or action that can be performed by an object or class. 43 Methods are declared using method-declarations: 44

method-declaration: 45 method-header method-body 46

method-header: 47 attributesopt method-modifiersopt return-type member-name type-parameter-listopt 48 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt 49

method-modifiers: 50 method-modifier 51 method-modifiers method-modifier 52

Page 298: C# Language Specification

C# LANGUAGE SPECIFICATION

278

method-modifier: 1 new 2 public 3 protected 4 internal 5 private 6 static 7 virtual 8 sealed 9 override 10 abstract 11 extern 12

return-type: 13 type 14 void 15

member-name: 16 identifier 17 interface-type . identifier 18

method-body: 19 block 20 ; 21

The member-name specifies the name of the method. Unless the method is an explicit interface member 22 implementation (§20.4.1), the member-name is simply an identifier. For an explicit interface member 23 implementation, the member-name consists of an interface-type followed by a “.” and an identifier. 24

A method-declaration can include a set of attributes (§24) and a valid combination of the four access 25 modifiers (§17.2.3), the new (§17.2.2), static (§17.5.2), virtual (§17.5.3), override (§17.5.4), 26 sealed (§17.5.5), abstract (§17.5.6), and extern (§17.5.7) modifiers. 27

A declaration has a valid combination of modifiers if all of the following are true: 28

• The declaration includes a valid combination of access modifiers (§17.2.3). 29

• The declaration does not include the same modifier multiple times. 30

• The declaration includes at most one of the following modifiers: static, virtual, and override. 31

• The declaration includes at most one of the following modifiers: new and override. 32

• If the declaration includes the abstract modifier, then the declaration does not include any of the 33 following modifiers: static, virtual, sealed, or extern. 34

• If the declaration includes the private modifier, then the declaration does not include any of the 35 following modifiers: virtual, override, or abstract. 36

• If the declaration includes the sealed modifier, then the declaration also includes the override 37 modifier. 38

• If the declaration is an explicit interface member implementation (indicated by a member-name 39 including an interface-type, §20.4.1), then the declaration includes no modifiers other than possibly the 40 extern modifier. 41

The return-type of a method declaration specifies the type of the value computed and returned by the 42 method. The return-type is void if the method does not return a value. 43

The optional type-parameter-list specifies the type parameters for a generic method. The optional type-44 parameter-constraints-clauses specify the constraints for the type parameters. A method-declaration shall 45 not have type-parameter-constraints-clauses unless it also has a type-parameter-list. A method-declaration 46 for an explicit interface member implementation shall not have any type-parameter-constraints-clauses. A 47

Page 299: C# Language Specification

Chapter 17 Classes

279

generic method-declaration for an explicit interface member implementation inherits any constraints from 1 the constraints on the interface method. Similarly, a method declaration with the override modifier shall 2 not have any type-parameter-constraints-clauses and the constraints of the method’s type parameters are 3 inherited from the virtual method being overridden. Generic methods are fully specified in §26.6. 4

The optional formal-parameter-list specifies the parameters of the method (§17.5.1). 5

The return-type and each of the types referenced in the formal-parameter-list of a method shall be at least as 6 accessible as the method itself (§10.5.4). 7

For abstract and extern methods, the method-body consists simply of a semicolon. For all other 8 methods, the method-body consists of a block, which specifies the statements to execute when the method is 9 invoked. 10

The name, the number of type parameters, and the formal parameter list of a method define the signature 11 (§10.6) of the method. Specifically, the signature of a method consists of its name, the number of its type 12 parameters, and the number, parameter-modifiers, and types of its formal parameters. The return type is not 13 part of a method’s signature, nor are the names of the formal parameters, the names of the type parameters, 14 or the constraints. When a formal parameter type references a type parameter of the method, the ordinal 15 position of the type parameter (not the name of the type parameter) is used for type equivalence. 16

The name of a method shall differ from the names of all other non-methods declared in the same class. In 17 addition, the signature of a method shall differ from the signatures of all other methods declared in the same 18 class, and two methods declared in the same class shall not have signatures that differ solely by ref and 19 out. 20

17.5.1 Method parameters 21

The parameters of a method, if any, are declared by the method’s formal-parameter-list. 22

formal-parameter-list: 23 fixed-parameters 24 fixed-parameters , parameter-array 25 parameter-array 26

fixed-parameters: 27 fixed-parameter 28 fixed-parameters , fixed-parameter 29

fixed-parameter: 30 attributesopt parameter-modifieropt type identifier 31

parameter-modifier: 32 ref 33 out 34

parameter-array: 35 attributesopt params array-type identifier 36

The formal parameter list consists of one or more comma-separated parameters of which only the last can be 37 a parameter-array. 38

A fixed-parameter consists of an optional set of attributes (§24), an optional ref or out modifier, a type, 39 and an identifier. Each fixed-parameter declares a parameter of the given type with the given name. 40

A parameter-array consists of an optional set of attributes (§24), a params modifier, an array-type, and an 41 identifier. A parameter array declares a single parameter of the given array type with the given name. The 42 array-type of a parameter array shall be a single-dimensional array type (§19.1). In a method invocation, a 43 parameter array permits either a single argument of the given array type to be specified, or it permits zero or 44 more arguments of the array element type to be specified. Parameter arrays are described further in 45 §17.5.1.4. 46

Page 300: C# Language Specification

C# LANGUAGE SPECIFICATION

280

A method declaration creates a separate declaration space (§10.3) for type parameters, formal parameters, 1 local variables, and local constants. Names are introduced into this declaration space by the type parameter 2 list and formal parameter list of the method and by local variable declarations and by local constant 3 declarations in the block of the method. All names in the declaration space of a method shall be unique; 4 otherwise, a compile-time error results. 5

A method invocation (§14.5.5.1) creates a copy, specific to that invocation, of the formal parameters and 6 local variables of the method, and the argument list of the invocation assigns values or variable references to 7 the newly created formal parameters. Within the block of a method, formal parameters can be referenced by 8 their identifiers in simple-name expressions (§14.5.2). 9

There are four kinds of formal parameters: 10

• Value parameters, which are declared without any modifiers. 11

• Reference parameters, which are declared with the ref modifier. 12

• Output parameters, which are declared with the out modifier. 13

• Parameter arrays, which are declared with the params modifier. 14

[Note: As described in §10.6, the ref and out modifiers are part of a method’s signature, but the params 15 modifier is not. end note] 16

17.5.1.1 Value parameters 17

A parameter declared with no modifiers is a value parameter. A value parameter corresponds to a local 18 variable that gets its initial value from the corresponding argument supplied in the method invocation. 19

When a formal parameter is a value parameter, the corresponding argument in a method invocation shall be 20 an expression of a type that is implicitly convertible (§13.1) to the formal parameter type. 21

A method is permitted to assign new values to a value parameter. Such assignments only affect the local 22 storage location represented by the value parameter—they have no effect on the actual argument given in the 23 method invocation. 24

17.5.1.2 Reference parameters 25

A parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a reference 26 parameter does not create a new storage location. Instead, a reference parameter represents the same storage 27 location as the variable given as the argument in the method invocation. 28

When a formal parameter is a reference parameter, the corresponding argument in a method invocation shall 29 consist of the keyword ref followed by a variable-reference (§12.3.3.27) of the same type as the formal 30 parameter. A variable shall be definitely assigned before it can be passed as a reference parameter. 31

Within a method, a reference parameter is always considered definitely assigned. 32

[Example: The example 33

using System; 34

class Test 35 { 36 static void Swap(ref int x, ref int y) { 37 int temp = x; 38 x = y; 39 y = temp; 40 } 41

static void Main() { 42 int i = 1, j = 2; 43 Swap(ref i, ref j); 44 Console.WriteLine("i = {0}, j = {1}", i, j); 45 } 46 } 47

Page 301: C# Language Specification

Chapter 17 Classes

281

produces the output 1

i = 2, j = 1 2

For the invocation of Swap in Main, x represents i and y represents j. Thus, the invocation has the effect of 3 swapping the values of i and j. end example] 4

In a method that takes reference parameters, it is possible for multiple names to represent the same storage 5 location. [Example: In the following code 6

class A 7 { 8 string s; 9

void F(ref string a, ref string b) { 10 s = "One"; 11 a = "Two"; 12 b = "Three"; 13 } 14

void G() { 15 F(ref s, ref s); 16 } 17 } 18

the invocation of F in G passes a reference to s for both a and b. Thus, for that invocation, the names s, a, 19 and b all refer to the same storage location, and the three assignments all modify the instance field s. end 20 example] 21

17.5.1.3 Output parameters 22

A parameter declared with an out modifier is an output parameter. Similar to a reference parameter, an 23 output parameter does not create a new storage location. Instead, an output parameter represents the same 24 storage location as the variable given as the argument in the method invocation. 25

When a formal parameter is an output parameter, the corresponding argument in a method invocation shall 26 consist of the keyword out followed by a variable-reference (§12.3.3.27) of the same type as the formal 27 parameter. A variable need not be definitely assigned before it can be passed as an output parameter, but 28 following an invocation where a variable was passed as an output parameter, the variable is considered 29 definitely assigned. 30

Within a method, just like a local variable, an output parameter is initially considered unassigned and shall 31 be definitely assigned before its value is used. 32

Every output parameter of a method shall be definitely assigned before the method returns. 33

Output parameters are typically used in methods that produce multiple return values. [Example: 34

using System; 35

class Test 36 { 37 static void SplitPath(string path, out string dir, out string name) { 38 int i = path.Length; 39 while (i > 0) { 40 char ch = path[i – 1]; 41 if (ch == '\\' || ch == '/' || ch == ':') break; 42 i--; 43 } 44 dir = path.Substring(0, i); 45 name = path.Substring(i); 46 } 47

static void Main() { 48 string dir, name; 49 SplitPath(@"c:\Windows\System\hello.txt", out dir, out name); 50 Console.WriteLine(dir); 51 Console.WriteLine(name); 52 } 53 } 54

Page 302: C# Language Specification

C# LANGUAGE SPECIFICATION

282

The example produces the output: 1

c:\Windows\System\ 2 hello.txt 3

Note that the dir and name variables can be unassigned before they are passed to SplitPath, and that they 4 are considered definitely assigned following the call. end example] 5

17.5.1.4 Parameter arrays 6

A parameter declared with a params modifier is a parameter array. If a formal parameter list includes a 7 parameter array, it shall be the last parameter in the list and it shall be of a single-dimensional array type. 8 [Example: The types string[] and string[][,] can be used as the type of a parameter array, but the 9 type string[,] can not. end example] It is not possible to combine the params modifier with the 10 modifiers ref and out. 11

A parameter array permits arguments to be specified in one of two ways in a method invocation: 12

• The argument given for a parameter array can be a single expression that is implicitly convertible 13 (§13.1) to the parameter array type. In this case, the parameter array acts precisely like a value 14 parameter. 15

• Alternatively, the invocation can specify zero or more arguments for the parameter array, where each 16 argument is an expression that is implicitly convertible (§13.1) to the element type of the parameter 17 array. In this case, the invocation creates an instance of the parameter array type with a length 18 corresponding to the number of arguments, initializes the elements of the array instance with the given 19 argument values, and uses the newly created array instance as the actual argument. 20

Except for allowing a variable number of arguments in an invocation, a parameter array is precisely 21 equivalent to a value parameter (§17.5.1.1) of the same type. 22

[Example: The example 23

using System; 24

class Test 25 { 26 static void F(params int[] args) { 27 Console.Write("Array contains {0} elements:", args.Length); 28 foreach (int i in args) 29 Console.Write(" {0}", i); 30 Console.WriteLine(); 31 } 32

static void Main() { 33 int[] arr = {1, 2, 3}; 34 F(arr); 35 F(10, 20, 30, 40); 36 F(); 37 } 38 } 39

produces the output 40

Array contains 3 elements: 1 2 3 41 Array contains 4 elements: 10 20 30 40 42 Array contains 0 elements: 43

The first invocation of F simply passes the array arr as a value parameter. The second invocation of F 44 automatically creates a four-element int[] with the given element values and passes that array instance as a 45 value parameter. Likewise, the third invocation of F creates a zero-element int[] and passes that instance 46 as a value parameter. The second and third invocations are precisely equivalent to writing: 47

F(new int[] {10, 20, 30, 40}); 48 F(new int[] {}); 49

end example] 50

Page 303: C# Language Specification

Chapter 17 Classes

283

When performing overload resolution, a method with a parameter array might be applicable, either in its 1 normal form or in its expanded form (§14.4.2.1). The expanded form of a method is available only if the 2 normal form of the method is not applicable and only if a method with the same signature as the expanded 3 form is not already declared in the same type. 4

[Example: The example 5

using System; 6

class Test 7 { 8 static void F(params object[] a) { 9 Console.WriteLine("F(object[])"); 10 } 11

static void F() { 12 Console.WriteLine("F()"); 13 } 14

static void F(object a0, object a1) { 15 Console.WriteLine("F(object,object)"); 16 } 17

static void Main() { 18 F(); 19 F(1); 20 F(1, 2); 21 F(1, 2, 3); 22 F(1, 2, 3, 4); 23 } 24 } 25

produces the output 26

F(); 27 F(object[]); 28 F(object,object); 29 F(object[]); 30 F(object[]); 31

In the example, two of the possible expanded forms of the method with a parameter array are already 32 included in the class as regular methods. These expanded forms are therefore not considered when 33 performing overload resolution, and the first and third method invocations thus select the regular methods. 34 When a class declares a method with a parameter array, it is not uncommon to also include some of the 35 expanded forms as regular methods. By doing so it is possible to avoid the allocation of an array instance 36 that occurs when an expanded form of a method with a parameter array is invoked. end example] 37

When the type of a parameter array is object[], a potential ambiguity arises between the normal form of 38 the method and the expended form for a single object parameter. The reason for the ambiguity is that an 39 object[] is itself implicitly convertible to type object. The ambiguity presents no problem, however, 40 since it can be resolved by inserting a cast if needed. 41

[Example: The example 42

using System; 43

class Test 44 { 45 static void F(params object[] args) { 46 foreach (object o in args) { 47 Console.Write(o.GetType().FullName); 48 Console.Write(" "); 49 } 50 Console.WriteLine(); 51 } 52

Page 304: C# Language Specification

C# LANGUAGE SPECIFICATION

284

static void Main() { 1 object[] a = {1, "Hello", 123.456}; 2 object o = a; 3 F(a); 4 F((object)a); 5 F(o); 6 F((object[])o); 7 } 8 } 9

produces the output 10

System.Int32 System.String System.Double 11 System.Object[] 12 System.Object[] 13 System.Int32 System.String System.Double 14

In the first and last invocations of F, the normal form of F is applicable because an implicit conversion exists 15 from the argument type to the parameter type (both are of type object[]). Thus, overload resolution selects 16 the normal form of F, and the argument is passed as a regular value parameter. In the second and third 17 invocations, the normal form of F is not applicable because no implicit conversion exists from the argument 18 type to the parameter type (type object cannot be implicitly converted to type object[]). However, the 19 expanded form of F is applicable, so it is selected by overload resolution. As a result, a one-element 20 object[] is created by the invocation, and the single element of the array is initialized with the given 21 argument value (which itself is a reference to an object[]). end example] 22

17.5.2 Static and instance methods 23

When a method declaration includes a static modifier, that method is said to be a static method. When no 24 static modifier is present, the method is said to be an instance method. 25

A static method does not operate on a specific instance, and it is a compile-time error to refer to this in a 26 static method. 27

An instance method operates on a given instance of a class, and that instance can be accessed as this 28 (§14.5.7). 29

The differences between static and instance members are discussed further in §17.2.5. 30

17.5.3 Virtual methods 31

When an instance method declaration includes a virtual modifier, that method is said to be a virtual 32 method. When no virtual modifier is present, the method is said to be a non-virtual method. 33

The implementation of a non-virtual method is invariant: The implementation is the same whether the 34 method is invoked on an instance of the class in which it is declared or an instance of a derived class. In 35 contrast, the implementation of a virtual method can be superseded by derived classes. The process of 36 superseding the implementation of an inherited virtual method is known as overriding that method (§17.5.4). 37

In a virtual method invocation, the run-time type of the instance for which that invocation takes place 38 determines the actual method implementation to invoke. In a non-virtual method invocation, the compile-39 time type of the instance is the determining factor. In precise terms, when a method named N is invoked with 40 an argument list A on an instance with a compile-time type C and a run-time type R (where R is either C or a 41 class derived from C), the invocation is processed as follows: 42

• First, overload resolution is applied to C, N, and A, to select a specific method M from the set of methods 43 declared in and inherited by C. This is described in §14.5.5.1. 44

• Then, if M is a non-virtual method, M is invoked. 45

• Otherwise, M is a virtual method, and the most derived implementation of M with respect to R is invoked. 46

For every virtual method declared in or inherited by a class, there exists a most derived implementation of 47 the method with respect to that class. The most derived implementation of a virtual method M with respect to 48 a class R is determined as follows: 49

Page 305: C# Language Specification

Chapter 17 Classes

285

• If R contains the introducing virtual declaration of M, then this is the most derived implementation 1 of M. 2

• Otherwise, if R contains an override of M, then this is the most derived implementation of M with 3 respect to R. 4

• Otherwise, the most derived implementation of M with respect to R is the same as the most derived 5 implementation of M with respect to the direct base class of R. 6

[Example: The following example illustrates the differences between virtual and non-virtual methods: 7

using System; 8

class A 9 { 10 public void F() { Console.WriteLine("A.F"); } 11 public virtual void G() { Console.WriteLine("A.G"); } 12 } 13

class B: A 14 { 15 new public void F() { Console.WriteLine("B.F"); } 16 public override void G() { Console.WriteLine("B.G"); } 17 } 18

class Test 19 { 20 static void Main() { 21 B b = new B(); 22 A a = b; 23 a.F(); 24 b.F(); 25 a.G(); 26 b.G(); 27 } 28 } 29

In the example, A introduces a non-virtual method F and a virtual method G. The class B introduces a new 30 non-virtual method F, thus hiding the inherited F, and also overrides the inherited method G. The example 31 produces the output: 32

A.F 33 B.F 34 B.G 35 B.G 36

Notice that the statement a.G() invokes B.G, not A.G. This is because the run-time type of the instance 37 (which is B), not the compile-time type of the instance (which is A), determines the actual method 38 implementation to invoke. end example] 39

Because methods are allowed to hide inherited methods, it is possible for a class to contain several virtual 40 methods with the same signature. This does not present an ambiguity problem, since all but the most derived 41 method are hidden. [Example: In the following code 42

using System; 43

class A 44 { 45 public virtual void F() { Console.WriteLine("A.F"); } 46 } 47

class B: A 48 { 49 public override void F() { Console.WriteLine("B.F"); } 50 } 51

class C: B 52 { 53 new public virtual void F() { Console.WriteLine("C.F"); } 54 } 55

Page 306: C# Language Specification

C# LANGUAGE SPECIFICATION

286

class D: C 1 { 2 public override void F() { Console.WriteLine("D.F"); } 3 } 4

class Test 5 { 6 static void Main() { 7 D d = new D(); 8 A a = d; 9 B b = d; 10 C c = d; 11 a.F(); 12 b.F(); 13 c.F(); 14 d.F(); 15 } 16 } 17

the C and D classes contain two virtual methods with the same signature: The one introduced by A and the 18 one introduced by C. The method introduced by C hides the method inherited from A. Thus, the override 19 declaration in D overrides the method introduced by C, and it is not possible for D to override the method 20 introduced by A. The example produces the output: 21

B.F 22 B.F 23 D.F 24 D.F 25

Note that it is possible to invoke the hidden virtual method by accessing an instance of D through a less 26 derived type in which the method is not hidden. end example] 27

17.5.4 Override methods 28

When an instance method declaration includes an override modifier, the method is said to be an override 29 method. An override method overrides an inherited virtual method with the same signature. Whereas a 30 virtual method declaration introduces a new method, an override method declaration specializes an existing 31 inherited virtual method by providing a new implementation of that method. 32

The method overridden by an override declaration is known as the overridden base method. For an 33 override method M declared in a class C, the overridden base method is determined by examining each base 34 class of C, starting with the direct base class of C and continuing with each successive direct base class, until 35 an accessible method with the same signature as M is located. For the purposes of locating the overridden 36 base method, a method is considered accessible if it is public, if it is protected, if it is protected 37 internal, or if it is internal and declared in the same program as C. 38

A compile-time error occurs unless all of the following are true for an override declaration: 39

• An overridden base method can be located as described above. 40

• The overridden base method is a virtual, abstract, or override method. In other words, the overridden 41 base method cannot be static or non-virtual. 42

• The overridden base method is not a sealed method. 43

• The override declaration and the overridden base method have the same return type. 44

• The override declaration and the overridden base method have the same declared accessibility. In other 45 words, an override declaration cannot change the accessibility of the virtual method. 46

An override declaration can access the overridden base method using a base-access (§14.5.8). [Example: In 47 the following code 48

class A 49 { 50 int x; 51

Page 307: C# Language Specification

Chapter 17 Classes

287

public virtual void PrintFields() { 1 Console.WriteLine("x = {0}", x); 2 } 3 } 4

class B: A 5 { 6 int y; 7

public override void PrintFields() { 8 base.PrintFields(); 9 Console.WriteLine("y = {0}", y); 10 } 11 } 12

the base.PrintFields() invocation in B invokes the PrintFields method declared in A. A base-13 access disables the virtual invocation mechanism and simply treats the base method as a non-virtual method. 14 Had the invocation in B been written ((A)this).PrintFields(), it would recursively invoke the 15 PrintFields method declared in B, not the one declared in A, since PrintFields is virtual and the run-time 16 type of ((A)this) is B. end example] 17

Only by including an override modifier can a method override another method. In all other cases, a 18 method with the same signature as an inherited method simply hides the inherited method. [Example: In the 19 following code 20

class A 21 { 22 public virtual void F() {} 23 } 24

class B: A 25 { 26 public virtual void F() {} // Warning, hiding inherited F() 27 } 28

the F method in B does not include an override modifier and therefore does not override the F method 29 in A. Rather, the F method in B hides the method in A, and a warning is reported because the declaration does 30 not include a new modifier. end example] 31

[Example: In the following code 32

class A 33 { 34 public virtual void F() {} 35 } 36

class B: A 37 { 38 new private void F() {} // Hides A.F within B 39 } 40

class C: B 41 { 42 public override void F() {} // Ok, overrides A.F 43 } 44

the F method in B hides the virtual F method inherited from A. Since the new F in B has private access, its 45 scope only includes the class body of B and does not extend to C. Therefore, the declaration of F in C is 46 permitted to override the F inherited from A. end example] 47

17.5.5 Sealed methods 48

When an instance method declaration includes a sealed modifier, that method is said to be a sealed 49 method. A sealed method overrides an inherited virtual method with the same signature. A sealed method 50 shall also be marked with the override modifier. Use of the sealed modifier prevents a derived class 51 from further overriding the method. 52

[Example: The example 53

using System; 54

Page 308: C# Language Specification

C# LANGUAGE SPECIFICATION

288

class A 1 { 2 public virtual void F() { 3 Console.WriteLine("A.F"); 4 } 5

public virtual void G() { 6 Console.WriteLine("A.G"); 7 } 8 } 9

class B: A 10 { 11 public sealed override void F() { 12 Console.WriteLine("B.F"); 13 } 14

public override void G() { 15 Console.WriteLine("B.G"); 16 } 17 } 18

class C: B 19 { 20 public override void G() { 21 Console.WriteLine("C.G"); 22 } 23 } 24

the class B provides two override methods: an F method that has the sealed modifier and a G method that 25 does not. B’s use of the sealed modifier prevents C from further overriding F. end example] 26

17.5.6 Abstract methods 27

When an instance method declaration includes an abstract modifier, that method is said to be an abstract 28 method. Although an abstract method is implicitly also a virtual method, it cannot have the modifier 29 virtual. 30

An abstract method declaration introduces a new virtual method but does not provide an implementation of 31 that method. Instead, non-abstract derived classes are required to provide their own implementation by 32 overriding that method. Because an abstract method provides no actual implementation, the method-body of 33 an abstract method simply consists of a semicolon. 34

Abstract method declarations are only permitted in abstract classes (§17.1.1.1). 35

[Example: In the following code 36

public abstract class Shape 37 { 38 public abstract void Paint(Graphics g, Rectangle r); 39 } 40

public class Ellipse: Shape 41 { 42 public override void Paint(Graphics g, Rectangle r) { 43 g.DrawEllipse(r); 44 } 45 } 46

public class Box: Shape 47 { 48 public override void Paint(Graphics g, Rectangle r) { 49 g.DrawRect(r); 50 } 51 } 52

the Shape class defines the abstract notion of a geometrical shape object that can paint itself. The Paint 53 method is abstract because there is no meaningful default implementation. The Ellipse and Box classes 54 are concrete Shape implementations. Because these classes are non-abstract, they are required to override 55 the Paint method and provide an actual implementation. end example] 56

Page 309: C# Language Specification

Chapter 17 Classes

289

It is a compile-time error for a base-access (§14.5.8) to reference an abstract method. [Example: In the 1 following code 2

abstract class A 3 { 4 public abstract void F(); 5 } 6

class B: A 7 { 8 public override void F() { 9 base.F(); // Error, base.F is abstract 10 } 11 } 12

a compile-time error is reported for the base.F() invocation because it references an abstract method. end 13 example] 14

An abstract method declaration is permitted to override a virtual method. This allows an abstract class to 15 force re-implementation of the method in derived classes, and makes the original implementation of the 16 method unavailable. [Example: In the following code 17

using System; 18

class A 19 { 20 public virtual void F() { 21 Console.WriteLine("A.F"); 22 } 23 } 24

abstract class B: A 25 { 26 public abstract override void F(); 27 } 28

class C: B 29 { 30 public override void F() { 31 Console.WriteLine("C.F"); 32 } 33 } 34

class A declares a virtual method, class B overrides this method with an abstract method, and class C 35 overrides that abstract method to provide its own implementation. end example] 36

17.5.7 External methods 37

When a method declaration includes an extern modifier, the method is said to be an external method. 38 External methods are implemented externally, typically using a language other than C#. Because an external 39 method declaration provides no actual implementation, the method-body of an external method simply 40 consists of a semicolon. 41

The mechanism by which linkage to an external method is achieved, is implementation-defined. 42

[Example: The following example demonstrates the use of the extern modifier in combination with a 43 DllImport attribute that specifies the name of the external library in which the method is implemented: 44

using System.Text; 45 using System.Security.Permissions; 46 using System.Runtime.InteropServices; 47

class Path 48 { 49 [DllImport("kernel32", SetLastError=true)] 50 static extern bool CreateDirectory(string name, SecurityAttribute sa); 51

[DllImport("kernel32", SetLastError=true)] 52 static extern bool RemoveDirectory(string name); 53

Page 310: C# Language Specification

C# LANGUAGE SPECIFICATION

290

[DllImport("kernel32", SetLastError=true)] 1 static extern int GetCurrentDirectory(int bufSize, StringBuilder buf); 2

[DllImport("kernel32", SetLastError=true)] 3 static extern bool SetCurrentDirectory(string name); 4 } 5

end example] 6

17.5.8 Method body 7

The method-body of a method declaration consists of either a block or a semicolon. 8

Abstract and external method declarations do not provide a method implementation, so their method bodies 9 simply consist of a semicolon. For any other method, the method body is a block (§15.2) that contains the 10 statements to execute when that method is invoked. 11

When the return type of a method is void, return statements (§15.9.4) in that method’s body are not 12 permitted to specify an expression. If execution of the method body of a void method completes normally 13 (that is, control flows off the end of the method body), that method simply returns to its caller. 14

When the return type of a method is not void, each return statement in that method body shall specify an 15 expression of a type that is implicitly convertible to the return type. The endpoint of the method body of a 16 value-returning method shall not be reachable. In other words, in a value-returning method, control is not 17 permitted to flow off the end of the method body. 18

[Example: In the following code 19

class A 20 { 21 public int F() {} // Error, return value required 22

public int G() { 23 return 1; 24 } 25

public int H(bool b) { 26 if (b) { 27 return 1; 28 } 29 else { 30 return 0; 31 } 32 } 33 } 34

the value-returning F method results in a compile-time error because control can flow off the end of the 35 method body. The G and H methods are correct because all possible execution paths end in a return 36 statement that specifies a return value. end example] 37

17.5.9 Method overloading 38

The method overload resolution rules are described in §14.4.2. 39

17.6 Properties 40

A property is a member that provides access to a characteristic of an object or a class. Examples of 41 properties include the length of a string, the size of a font, the caption of a window, the name of a customer, 42 and so on. Properties are a natural extension of fields—both are named members with associated types, and 43 the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote 44 storage locations. Instead, properties have accessors that specify the statements to be executed when their 45 values are read or written. Properties thus provide a mechanism for associating actions with the reading and 46 writing of an object’s characteristics; furthermore, they permit such characteristics to be computed. 47

Properties are declared using property-declarations: 48

Page 311: C# Language Specification

Chapter 17 Classes

291

property-declaration: 1 attributesopt property-modifiersopt type member-name { accessor-declarations } 2

property-modifiers: 3 property-modifier 4 property-modifiers property-modifier 5

property-modifier: 6 new 7 public 8 protected 9 internal 10 private 11 static 12 virtual 13 sealed 14 override 15 abstract 16 extern 17

member-name: 18 identifier 19 interface-type . identifier 20

A property-declaration can include a set of attributes (§24) and a valid combination of the four access 21 modifiers (§17.2.3), the new (§17.2.2), static (§17.6.1), virtual (§17.5.3, §17.6.3), override (§17.5.4, 22 §17.6.3), sealed (§17.5.5), abstract (§17.5.6, §17.6.3), and extern modifiers. 23

Property declarations are subject to the same rules as method declarations (§17.5) with regard to valid 24 combinations of modifiers. 25

The type of a property declaration specifies the type of the property introduced by the declaration, and the 26 member-name specifies the name of the property. Unless the property is an explicit interface member 27 implementation, the member-name is simply an identifier. For an explicit interface member implementation 28 (§20.4.1), the member-name consists of an interface-type followed by a “.” and an identifier. 29

The type of a property shall be at least as accessible as the property itself (§10.5.4). 30

The accessor-declarations, which shall be enclosed in “{” and “}” tokens, declare the accessors (§17.6.2) of 31 the property. The accessors specify the executable statements associated with reading and writing the 32 property. 33

Even though the syntax for accessing a property is the same as that for a field, a property is not classified as 34 a variable. Thus, it is not possible to pass a property as a ref or out argument. 35

When a property declaration includes an extern modifier, the property is said to be an external property. 36 Because an external property declaration provides no actual implementation, each of its accessor-37 declarations consists of a semicolon. 38

17.6.1 Static and instance properties 39

When a property declaration includes a static modifier, the property is said to be a static property. When 40 no static modifier is present, the property is said to be an instance property. 41

A static property is not associated with a specific instance, and it is a compile-time error to refer to this in 42 the accessors of a static property. 43

An instance property is associated with a given instance of a class, and that instance can be accessed as 44 this (§14.5.7) in the accessors of that property. 45

The differences between static and instance members are discussed further in §17.2.5. 46

Page 312: C# Language Specification

C# LANGUAGE SPECIFICATION

292

17.6.2 Accessors 1

The accessor-declarations of a property specify the executable statements associated with reading and 2 writing that property. 3

accessor-declarations: 4 get-accessor-declaration set-accessor-declarationopt 5 set-accessor-declaration get-accessor-declarationopt 6

get-accessor-declaration: 7 attributesopt accessor-modifieropt get accessor-body 8

set-accessor-declaration: 9 attributesopt accessor-modifieropt set accessor-body 10

accessor-modifier: 11 protected 12 internal 13 private 14 protected internal 15 internal protected 16

accessor-body: 17 block 18 ; 19

The accessor declarations consist of a get-accessor-declaration, a set-accessor-declaration, or both. Each 20 accessor declaration consists of the token get or set followed by an accessor-body. For abstract and 21 extern properties, the accessor-body for each accessor specified is simply a semicolon. For the accessors 22 of any non-abstract, non-extern property, the accessor-body is a block which specifies the statements to be 23 executed when the corresponding accessor is invoked. 24

A get accessor corresponds to a parameterless method with a return value of the property type. Except as 25 the target of an assignment, when a property is referenced in an expression, the get accessor of the property 26 is invoked to compute the value of the property (§14.1.1). The body of a get accessor shall conform to the 27 rules for value-returning methods described in §17.5.8. In particular, all return statements in the body of a 28 get accessor shall specify an expression that is implicitly convertible to the property type. Furthermore, the 29 endpoint of a get accessor shall not be reachable. 30

A set accessor corresponds to a method with a single value parameter of the property type and a void 31 return type. The implicit parameter of a set accessor is always named value. When a property is 32 referenced as the target of an assignment (§14.13), or as the operand of ++ or –- (§14.5.9, 14.6.5), the set 33 accessor is invoked with an argument that provides the new value (§14.13.1). The body of a set accessor 34 shall conform to the rules for void methods described in §17.5.8. In particular, return statements in the 35 set accessor body are not permitted to specify an expression. Since a set accessor implicitly has a 36 parameter named value, it is a compile-time error for a local variable declaration or a local constant 37 declaration in a set accessor to have that name. 38

Based on the presence or absence of the get and set accessors, a property is classified as follows: 39

• A property that includes both a get accessor and a set accessor is said to be a read-write property. 40

• A property that has only a get accessor is said to be a read-only property. It is a compile-time error for 41 a read-only property to be the target of an assignment. 42

• A property that has only a set accessor is said to be a write-only property. Except as the target of a 43 simple assignment, it is a compile-time error to reference a write-only property in an expression. [Note: 44 The pre- and postfix ++ and -- operators and compound assignment operators cannot be applied to 45 write-only properties, since these operators read the old value of their operand before they write the new 46 one. end note] 47

The use of accessor-modifiers is governed by the following restrictions: 48

Page 313: C# Language Specification

Chapter 17 Classes

293

• An accessor-modifier cannot be used in an interface or in an explicit interface member implementation. 1

• For a property or indexer that has no override modifer, an accessor-modifier is permitted only if the 2 property or indexer has both a get and set accessor, and then is permitted only on one of those 3 accessors. 4

• For a property or indexer that includes an override modifer, the accessor-modifier of an accessor shall 5 match the accessor-modifier, if any, of the accessor being overridden. 6

• The accessor-modifier shall declare an accessibility that is strictly more restrictive than the declared 7 accessibility of the property or indexer itself. To be precise: 8

o If the property or indexer has a declared accessibility of public, any accessor-modifier can be used. 9

o If the property or indexer has a declared accessibility of protected internal, the accessor-modifier can 10 be internal, protected, or private. 11

o If the property or indexer has a declared accessibility of internal or protected, the accessor-modifier 12 shall be private. 13

o If the property or indexer has a declared accessibility of private, no accessor-modifier shall be used. 14

If an accessor has an accessor-modifier, the accessibility domain (§10.5.2) of the accessor is determined 15 using the declared accessibility of the accessor-modifier. If an accessor does not have an accessor-modifier, 16 the accessibility domain of the accessor is determined from the declared accessibility of the property or 17 indexer. 18

[Example: In the following code 19

public class Button: Control 20 { 21 private string caption; 22

public string Caption { 23 get { 24 return caption; 25 } 26 set { 27 if (caption != value) { 28 caption = value; 29 Repaint(); 30 } 31 } 32 } 33

public override void Paint(Graphics g, Rectangle r) { 34 // Painting code goes here 35 } 36 } 37

the Button control declares a public Caption property. The get accessor of the Caption property returns 38 the string stored in the private caption field. The set accessor checks if the new value is different from the 39 current value, and if so, it stores the new value and repaints the control. Properties often follow the pattern 40 shown above: The get accessor simply returns a value stored in a private field, and the set accessor 41 modifies that private field and then performs any additional actions required to fully update the state of the 42 object. 43

Given the Button class above, the following is an example of use of the Caption property: 44

Button okButton = new Button(); 45 okButton.Caption = "OK"; // Invokes set accessor 46 string s = okButton.Caption; // Invokes get accessor 47

Here, the set accessor is invoked by assigning a value to the property, and the get accessor is invoked by 48 referencing the property in an expression. end example] 49

The get and set accessors of a property are not distinct members, and it is not possible to declare the 50 accessors of a property separately. [Example: The example 51

Page 314: C# Language Specification

C# LANGUAGE SPECIFICATION

294

class A 1 { 2 private string name; 3

public string Name { // Error, duplicate member name 4 get { return name; } 5 } 6

public string Name { // Error, duplicate member name 7 set { name = value; } 8 } 9 } 10

does not declare a single read-write property. Rather, it declares two properties with the same name, one 11 read-only and one write-only. Since two members declared in the same class cannot have the same name, the 12 example causes a compile-time error to occur. end example] 13

When a derived class declares a property by the same name as an inherited property, the derived property 14 hides the inherited property with respect to both reading and writing. [Example: In the following code 15

class A 16 { 17 public int P { 18 set {…} 19 } 20 } 21

class B: A 22 { 23 new public int P { 24 get {…} 25 } 26 } 27

the P property in B hides the P property in A with respect to both reading and writing. Thus, in the 28 statements 29

B b = new B(); 30 b.P = 1; // Error, B.P is read-only 31 ((A)b).P = 1; // Ok, reference to A.P 32

the assignment to b.P causes a compile-time error to be reported, since the read-only P property in B hides 33 the write-only P property in A. Note, however, that a cast can be used to access the hidden P property. end 34 example] 35

Unlike public fields, properties provide a separation between an object’s internal state and its public 36 interface. [Example: Consider the example: 37

class Label 38 { 39 private int x, y; 40 private string caption; 41

public Label(int x, int y, string caption) { 42 this.x = x; 43 this.y = y; 44 this.caption = caption; 45 } 46

public int X { 47 get { return x; } 48 } 49

public int Y { 50 get { return y; } 51 } 52

public Point Location { 53 get { return new Point(x, y); } 54 } 55

Page 315: C# Language Specification

Chapter 17 Classes

295

public string Caption { 1 get { return caption; } 2 } 3 } 4

Here, the Label class uses two int fields, x and y, to store its location. The location is publicly exposed 5 both as an X and a Y property and as a Location property of type Point. If, in a future version of Label, 6 it becomes more convenient to store the location as a Point internally, the change can be made without 7 affecting the public interface of the class: 8

class Label 9 { 10 private Point location; 11 private string caption; 12

public Label(int x, int y, string caption) { 13 this.location = new Point(x, y); 14 this.caption = caption; 15 } 16

public int X { 17 get { return location.x; } 18 } 19

public int Y { 20 get { return location.y; } 21 } 22

public Point Location { 23 get { return location; } 24 } 25

public string Caption { 26 get { return caption; } 27 } 28 } 29

Had x and y instead been public readonly fields, it would have been impossible to make such a change 30 to the Label class. end example] 31

[Note: Exposing state through properties is not necessarily any less efficient than exposing fields directly. In 32 particular, when a property is non-virtual and contains only a small amount of code, the execution 33 environment might replace calls to accessors with the actual code of the accessors. This process is known as 34 inlining, and it makes property access as efficient as field access, yet preserves the increased flexibility of 35 properties. end note] 36

[Example: Since invoking a get accessor is conceptually equivalent to reading the value of a field, it is 37 considered bad programming style for get accessors to have observable side-effects. In the example 38

class Counter 39 { 40 private int next; 41

public int Next { 42 get { return next++; } 43 } 44 } 45

the value of the Next property depends on the number of times the property has previously been accessed. 46 Thus, accessing the property produces an observable side effect, and the property should be implemented as 47 a method instead. end example] 48

[Note: The “no side-effects” convention for get accessors doesn’t mean that get accessors should always 49 be written to simply return values stored in fields. Indeed, get accessors often compute the value of a 50 property by accessing multiple fields or invoking methods. However, a properly designed get accessor 51 performs no actions that cause observable changes in the state of the object. end note] 52

Properties can be used to delay initialization of a resource until the moment it is first referenced. [Example: 53

using System.IO; 54

Page 316: C# Language Specification

C# LANGUAGE SPECIFICATION

296

public class Console 1 { 2 private static TextReader reader; 3 private static TextWriter writer; 4 private static TextWriter error; 5

public static TextReader In { 6 get { 7 if (reader == null) { 8 reader = new StreamReader(Console.OpenStandardInput()); 9 } 10 return reader; 11 } 12 } 13

public static TextWriter Out { 14 get { 15 if (writer == null) { 16 writer = new StreamWriter(Console.OpenStandardOutput()); 17 } 18 return writer; 19 } 20 } 21

public static TextWriter Error { 22 get { 23 if (error == null) { 24 error = new StreamWriter(Console.OpenStandardError()); 25 } 26 return error; 27 } 28 } 29 … 30 } 31

The Console class contains three properties, In, Out, and Error, that represent the standard input, output, 32 and error devices, respectively. By exposing these members as properties, the Console class can delay their 33 initialization until they are actually used. For example, upon first referencing the Out property, as in 34

Console.Out.WriteLine("hello, world"); 35

the underlying TextWriter for the output device is created. But if the application makes no reference to the 36 In and Error properties, then no objects are created for those devices. end example] 37

The presence of an accessor-modifier never affects member lookup (§14.3) or overload resolution (§14.4.2). 38 The modifiers on the property or indexer always determine which property or indexer is bound to, regardless 39 of the context of the access. 40

Once a particular property or indexer has been selected, the accessibility domains of the specific accessors 41 involved are used to determine if that usage is valid: 42

• If the usage is as a value (§14.1.1), the get accessor shall exist and be accessible. 43

• If the usage is as the target of a simple assignment (§14.13.1), the set accessor shall exist and be 44 accessible. 45

• If the usage is as the target of compound assignment (§14.13.2), or as the target of the ++ or -- 46 operators (§14.5.9, §14.6.5), both the get accessors and the set accessor shall exist and be accessible. 47

[Example: In the following example, the property A.Text is hidden by the property B.Text, even in 48 contexts where only the set accessor is called. In contrast, the property B.Count is not accessible to class 49 M, so the accessible property A.Count is used instead. 50

class A 51 { 52 public string Text { 53 get { return "hello"; } 54 set { } 55 } 56

Page 317: C# Language Specification

Chapter 17 Classes

297

public int Count { 1 get { return 5; } 2 set { } 3 } 4 } 5

class B: A 6 { 7 private string text = "goodbye"; 8 private int count = 0; 9

new public string Text { 10 get { return text; } 11 protected set { text = value; } 12 } 13

new protected int Count { 14 get { return count; } 15 set { count = value; } 16 } 17 } 18

class M 19 { 20 static void Main() { 21 B b = new B(); 22 b.Count = 12; // Calls A.Count set accessor 23 int i = b.Count; // Calls A.Count get accessor 24 b.Text = "howdy"; // Error, B.Text set accessor not accessible 25 string s = b.Text; // Calls B.Text get accessor 26 } 27 } 28

end example] 29

An accessor that is used to implement an interface cannot have an accessor-modifier. However, if only one 30 accessor is used to implement an interface, the other accessor can be declared with an accessor-modifier: 31 [Example: 32

public interface I 33 { 34 string Prop { get; } 35 } 36

public class C: I 37 { 38 public string Prop { 39 get { return "April"; } // Shall not have a modifier here 40 internal set {…} // Ok, because I.Prop has no set accessor 41 } 42 } 43

end example] 44

17.6.3 Virtual, sealed, override, and abstract accessors 45

A virtual property declaration specifies that the accessors of the property are virtual. The virtual 46 modifier applies to all non-private accessors of a property. When an accessor of a virtual property has the 47 private accessor-modifier, the private accessor is implicitly not virtual. 48

An abstract property declaration specifies that the accessors of the property are virtual, but does not 49 provide an actual implementation of the accessors. Instead, non-abstract derived classes are required to 50 provide their own implementation for the accessors by overriding the property. Because an accessor for an 51 abstract property declaration provides no actual implementation, its accessor-body simply consists of a 52 semicolon. An abstract property shall not have a private accessor. 53

A property declaration that includes both the abstract and override modifiers specifies that the property 54 is abstract and overrides a base property. The accessors of such a property are also abstract. 55

Page 318: C# Language Specification

C# LANGUAGE SPECIFICATION

298

Abstract property declarations are only permitted in abstract classes (§17.1.1.1). The accessors of an 1 inherited virtual property can be overridden in a derived class by including a property declaration that 2 specifies an override directive. This is known as an overriding property declaration. An overriding 3 property declaration does not declare a new property. Instead, it simply specializes the implementations of 4 the accessors of an existing virtual property. 5

An overriding property declaration shall specify the exact same accessibility modifiers, type, and name as 6 the inherited property. If the inherited property has only a single accessor (i.e., if the inherited property is 7 read-only or write-only), the overriding property shall include only that accessor. If the inherited property 8 includes both accessors (i.e., if the inherited property is read-write), the overriding property can include 9 either a single accessor or both accessors. If one of the inherited accessors is not accessible to the overriding 10 type, the overriding property shall not include that accessor. In addition, the declared accessibility of the 11 accessors shall match that of the overridden accessors. [Example:For example: 12

public class B 13 { 14 public virtual int P { 15 protected set {…} 16 get {…} 17 } 18 public virtual int Q { 19 private set {…} // not virtual 20 get {…} 21 } 22 } 23

public class D: B 24 { 25 public override int P { 26 protected set {…} // Must specify protected here 27 get {…} // Must not have a modifier here 28 } 29 public override int Q { 30 private set {…} // Error: inherited set is not accessible 31 get {…} // Must not have a modifier here 32 } 33 } 34

end example] 35

An overriding property declaration can include the sealed modifier. Use of this modifier prevents a 36 derived class from further overriding the property. The accessors of a sealed property are also sealed. 37

Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessors 38 behave exactly like virtual, sealed, override and abstract methods. Specifically, the rules described in 39 §17.5.3, §17.5.4, §17.5.5, and §17.5.6 apply as if accessors were methods of a corresponding form: 40

• A get accessor corresponds to a parameterless method with a return value of the property type and the 41 same modifiers as the containing property. 42

• A set accessor corresponds to a method with a single value parameter of the property type, a void 43 return type, and the same modifiers as the containing property. 44

[Example: In the following code 45

abstract class A 46 { 47 int y; 48

public virtual int X { 49 get { return 0; } 50 } 51

public virtual int Y { 52 get { return y; } 53 set { y = value; } 54 } 55

Page 319: C# Language Specification

Chapter 17 Classes

299

public abstract int Z { get; set; } 1 } 2

X is a virtual read-only property, Y is a virtual read-write property, and Z is an abstract read-write property. 3 Because Z is abstract, the containing class A shall also be declared abstract. 4

A class that derives from A is show below: 5

class B: A 6 { 7 int z; 8

public override int X { 9 get { return base.X + 1; } 10 } 11

public override int Y { 12 set { base.Y = value < 0? 0: value; } 13 } 14

public override int Z { 15 get { return z; } 16 set { z = value; } 17 } 18 } 19

Here, the declarations of X, Y, and Z are overriding property declarations. Each property declaration exactly 20 matches the accessibility modifiers, type, and name of the corresponding inherited property. The get 21 accessor of X and the set accessor of Y use the base keyword to access the inherited accessors. The 22 declaration of Z overrides both abstract accessors—thus, there are no outstanding abstract function members 23 in B, and B is permitted to be a non-abstract class. end example] 24

17.7 Events 25

An event is a member that enables an object or class to provide notifications. Clients can attach executable 26 code for events by supplying event handlers. 27

Events are declared using event-declarations: 28

event-declaration: 29 attributesopt event-modifiersopt event type variable-declarators ; 30 attributesopt event-modifiersopt event type member-name 31 { event-accessor-declarations } 32

event-modifiers: 33 event-modifier 34 event-modifiers event-modifier 35

event-modifier: 36 new 37 public 38 protected 39 internal 40 private 41 static 42 virtual 43 sealed 44 override 45 abstract 46 extern 47

event-accessor-declarations: 48 add-accessor-declaration remove-accessor-declaration 49 remove-accessor-declaration add-accessor-declaration 50

Page 320: C# Language Specification

C# LANGUAGE SPECIFICATION

300

add-accessor-declaration: 1 attributesopt add block 2

remove-accessor-declaration: 3 attributesopt remove block 4

An event-declaration can include a set of attributes (§24) and a valid combination of the four access 5 modifiers (§17.2.3), the new (§17.2.2), static (§17.5.2, §17.7.3), virtual (§17.5.3, §17.7.4), override 6 (§17.5.4, §17.7.4), sealed (§17.5.5), abstract (§17.5.6, §17.7.4), and extern modifiers. 7

Event declarations are subject to the same rules as method declarations (§17.5) with regard to valid 8 combinations of modifiers. 9

The type of an event declaration shall be a delegate-type (§11.2), and that delegate-type shall be at least as 10 accessible as the event itself (§10.5.4). 11

An event declaration can include event-accessor-declarations. However, if it does not, for non-extern, non-12 abstract events, the compiler shall supply them automatically (§17.7.1); for extern events, the accessors are 13 provided externally. 14

An event declaration that omits event-accessor-declarations defines one or more events—one for each of the 15 variable-declarators. The attributes and modifiers apply to all of the members declared by such an event-16 declaration. 17

It is a compile-time error for an event-declaration to include both the abstract modifier and variable-18 initializers or brace-delimited event-accessor-declarations. 19

When an event declaration includes an extern modifier, the event is said to be an external event. Because 20 an external event declaration provides no actual implementation, it is an error for it to include both the 21 extern modifier and event-accessor-declarations. 22

An event can be used as the left-hand operand of the += and -= operators (§14.13.3). These operators are 23 used, respectively, to attach event handlers to, or to remove event handlers from an event, and the access 24 modifiers of the event control the contexts in which such operations are permitted. 25

The only operations that are permitted on an event by code that is outside the type in which that event is 26 declared, are += and -=. So while such code can add and remove handlers for an event, it cannot directly 27 obtain or modify the underlying list of event handlers. In an operation of the form x += y or x –= y, when 28 x is an event and the reference takes place outside the type that contains the declaration of x, the result of the 29 operation has type void (as opposed to having the type of x, with the value of x after the assignment). This 30 rule prohibits external code from indirectly examining the underlying delegate of an event. 31

[Example: The following example shows how event handlers are attached to instances of the Button class: 32

public delegate void EventHandler(object sender, EventArgs e); 33

public class Button: Control 34 { 35 public event EventHandler Click; 36 } 37

public class LoginDialog: Form 38 { 39 Button OkButton; 40 Button CancelButton; 41

public LoginDialog() { 42 OkButton = new Button(…); 43 OkButton.Click += OkButtonClick; 44 CancelButton = new Button(…); 45 CancelButton.Click += CancelButtonClick; 46 } 47

void OkButtonClick(object sender, EventArgs e) { 48 // Handle OkButton.Click event 49 } 50

Page 321: C# Language Specification

Chapter 17 Classes

301

void CancelButtonClick(object sender, EventArgs e) { 1 // Handle CancelButton.Click event 2 } 3 } 4

Here, the LoginDialog instance constructor creates two Button instances and attaches event handlers to 5 the Click events. end example] 6

17.7.1 Field-like events 7

Within the program text of the class or struct that contains the declaration of an event, certain events can be 8 used like fields. To be used in this way, an event shall not be abstract or extern, and shall not explicitly 9 include event-accessor-declarations. Such an event can be used in any context that permits a field. The field 10 contains a delegate (§22), which refers to the list of event handlers that have been added to the event. If no 11 event handlers have been added, the field contains null. 12

[Example: In the following code 13

public delegate void EventHandler(object sender, EventArgs e); 14

public class Button: Control 15 { 16 public event EventHandler Click; 17

protected void OnClick(EventArgs e) { 18 EventHandler toRaise = Click; 19 if (toRaise != null) 20 toRaise(this, e); 21 } 22

public void Reset() { 23 Click = null; 24 } 25 } 26

Click is used as a field within the Button class. As the example demonstrates, the field can be examined, 27 modified, and used in delegate invocation expressions. The OnClick method in the Button class “raises” 28 the Click event. The notion of raising an event is precisely equivalent to invoking the delegate represented 29 by the event—thus, there are no special language constructs for raising events. Note that the delegate 30 invocation is preceded by a check that ensures the delegate is non-null and that the check is made on a local 31 copy to ensure thread safety. 32

Outside the declaration of the Button class, the Click member can only be used on the left-hand side of 33 the += and –= operators, as in 34

b.Click += new EventHandler(…); 35

which appends a delegate to the invocation list of the Click event, and 36

b.Click –= new EventHandler(…); 37

which removes a delegate from the invocation list of the Click event. end example] 38

When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and 39 creates accessors for the event that add or remove event handlers to the delegate field. In order to be thread-40 safe, the addition or removal operations are done while holding the lock (§15.12) on the containing object 41 for an instance event, or the type object (§14.5.11) for a static event. 42

[Note: Thus, an instance event declaration of the form: 43

class X 44 { 45 public event D Ev; 46 } 47

shall be compiled to something equivalent to: 48

class X 49 { 50 private D __Ev; // field to hold the delegate 51

Page 322: C# Language Specification

C# LANGUAGE SPECIFICATION

302

public event D Ev { 1 add { 2 lock(this) { __Ev = __Ev + value; } 3 } 4

remove { 5 lock(this) { __Ev = __Ev - value; } 6 } 7 } 8 } 9

Within the class X, references to Ev are compiled to reference the hidden field __Ev instead. The name 10 “__Ev” is arbitrary; the hidden field could have any name or no name at all. 11

Similarly, a static event declaration of the form: 12

class X 13 { 14 public static event D Ev; 15 } 16

shall be compiled to something equivalent to: 17

class X 18 { 19 private static D __Ev; // field to hold the delegate 20

public static event D Ev { 21 add { 22 lock(typeof(X)) { __Ev = __Ev + value; } 23 } 24

remove { 25 lock(typeof(X)) { __Ev = __Ev - value; } 26 } 27 } 28 } 29

end note] 30

17.7.2 Event accessors 31

[Note: Event declarations typically omit event-accessor-declarations, as in the Button example above. One 32 situation for doing so involves the case in which the storage cost of one field per event is not acceptable. In 33 such cases, a class can include event-accessor-declarations and use a private mechanism for storing the list 34 of event handlers. Similarly, in cases where the handling of an event requires access to external resources, 35 event accessors can be used to manage these resources. end note] 36

The event-accessor-declarations of an event specify the executable statements associated with adding and 37 removing event handlers. 38

The accessor declarations consist of an add-accessor-declaration and a remove-accessor-declaration. Each 39 accessor declaration consists of the token add or remove followed by a block. The block associated with an 40 add-accessor-declaration specifies the statements to execute when an event handler is added, and the block 41 associated with a remove-accessor-declaration specifies the statements to execute when an event handler is 42 removed. 43

Each add-accessor-declaration and remove-accessor-declaration corresponds to a method with a single 44 value parameter of the event type, and a void return type. The implicit parameter of an event accessor is 45 named value. When an event is used in an event assignment, the appropriate event accessor is used. 46 Specifically, if the assignment operator is += then the add accessor is used, and if the assignment operator is 47 –= then the remove accessor is used. In either case, the right-hand operand of the assignment operator is 48 used as the argument to the event accessor. The block of an add-accessor-declaration or a remove-accessor-49 declaration shall conform to the rules for void methods described in §17.5.8. In particular, return 50 statements in such a block are not permitted to specify an expression. 51

Page 323: C# Language Specification

Chapter 17 Classes

303

Since an event accessor implicitly has a parameter named value, it is a compile-time error for a local 1 variable or constant declared in an event accessor to have that name. 2

[Example: In the following code 3

class Control: Component 4 { 5 // Unique keys for events 6 static readonly object mouseDownEventKey = new object(); 7 static readonly object mouseUpEventKey = new object(); 8

// Return event handler associated with key 9 protected Delegate GetEventHandler(object key) {…} 10

// Add event handler associated with key 11 protected void AddEventHandler(object key, Delegate handler) {…} 12

// Remove event handler associated with key 13 protected void RemoveEventHandler(object key, Delegate handler) {…} 14

// MouseDown event 15 public event MouseEventHandler MouseDown { 16 add { AddEventHandler(mouseDownEventKey, value); } 17 remove { RemoveEventHandler(mouseDownEventKey, value); } 18 } 19

// MouseUp event 20 public event MouseEventHandler MouseUp { 21 add { AddEventHandler(mouseUpEventKey, value); } 22 remove { RemoveEventHandler(mouseUpEventKey, value); } 23 } 24

// Invoke the MouseUp event 25 protected void OnMouseUp(MouseEventArgs args) { 26 MouseEventHandler handler; 27 handler = (MouseEventHandler)GetEventHandler(mouseUpEventKey); 28 if (handler != null) 29 handler(this, args); 30 } 31 } 32

the Control class implements an internal storage mechanism for events. The AddEventHandler method 33 associates a delegate value with a key, the GetEventHandler method returns the delegate currently 34 associated with a key, and the RemoveEventHandler method removes a delegate as an event handler for 35 the specified event. Presumably, the underlying storage mechanism is designed such that there is no cost for 36 associating a null delegate value with a key, and thus unhandled events consume no storage. end example] 37

17.7.3 Static and instance events 38

When an event declaration includes a static modifier, the event is said to be a static event. When no 39 static modifier is present, the event is said to be an instance event. 40

A static event is not associated with a specific instance, and it is a compile-time error to refer to this in the 41 accessors of a static event. 42

An instance event is associated with a given instance of a class, and this instance can be accessed as this 43 (§14.5.7) in the accessors of that event. 44

When an event is referenced in a member-access (§14.5.4) of the form E.M, if M is a static event, E shall 45 denote a type, and if M is an instance event, E shall denote an instance. 46

The differences between static and instance members are discussed further in §17.2.5. 47

17.7.4 Virtual, sealed, override, and abstract accessors 48

A virtual event declaration specifies that the accessors of that event are virtual. The virtual modifier 49 applies to both accessors of an event. 50

Page 324: C# Language Specification

C# LANGUAGE SPECIFICATION

304

An abstract event declaration specifies that the accessors of the event are virtual, but does not provide an 1 actual implementation of the accessors. Instead, non-abstract derived classes are required to provide their 2 own implementation for the accessors by overriding the event. Because an accessor for an abstract event 3 declaration provides no actual implementation, its accessor-body simply consists of a semicolon. 4

An event declaration that includes both the abstract and override modifiers specifies that the event is 5 abstract and overrides a base event. The accessors of such an event are also abstract. 6

Abstract event declarations are only permitted in abstract classes (§17.1.1.1). 7

The accessors of an inherited virtual event can be overridden in a derived class by including an event 8 declaration that specifies an override modifier. This is known as an overriding event declaration. An 9 overriding event declaration does not declare a new event. Instead, it simply specializes the implementations 10 of the accessors of an existing virtual event. 11

An overriding event declaration shall specify the exact same accessibility modifiers, type, and name as the 12 overridden event. 13

An overriding event declaration can include the sealed modifier. Use of this modifier prevents a derived 14 class from further overriding the event. The accessors of a sealed event are also sealed. 15

It is a compile-time error for an overriding event declaration to include a new modifier. 16

Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessors 17 behave exactly like virtual, sealed, override and abstract methods. Specifically, the rules described in 18 §17.5.3, §17.5.4, §17.5.5, and §17.5.6 apply as if accessors were methods of a corresponding form. Each 19 accessor corresponds to a method with a single value parameter of the event type, a void return type, and 20 the same modifiers as the containing event. 21

17.8 Indexers 22

An indexer is a member that enables an object to be indexed similar to an array. Indexers are declared using 23 indexer-declarations: 24

indexer-declaration: 25 attributesopt indexer-modifiersopt indexer-declarator { accessor-declarations } 26

indexer-modifiers: 27 indexer-modifier 28 indexer-modifiers indexer-modifier 29

indexer-modifier: 30 new 31 public 32 protected 33 internal 34 private 35 virtual 36 sealed 37 override 38 abstract 39 extern 40

indexer-declarator: 41 type this [ formal-parameter-list ] 42 type interface-type . this [ formal-parameter-list ] 43

An indexer-declaration can include a set of attributes (§24) and a valid combination of the four access 44 modifiers (§17.2.3), the new (§17.2.2), virtual (§17.5.3), override (§17.5.4), sealed (§17.5.5), 45 abstract (§17.5.6), and extern (§17.5.7) modifiers. 46

Page 325: C# Language Specification

Chapter 17 Classes

305

Indexer declarations are subject to the same rules as method declarations (§17.5) with regard to valid 1 combinations of modifiers, with the one exception being that the static modifier is not permitted on an 2 indexer declaration. 3

The modifiers virtual, override, and abstract are mutually exclusive except in one case. The 4 abstract and override modifiers can be used together so that an abstract indexer can override a virtual 5 one. 6

The type of an indexer declaration specifies the element type of the indexer introduced by the declaration. 7 Unless the indexer is an explicit interface member implementation, the type is followed by the keyword 8 this. For an explicit interface member implementation, the type is followed by an interface-type, a “.”, and 9 the keyword this. Unlike other members, indexers do not have user-defined names. 10

The formal-parameter-list specifies the parameters of the indexer. The formal parameter list of an indexer 11 corresponds to that of a method (§17.5.1), except that at least one parameter shall be specified, and that the 12 ref and out parameter modifiers are not permitted. 13

The type of an indexer and each of the types referenced in the formal-parameter-list shall be at least as 14 accessible as the indexer itself (§10.5.4). 15

The accessor-declarations (§17.6.2), which shall be enclosed in “{” and “}” tokens, declare the accessors 16 of the indexer. The accessors specify the executable statements associated with reading and writing indexer 17 elements. 18

Even though the syntax for accessing an indexer element is the same as that for an array element, an indexer 19 element is not classified as a variable. Thus, it is not possible to pass an indexer element as a ref or out 20 argument. 21

The formal-parameter-list of an indexer defines the signature (§10.6) of the indexer. Specifically, the 22 signature of an indexer consists of the number and types of its formal parameters. The element type and 23 names of the formal parameters are not part of an indexer’s signature. 24

The signature of an indexer shall differ from the signatures of all other indexers declared in the same class. 25

Indexers and properties are very similar in concept, but differ in the following ways: 26

• A property is identified by its name, whereas an indexer is identified by its signature. 27

• A property is accessed through a simple-name (§14.5.2) or a member-access (§14.5.4), whereas an 28 indexer element is accessed through an element-access (§14.5.6.2). 29

• A property can be a static member, whereas an indexer is always an instance member. 30

• A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of an 31 indexer corresponds to a method with the same formal parameter list as the indexer. 32

• A set accessor of a property corresponds to a method with a single parameter named value, whereas a 33 set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, 34 plus an additional parameter named value. 35

• It is a compile-time error for an indexer accessor to declare a local variable or local constant with the 36 same name as an indexer parameter. 37

• In an overriding property declaration, the inherited property is accessed using the syntax base.P, where 38 P is the property name. In an overriding indexer declaration, the inherited indexer is accessed using the 39 syntax base[E], where E is a comma-separated list of expressions. 40

Aside from these differences, all rules defined in §17.6.2 and §17.6.3 apply to indexer accessors as well as to 41 property accessors. 42

When an indexer declaration includes an extern modifier, the indexer is said to be an external indexer. 43 Because an external indexer declaration provides no actual implementation, each of its accessor-44 declarations consists of a semicolon. 45

Page 326: C# Language Specification

C# LANGUAGE SPECIFICATION

306

[Example: The example below declares a BitArray class that implements an indexer for accessing the 1 individual bits in the bit array. 2

using System; 3

class BitArray 4 { 5 int[] bits; 6 int length; 7

public BitArray(int length) { 8 if (length < 0) throw new ArgumentException(); 9 bits = new int[((length - 1) >> 5) + 1]; 10 this.length = length; 11 } 12

public int Length { 13 get { return length; } 14 } 15

public bool this[int index] { 16 get { 17 if (index < 0 || index >= length) { 18 throw new IndexOutOfRangeException(); 19 } 20 return (bits[index >> 5] & 1 << index) != 0; 21 } 22 set { 23 if (index < 0 || index >= length) { 24 throw new IndexOutOfRangeException(); 25 } 26 if (value) { 27 bits[index >> 5] |= 1 << index; 28 } 29 else { 30 bits[index >> 5] &= ~(1 << index); 31 } 32 } 33 } 34 } 35

An instance of the BitArray class consumes substantially less memory than a corresponding bool[] 36 (since each value of the former occupies only one bit instead of the latter’s one byte), but it permits the same 37 operations as a bool[]. 38

The following CountPrimes class uses a BitArray and the classical “sieve” algorithm to compute the 39 number of primes between 2 and a given maximum: 40

class CountPrimes 41 { 42 static int Count(int max) { 43 BitArray flags = new BitArray(max + 1); 44 int count = 0; 45 for (int i = 2; i <= max; i++) { 46 if (!flags[i]) { 47 for (int j = i * 2; j <= max; j += i) flags[j] = true; 48 count++; 49 } 50 } 51 return count; 52 } 53

static void Main(string[] args) { 54 int max = int.Parse(args[0]); 55 int count = Count(max); 56 Console.WriteLine( 57 "Found {0} primes between 2 and {1}", count, max); 58 } 59 } 60

Page 327: C# Language Specification

Chapter 17 Classes

307

Note that the syntax for accessing elements of the BitArray is precisely the same as for a bool[]. end 1 example] 2

[Example: The following example shows a 26×10 grid class that has an indexer with two parameters. The 3 first parameter is required to be an upper- or lowercase letter in the range A–Z, and the second is required to 4 be an integer in the range 0–9. 5

using System; 6

class Grid 7 { 8 const int NumRows = 26; 9 const int NumCols = 10; 10 int[,] cells = new int[NumRows, NumCols]; 11

12 public int this[char c, int colm] 13 { 14 get { 15 c = Char.ToUpper(c); 16 if (c < 'A' || c > 'Z') { 17 throw new ArgumentException(); 18 } 19 if (colm < 0 || colm >= NumCols) { 20 throw new IndexOutOfRangeException(); 21 } 22 return cells[c - 'A', colm]; 23 } 24

set { 25 c = Char.ToUpper(c); 26 if (c < 'A' || c > 'Z') { 27 throw new ArgumentException(); 28 } 29 if (colm < 0 || colm >= NumCols) { 30 throw new IndexOutOfRangeException(); 31 } 32 cells[c - 'A', colm] = value; 33 } 34 } 35 } 36

end example] 37

17.8.1 Indexer overloading 38

The indexer overload resolution rules are described in §14.4.2. 39

17.9 Operators 40

An operator is a member that defines the meaning of an expression operator that can be applied to instances 41 of the class. Operators are declared using operator-declarations: 42

operator-declaration: 43 attributesopt operator-modifiers operator-declarator operator-body 44

operator-modifiers: 45 operator-modifier 46 operator-modifiers operator-modifier 47

operator-modifier: 48 public 49 static 50 extern 51

Page 328: C# Language Specification

C# LANGUAGE SPECIFICATION

308

operator-declarator: 1 unary-operator-declarator 2 binary-operator-declarator 3 conversion-operator-declarator 4

unary-operator-declarator: 5 type operator overloadable-unary-operator ( type identifier ) 6

overloadable-unary-operator: one of 7 + - ! ~ ++ -- true false 8

binary-operator-declarator: 9 type operator overloadable-binary-operator ( type identifier , type identifier ) 10

overloadable-binary-operator: one of 11 + - * / % 12 & | ^ 13 << right-shift 14 == != > < >= <= 15

conversion-operator-declarator: 16 implicit operator type ( type identifier ) 17 explicit operator type ( type identifier ) 18

operator-body: 19 block 20 ; 21

There are three categories of overloadable operators: Unary operators (§17.9.1), binary operators (§17.9.2), 22 and conversion operators (§17.9.3). 23

When an operator declaration includes an extern modifier, the operator is said to be an external operator. 24 Because an external operator provides no actual implementation, its operator-body consists of a semi-colon. 25 For all other operators, the operator-body consists of a block, which specifies the statements to execute 26 when the operator is invoked. The block of an operator shall conform to the rules for value-returning 27 methods described in §17.5.8. 28

The following rules apply to all operator declarations: 29

• An operator declaration shall include both a public and a static modifier. 30

• The parameter(s) of an operator shall be value parameters. It is a compile-time error for an operator 31 declaration to specify ref or out parameters. 32

• The signature of an operator (§17.9.1, §17.9.2, §17.9.3) shall differ from the signatures of all other 33 operators declared in the same class. 34

• All types referenced in an operator declaration shall be at least as accessible as the operator itself 35 (§10.5.4). 36

• It is an error for the same modifier to appear multiple times in an operator declaration. 37

Each operator category imposes additional restrictions, as described in the following subclauses. 38

Like other members, operators declared in a base class are inherited by derived classes. Because operator 39 declarations always require the class or struct in which the operator is declared to participate in the signature 40 of the operator, it is not possible for an operator declared in a derived class to hide an operator declared in a 41 base class. Thus, the new modifier is never required, and therefore never permitted, in an operator 42 declaration. 43

Additional information on unary and binary operators can be found in §14.2. 44

Additional information on conversion operators can be found in §13.4. 45

Page 329: C# Language Specification

Chapter 17 Classes

309

17.9.1 Unary operators 1

The following rules apply to unary operator declarations, where T denotes the class or struct type that 2 contains the operator declaration: 3

• A unary +, -, !, or ~ operator shall take a single parameter of type T and can return any type. 4

• A unary ++ or -- operator shall take a single parameter of type T and shall return type T or a type 5 derived from T. 6

• A unary true or false operator shall take a single parameter of type T and shall return type bool. 7

The signature of a unary operator consists of the operator token (+, -, !, ~, ++, --, true, or false) and the 8 type of the single formal parameter. The return type is not part of a unary operator’s signature, nor is the 9 name of the formal parameter. 10

The true and false unary operators require pair-wise declaration. A compile-time error occurs if a class 11 declares one of these operators without also declaring the other. The true and false operators are 12 described further in §14.16. 13

[Example: The following example shows an implementation and subsequent usage of operator++ for an 14 integer vector class: 15

public class IntVector 16 { 17 public int Length { … } // read-only property 18 public int this[int index] { … } // read-write indexer 19 public IntVector(int vectorLength) { … } 20 public static IntVector operator++(IntVector iv) { 21 IntVector temp = new IntVector(iv.Length); 22 for (int i = 0; i < iv.Length; ++i) 23 temp[i] = iv[i] + 1; 24 return temp; 25 } 26 } 27

class Test 28 { 29 static void Main() { 30 IntVector iv1 = new IntVector(4); // vector of 4x0 31 IntVector iv2; 32 33 iv2 = iv1++; // iv2 contains 4x0, iv1 contains 4x1 34 iv2 = ++iv1; // iv2 contains 4x2, iv1 contains 4x2 35 } 36 } 37

Note how the operator method returns the value produced by adding 1 to the operand, just like the postfix 38 increment and decrement operators (§14.5.9), and the prefix increment and decrement operators (§14.6.5). 39 Unlike in C++, this method need not, and, in fact, should not, modify the value of its operand directly. end 40 example] 41

17.9.2 Binary operators 42

A binary operator shall take two parameters, at least one of which shall have the class or struct type in which 43 the operator is declared. The shift operators (§14.8) are further constrained: The type of the first parameter 44 shall be the class or struct type in which the operator is declared, and the second parameter shall always have 45 the type int. A binary operator can return any type. 46

The signature of a binary operator consists of the operator token (+, -, *, /, %, &, |, ^, <<, right-shift, ==, 47 !=, >, <, >=, or <=) and the types of the two formal parameters. The return type and the names of the formal 48 parameters are not part of a binary operator’s signature. 49

Certain binary operators require pair-wise declaration. For every declaration of either operator of a pair, 50 there shall be a matching declaration of the other operator of the pair. Two operator declarations match when 51

Page 330: C# Language Specification

C# LANGUAGE SPECIFICATION

310

they have the same return type and the same type for each parameter. The following operators require pair-1 wise declaration: 2

• operator == and operator != 3

• operator > and operator < 4

• operator >= and operator <= 5

17.9.3 Conversion operators 6

A conversion operator declaration introduces a user-defined conversion operator (§13.4), which augments 7 the pre-defined implicit and explicit conversions. 8

A conversion operator declaration that includes the implicit keyword introduces a user-defined implicit 9 conversion operator. Implicit conversions can occur in a variety of situations, including function member 10 invocations, cast expressions, and assignments. This is described further in §13.1. 11

A conversion operator declaration that includes the explicit keyword introduces a user-defined explicit 12 conversion operator. Explicit conversions can occur in cast expressions, and are described further in §13.2. 13

A conversion operator converts from a source type, indicated by the parameter type of the conversion 14 operator, to a target type, indicated by the return type of the conversion operator. A class or struct is 15 permitted to declare a conversion operator from a source type S to a target type T provided all of the 16 following are true: 17

• S and T are different types. 18

• Either S or T is the class or struct type in which the operator declaration takes place. 19

• Neither S nor T is object or an interface-type. 20

• T is not a base class of S, and S is not a base class of T. 21

From the second rule it follows that a conversion operator shall convert either to or from the class or struct 22 type in which the operator is declared. [Example: It is possible for a class or struct type C to define a 23 conversion from C to int and from int to C, but not from int to bool. end example] 24

It is not possible to redefine a pre-defined conversion. Thus, conversion operators are not allowed to convert 25 from or to object because implicit and explicit conversions already exist between object and all other 26 types. Likewise, neither the source nor the target types of a conversion can be a base type of the other, since 27 a conversion would then already exist. 28

User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction 29 ensures that no user-defined transformations occur when converting to an interface-type, and that a 30 conversion to an interface-type succeeds only if the object being converted actually implements the specified 31 interface-type. 32

The signature of a conversion operator consists of the source type and the target type. (This is the only form 33 of member for which the return type participates in the signature.) The implicit or explicit 34 classification of a conversion operator is not part of the operator’s signature. Thus, a class or struct cannot 35 declare both an implicit and an explicit conversion operator with the same source and target types. 36

[Note: In general, user-defined implicit conversions should be designed to never throw exceptions and never 37 lose information. If a user-defined conversion can give rise to exceptions (for example, because the source 38 argument is out of range) or loss of information (such as discarding high-order bits), then that conversion 39 should be defined as an explicit conversion. end note] 40

[Example: In the following code 41

using System; 42

public struct Digit 43 { 44 byte value; 45

Page 331: C# Language Specification

Chapter 17 Classes

311

public Digit(int value) { 1 if (value < 0 || value > 9) throw new ArgumentException(); 2 this.value = value; 3 } 4

public static implicit operator byte(Digit d) { 5 return d.value; 6 } 7

public static explicit operator Digit(int n) { 8 return new Digit(n); 9 } 10 } 11

the conversion operator from Digit to byte is implicit because it never throws exceptions or loses 12 information, but the conversion operator from int to Digit is explicit since Digit can only represent a 13 subset of the possible values of an int. end example] 14

17.10 Instance constructors 15

An instance constructor is a member that implements the actions required to initialize an instance of a class. 16 Instance constructors are declared using constructor-declarations: 17

constructor-declaration: 18 attributesopt constructor-modifiersopt constructor-declarator constructor-body 19

constructor-modifiers: 20 constructor-modifier 21 constructor-modifiers constructor-modifier 22

constructor-modifier: 23 public 24 protected 25 internal 26 private 27 extern 28

constructor-declarator: 29 identifier ( formal-parameter-listopt ) constructor-initializeropt 30

constructor-initializer: 31 : base ( argument-listopt ) 32 : this ( argument-listopt ) 33

constructor-body: 34 block 35 ; 36

A constructor-declaration can include a set of attributes (§24), a valid combination of the four access 37 modifiers (§17.2.3), and an extern (§17.5.7) modifier. A constructor declaration is not permitted to include 38 the same modifier multiple times. 39

The identifier of a constructor-declarator shall name the class in which the instance constructor is declared. 40 If any other name is specified, a compile-time error occurs. 41

The optional formal-parameter-list of an instance constructor is subject to the same rules as the formal-42 parameter-list of a method (§17.5). The formal parameter list defines the signature (§10.6) of an instance 43 constructor and governs the process whereby overload resolution (§14.4.2) selects a particular instance 44 constructor in an invocation. 45

Each of the types referenced in the formal-parameter-list of an instance constructor shall be at least as 46 accessible as the constructor itself (§10.5.4). 47

The optional constructor-initializer specifies another instance constructor to invoke before executing the 48 statements given in the constructor-body of this instance constructor. This is described further in §17.10.1. 49

Page 332: C# Language Specification

C# LANGUAGE SPECIFICATION

312

When a constructor declaration includes an extern modifier, the constructor is said to be an external 1 constructor. 2

Because an external constructor declaration provides no actual implementation, its constructor-body consists 3 of a semicolon. For all other constructors, the constructor-body consists of a block, which specifies the 4 statements to initialize a new instance of the class. This corresponds exactly to the block of an instance 5 method with a void return type (§17.5.8). 6

Instance constructors are not inherited. Thus, a class has no instance constructors other than those actually 7 declared in the class. If a class contains no instance constructor declarations, a default instance constructor is 8 automatically provided (§17.10.4). 9

Instance constructors are invoked by object-creation-expressions (§14.5.10.1) and through constructor-10 initializers. 11

17.10.1 Constructor initializers 12

All instance constructors (except those for class object) implicitly include an invocation of another 13 instance constructor immediately before the constructor-body. The constructor to implicitly invoke is 14 determined by the constructor-initializer: 15

• An instance constructor initializer of the form base(argument-listopt) causes an instance constructor 16 from the direct base class to be invoked. That constructor is selected using argument-list and the 17 overload resolution rules of §14.4.2. The set of candidate instance constructors consists of all accessible 18 instance constructors declared in the direct base class, or the default constructor (§17.10.4), if no 19 instance constructors are declared in the direct base class. If this set is empty, or if a single best instance 20 constructor cannot be identified, a compile-time error occurs. 21

• An instance constructor initializer of the form this(argument-listopt) causes an instance constructor 22 from the class itself to be invoked. The constructor is selected using argument-list and the overload 23 resolution rules of §14.4.2. The set of candidate instance constructors consists of all accessible instance 24 constructors declared in the class itself. If that set is empty, or if a single best instance constructor cannot 25 be identified, a compile-time error occurs. If an instance constructor declaration includes a constructor 26 initializer that invokes the constructor itself, a compile-time error occurs. 27

If an instance constructor has no constructor initializer, a constructor initializer of the form base() is 28 implicitly provided. [Note: Thus, an instance constructor declaration of the form 29

C(…) {…} 30

is exactly equivalent to 31

C(…): base() {…} 32

end note] 33

The scope of the parameters given by the formal-parameter-list of an instance constructor declaration 34 includes the constructor initializer of that declaration. Thus, a constructor initializer is permitted to access 35 the parameters of the constructor. [Example: 36

class A 37 { 38 public A(int x, int y) {} 39 } 40

class B: A 41 { 42 public B(int x, int y): base(x + y, x - y) {} 43 } 44

end example] 45

An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time 46 error to reference this in an argument expression of the constructor initializer, as it is a compile-time error 47 for an argument expression to reference any instance member through a simple-name. 48

Page 333: C# Language Specification

Chapter 17 Classes

313

17.10.2 Instance variable initializers 1

When an instance constructor has no constructor initializer, or it has a constructor initializer of the form 2 base(…), that constructor implicitly performs the initializations specified by the variable-initializers of the 3 instance fields declared in its class. This corresponds to a sequence of assignments that are executed 4 immediately upon entry to the constructor and before the implicit invocation of the direct base class 5 constructor. The variable initializers are executed in the textual order in which they appear in the class 6 declaration (§17.4.5). 7

17.10.3 Constructor execution 8

Variable initializers are transformed into assignment statements, and these assignment statements are 9 executed before the invocation of the base class instance constructor. This ordering ensures that all instance 10 fields are initialized by their variable initializers before any statements that have access to that instance are 11 executed. [Example: 12

using System; 13

class A 14 { 15 public A() { 16 PrintFields(); 17 } 18

public virtual void PrintFields() {} 19 } 20

class B: A 21 { 22 int x = 1; 23 int y; 24

public B() { 25 y = -1; 26 } 27

public override void PrintFields() { 28 Console.WriteLine("x = {0}, y = {1}", x, y); 29 } 30 } 31

When new B() is used to create an instance of B, the following output is produced: 32

x = 1, y = 0 33

The value of x is 1 because the variable initializer is executed before the base class instance constructor is 34 invoked. However, the value of y is 0 (the default value of an int) because the assignment to y is not 35 executed until after the base class constructor returns. 36

It is useful to think of instance variable initializers and constructor initializers as statements that are 37 automatically inserted before the constructor-body. The example 38

using System; 39 using System.Collections; 40

class A 41 { 42 int x = 1, y = -1, count; 43

public A() { 44 count = 0; 45 } 46

public A(int n) { 47 count = n; 48 } 49 } 50

Page 334: C# Language Specification

C# LANGUAGE SPECIFICATION

314

class B: A 1 { 2 double sqrt2 = Math.Sqrt(2.0); 3 ArrayList items = new ArrayList(100); 4 int max; 5

public B(): this(100) { 6 items.Add("default"); 7 } 8

public B(int n): base(n – 1) { 9 max = n; 10 } 11 } 12

contains several variable initializers; it also contains constructor initializers of both forms (base and this). 13 The example corresponds to the code shown below, where each comment indicates an automatically inserted 14 statement (the syntax used for the automatically inserted constructor invocations isn’t valid, but merely 15 serves to illustrate the mechanism). 16

using System.Collections; 17

class A 18 { 19 int x, y, count; 20

public A() { 21 x = 1; // Variable initializer 22 y = -1; // Variable initializer 23 object(); // Invoke object() constructor 24 count = 0; 25 } 26

public A(int n) { 27 x = 1; // Variable initializer 28 y = -1; // Variable initializer 29 object(); // Invoke object() constructor 30 count = n; 31 } 32 } 33

class B: A 34 { 35 double sqrt2; 36 ArrayList items; 37 int max; 38

public B(): this(100) { 39 B(100); // Invoke B(int) constructor 40 items.Add("default"); 41 } 42

public B(int n): base(n – 1) { 43 sqrt2 = Math.Sqrt(2.0); // Variable initializer 44 items = new ArrayList(100); // Variable initializer 45 A(n – 1); // Invoke A(int) constructor 46 max = n; 47 } 48 } 49

end example] 50

17.10.4 Default constructors 51

If a class contains no instance constructor declarations, a default instance constructor is automatically 52 provided. That default constructor simply invokes the parameterless constructor of the direct base class. If 53 the direct base class does not have an accessible parameterless instance constructor, a compile-time error 54 occurs. If the class is abstract then the declared accessibility for the default constructor is protected. 55 Otherwise, the declared accessibility for the default constructor is public. [Note: Thus, the default 56 constructor is always of the form 57

Page 335: C# Language Specification

Chapter 17 Classes

315

protected C(): base() {} 1

or 2

public C(): base() {} 3

where C is the name of the class. end note] 4

[Example: In the following code 5

class Message 6 { 7 object sender; 8 string text; 9 } 10

a default constructor is provided because the class contains no instance constructor declarations. Thus, the 11 example is precisely equivalent to 12

class Message 13 { 14 object sender; 15 string text; 16

public Message(): base() {} 17 } 18

end example] 19

17.10.5 Private constructors 20

When a class declares only private instance constructors, it is not possible for other classes (that are not 21 nested in the class) to derive from that class or to create instances of that class. [Example: Private instance 22 constructors can be used to restrict instantiation of the class to a prescribed set of instances. For example: 23

public class Color 24 { 25 private byte val; 26

private static Color red; 27 private static Color green; 28 private static Color blue; 29

private Color(byte val) { this.val = val; } 30

public static Color Red 31 { 32 get { if (red == null) red = new Color(0); return red; } 33 } 34

public static Color Green 35 { 36 get { if (green == null) green = new Color(1); return green; } 37 } 38

public static Color Blue 39 { 40 get { if (blue == null) blue = new Color(2); return blue; } 41 } 42 } 43

Since Color’s constructor is private, there will never be more than three instances of the Color class, 44 namely the ones stored in the static fields and returned by the static properties. end example] 45

At least one instance constructor shall be declared to suppress the automatic generation of a default 46 constructor. 47

17.10.6 Optional instance constructor parameters 48

[Note: The this(…) form of constructor initializer is commonly used in conjunction with overloading to 49 implement optional instance constructor parameters. In the example 50

Page 336: C# Language Specification

C# LANGUAGE SPECIFICATION

316

class Text 1 { 2 public Text(): this(0, 0, null) {} 3 public Text(int x, int y): this(x, y, null) {} 4 public Text(int x, int y, string s) { 5 // Actual constructor implementation 6 } 7 } 8

the first two instance constructors merely provide the default values for the missing arguments. Both use a 9 this(…) constructor initializer to invoke the third instance constructor, which actually does the work of 10 initializing the new instance. The effect is that of optional constructor parameters: 11

Text t1 = new Text(); // Same as Text(0, 0, null) 12 Text t2 = new Text(5, 10); // Same as Text(5, 10, null) 13 Text t3 = new Text(5, 20, "Hello"); 14

end note] 15

17.11 Static constructors 16

A static constructor is a member that implements the actions required to initialize a class. Static 17 constructors are declared using static-constructor-declarations: 18

static-constructor-declaration: 19 attributesopt static-constructor-modifiers identifier ( ) static-constructor-body 20

static-constructor-modifiers: 21 externopt static 22 static externopt 23

static-constructor-body: 24 block 25 ; 26

A static-constructor-declaration can include a set of attributes (§24) and an extern modifier (§17.5.7). 27

The identifier of a static-constructor-declaration shall name the class in which the static constructor is 28 declared. If any other name is specified, a compile-time error occurs. 29

When a static constructor declaration includes an extern modifier, the static constructor is said to be an 30 external static constructor. Because an external static constructor declaration provides no actual 31 implementation, its static-constructor-body consists of a semicolon. For all other static constructor 32 declarations, the static-constructor-body consists of a block, which specifies the statements to execute in 33 order to initialize the class. This corresponds exactly to the method-body of a static method with a void 34 return type (§17.5.8). 35

Static constructors are not inherited, and cannot be called directly. 36

The static constructor for a non-generic class executes at most once in a given application domain. The static 37 constructor for a generic class declaration executes at most once for each closed constructed type 38 constructed from the class declaration (§26.1.5). The execution of a static constructor is triggered by the first 39 of the following events to occur within an application domain: 40

• An instance of the class is created. 41

• Any of the static members of the class are referenced. 42

If a class contains the Main method (§10.1) in which execution begins, the static constructor for that class 43 executes before the Main method is called. If a class contains any static fields with initializers, those 44 initializers are executed in textual order immediately prior to executing the static constructor (§17.4.5). 45

[Example: The example 46

using System; 47

Page 337: C# Language Specification

Chapter 17 Classes

317

class Test 1 { 2 static void Main() { 3 A.F(); 4 B.F(); 5 } 6 } 7

class A 8 { 9 static A() { 10 Console.WriteLine("Init A"); 11 } 12 public static void F() { 13 Console.WriteLine("A.F"); 14 } 15 } 16

class B 17 { 18 static B() { 19 Console.WriteLine("Init B"); 20 } 21 public static void F() { 22 Console.WriteLine("B.F"); 23 } 24 } 25

shall produce the output: 26

Init A 27 A.F 28 Init B 29 B.F 30

because the execution of A's static constructor is triggered by the call to A.F, and the execution of B's static 31 constructor is triggered by the call to B.F. end example] 32

It is possible to construct circular dependencies that allow static fields with variable initializers to be 33 observed in their default value state. 34

[Example: The example 35

using System; 36

class A 37 { 38 public static int X; 39 static A() { X = B.Y + 1;} 40 } 41

class B 42 { 43 public static int Y = A.X + 1; 44 static B() {} 45 static void Main() { 46 Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y); 47 } 48 } 49

produces the output 50

X = 1, Y = 2 51

To execute the Main method, the system first runs the initializer for B.Y, prior to class B's static constructor. 52 Y's initializer causes A's static constructor to be run because the value of A.X is referenced. The static 53 constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y, 54 which is zero. A.X is thus initialized to 1. The process of running A's static field initializers and static 55 constructor then completes, returning to the calculation of the initial value of Y, the result of which 56 becomes 2. end example] 57

Page 338: C# Language Specification

C# LANGUAGE SPECIFICATION

318

17.12 Destructors 1

A destructor is a member that implements the actions required to destruct an instance of a class. A 2 destructor is declared using a destructor-declaration: 3

destructor-declaration: 4 attributesopt externopt ~ identifier ( ) destructor-body 5

destructor-body: 6 block 7 ; 8

A destructor-declaration can include a set of attributes (§24). 9

The identifier of a destructor-declarator shall name the class in which the destructor is declared. If any other 10 name is specified, a compile-time error occurs. 11

When a destructor declaration includes an extern modifier, the destructor is said to be an external 12 destructor. Because an external destructor declaration provides no actual implementation, its destructor-13 body consists of a semicolon. For all other destructors, the destructor-body consists of a block, which 14 specifies the statements to execute in order to destruct an instance of the class. A destructor-body 15 corresponds exactly to the method-body of an instance method with a void return type (§17.5.8). 16

Destructors are not inherited. Thus, a class has no destructors other than the one which can be declared in 17 that class. 18

[Note: Since a destructor is required to have no parameters, it cannot be overloaded, so a class can have, at 19 most, one destructor. end note] 20

Destructors are invoked automatically, and cannot be invoked explicitly. An instance becomes eligible for 21 destruction when it is no longer possible for any code to use that instance. Execution of the destructor for the 22 instance can occur at any time after the instance becomes eligible for destruction. When an instance is 23 destructed, the destructors in that instance’s inheritance chain are called, in order, from most derived to least 24 derived [Example: The output of the example 25

using System; 26

class A 27 { 28 ~A() { 29 Console.WriteLine("A's destructor"); 30 } 31 } 32

class B: A 33 { 34 ~B() { 35 Console.WriteLine("B's destructor"); 36 } 37 } 38

class Test 39 { 40 static void Main() { 41 B b = new B(); 42 b = null; 43 GC.Collect(); 44 GC.WaitForPendingFinalizers(); 45 } 46 } 47

is 48

B’s destructor 49 A’s destructor 50

since destructors in an inheritance chain are called in order, from most derived to least derived. The 51 GC.Collect method is not required, but can be provided by an implementation. end example] 52

Page 339: C# Language Specification

Chapter 17 Classes

319

Destructors can be implemented by overriding the virtual method Finalize on System.Object. In any 1 event, C# programs are not permitted to override this method or call it (or overrides of it) directly. 2 [Example: For instance, the program 3

class A 4 { 5 protected override void Finalize() {} // error 6 public void F() { 7 this.Finalize(); // error 8 } 9 } 10

contains two errors. end example] 11

The compiler behaves as if System.Object.Finalize, and overrides of it, does not exist at all. 12 [Example: Thus, this program: 13

class A 14 { 15 void Finalize() {} // permitted 16 } 17

is valid and the method shown hides System.Object's Finalize method, without requiring the new 18 modifier. end example] 19

For a discussion of the behavior when an exception is thrown from a destructor, see §23.3. 20

Page 340: C# Language Specification
Page 341: C# Language Specification

Chapter 18 Structs

321

18. Structs 1

Structs are similar to classes in that they represent data structures that can contain data members and 2 function members. However, unlike classes, structs are value types and do not require heap allocation. A 3 variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a 4 reference to the data, the latter known as an object. 5

[Note: Structs are particularly useful for small data structures that have value semantics. Complex numbers, 6 points in a coordinate system, or key-value pairs in a dictionary are all good examples of structs. Key to 7 these data structures is that they have few data members, that they do not require use of inheritance or 8 referential identity, and that they can be conveniently implemented using value semantics where assignment 9 copies the value instead of the reference. end note] 10

As described in §11.1.4, the simple types provided by C#, such as int, double, and bool, are, in fact, all 11 struct types. Just as these predefined types are structs, it is also possible to use structs and operator 12 overloading to implement new “primitive” types in the C# language. Two examples of such types are given 13 at the end of this clause (§18.4). 14

18.1 Struct declarations 15

A struct-declaration is a type-declaration (§16.6) that declares a new struct: 16

struct-declaration: 17 attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt 18

struct-interfacesopt type-parameter-constraints-clausesopt struct-body ;opt 19

A struct-declaration consists of an optional set of attributes (§24), followed by an optional set of struct-20 modifiers (§18.1.1), followed by an optional partial modifier (§17.1.4), followed by the keyword struct 21 and an identifier that names the struct, followed by an optional type-parameter-list (§26.1.1), followed by an 22 optional struct-interfaces specification (§18.1.2), followed by an optional type-parameter-constraints-23 clauses (§26.7), followed by a struct-body (§18.1.3), optionally followed by a semicolon. 24

A struct declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-25 parameter-list. 26

A struct declaration that supplies a type-parameter-list is a generic struct declaration (§26.2). 27

18.1.1 Struct modifiers 28

A struct-declaration can optionally include a sequence of struct modifiers: 29

struct-modifiers: 30 struct-modifier 31 struct-modifiers struct-modifier 32

struct-modifier: 33 new 34 public 35 protected 36 internal 37 private 38

It is a compile-time error for the same modifier to appear multiple times in a struct declaration. 39

The modifiers of a struct declaration have the same meaning as those of a class declaration (§17.1.1). 40

Page 342: C# Language Specification

C# LANGUAGE SPECIFICATION

322

18.1.2 Struct interfaces 1

A struct declaration can include a struct-interfaces specification, in which case the struct is said to 2 implement the given interface types. 3

struct-interfaces: 4 : interface-type-list 5

The handling of interfaces on multiple parts of a partial struct declaration (§17.1.4) are discussed further 6 in §17.1.2.2. 7

Interface implementations are discussed further in §20.4. 8

18.1.3 Struct body 9

The struct-body of a struct defines the members of the struct. 10

struct-body: 11 { struct-member-declarationsopt } 12

18.2 Struct members 13

The members of a struct consist of the members introduced by its struct-member-declarations and the 14 members inherited from the type System.ValueType. 15

struct-member-declarations: 16 struct-member-declaration 17 struct-member-declarations struct-member-declaration 18

struct-member-declaration: 19 constant-declaration 20 field-declaration 21 method-declaration 22 property-declaration 23 event-declaration 24 indexer-declaration 25 operator-declaration 26 constructor-declaration 27 static-constructor-declaration 28 type-declaration 29

All kinds of class-member-declarations except destructor-declaration are also struct-member-declarations. 30 Except for the differences noted in §18.3, the descriptions of class members provided in §17.1.4 through 31 §17.11 apply to struct members as well. 32

18.3 Class and struct differences 33

18.3.1 Value semantics 34

Structs are value types (§11.1) and are said to have value semantics. Classes, on the other hand, are reference 35 types (§11.2) and are said to have reference semantics. 36

A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains 37 a reference to the data, the latter known as an object. 38

With classes, it is possible for two variables to reference the same object, and thus possible for operations on 39 one variable to affect the object referenced by the other variable. With structs, the variables each have their 40 own copy of the data, and it is not possible for operations on one to affect the other. Furthermore, because 41 structs are not reference types, it is not possible for values of a struct type to be null. 42

[Example: Given the declaration 43

Page 343: C# Language Specification

Chapter 18 Structs

323

struct Point 1 { 2 public int x, y; 3

public Point(int x, int y) { 4 this.x = x; 5 this.y = y; 6 } 7 } 8

the code fragment 9

Point a = new Point(10, 10); 10 Point b = a; 11 a.x = 100; 12 System.Console.WriteLine(b.x); 13

outputs the value 10. The assignment of a to b creates a copy of the value, and b is thus unaffected by the 14 assignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and b 15 would reference the same object. end example] 16

18.3.2 Inheritance 17

All struct types implicitly inherit from System.ValueType, which, in turn, inherits from class object. A 18 struct declaration can specify a list of implemented interfaces, but it is not possible for a struct declaration to 19 specify a base class. 20

Struct types are never abstract and are always implicitly sealed. The abstract and sealed modifiers are 21 therefore not permitted in a struct declaration. 22

Since inheritance isn’t supported for structs, the declared accessibility of a struct member cannot be 23 protected or protected internal. 24

Function members in a struct cannot be abstract or virtual, and the override modifier is allowed 25 only to override methods inherited from the type System.ValueType. 26

18.3.3 Assignment 27

Assignment to a variable of a struct type creates a copy of the value being assigned. This differs from 28 assignment to a variable of a class type, which copies the reference but not the object identified by the 29 reference. 30

Similar to an assignment, when a struct is passed as a value parameter or returned as the result of a function 31 member, a copy of the struct is created. A struct can be passed by reference to a function member using a 32 ref or out parameter. 33

When a property or indexer of a struct is the target of an assignment, the instance expression associated with 34 the property or indexer access shall be classified as a variable. If the instance expression is classified as a 35 value, a compile-time error occurs. This is described in further detail in §14.13.1. 36

18.3.4 Default values 37

As described in §12.2, several kinds of variables are automatically initialized to their default value when 38 they are created. For variables of class types and other reference types, this default value is null. However, 39 since structs are value types that cannot be null, the default value of a struct is the value produced by 40 setting all value type fields to their default value and all reference type fields to null. 41

[Example: Referring to the Point struct declared above, the example 42

Point[] a = new Point[100]; 43

initializes each Point in the array to the value produced by setting the x and y fields to zero. end example] 44

The default value of a struct corresponds to the value returned by the default constructor of the struct 45 (§11.1.1). Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, 46

Page 344: C# Language Specification

C# LANGUAGE SPECIFICATION

324

every struct implicitly has a parameterless instance constructor, which always returns the value that results 1 from setting all value type fields to their default value and all reference type fields to null. 2

[Note: Structs should be designed to consider the default initialization state a valid state. In the example 3

using System; 4

struct KeyValuePair 5 { 6 string key; 7 string value; 8

public KeyValuePair(string key, string value) { 9 if (key == null || value == null) throw new ArgumentException(); 10 this.key = key; 11 this.value = value; 12 } 13 } 14

the user-defined instance constructor protects against null values only where it is explicitly called. In cases 15 where a KeyValuePair variable is subject to default value initialization, the key and value fields will be 16 null, and the struct should be prepared to handle this state. end note] 17

18.3.5 Boxing and unboxing 18

A value of a class type can be converted to type object or to an interface type that is implemented by the 19 class simply by treating the reference as another type at compile-time. Likewise, a value of type object or 20 a value of an interface type can be converted back to a class type without changing the reference (but, of 21 course, a run-time type check is required in this case). 22

Since structs are not reference types, these operations are implemented differently for struct types. When a 23 value of a struct type is converted to type object or to an interface type that is implemented by the struct, a 24 boxing operation takes place. Likewise, when a value of type object or a value of an interface type is 25 converted back to a struct type, an unboxing operation takes place. A key difference from the same 26 operations on class types is that boxing and unboxing copies the struct value either into or out of the boxed 27 instance. [Note: Thus, following a boxing or unboxing operation, changes made to the unboxed struct are not 28 reflected in the boxed struct. end note] 29

For further details on boxing and unboxing, see §11.3. 30

18.3.6 Meaning of this 31

Within an instance constructor or instance function member of a class, this is classified as a value. Thus, 32 while this can be used to refer to the instance for which the function member was invoked, it is not 33 possible to assign to this in a function member of a class. 34

Within an instance constructor of a struct, this corresponds to an out parameter of the struct type, and 35 within an instance function member of a struct, this corresponds to a ref parameter of the struct type. In 36 both cases, this is classified as a variable, and it is possible to modify the entire struct for which the 37 function member was invoked by assigning to this or by passing this as a ref or out parameter. 38

18.3.7 Field initializers 39

As described in §18.3.4, the default value of a struct consists of the value that results from setting all value 40 type fields to their default value and all reference type fields to null. For this reason, a struct does not 41 permit instance field declarations to include variable initializers. [Example: As such, the following example 42 results in one or more compile-time errors: 43

struct Point 44 { 45 public int x = 1; // Error, initializer not permitted 46 public int y = 1; // Error, initializer not permitted 47 } 48

end example] 49

Page 345: C# Language Specification

Chapter 18 Structs

325

This restriction applies only to instance fields. Static fields of a struct are permitted to include variable 1 initializers. 2

18.3.8 Constructors 3

Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct 4 implicitly has a parameterless instance constructor, which always returns the value that results from setting 5 all value type fields to their default value and all reference type fields to null (§11.1.1). A struct can declare 6 instance constructors having parameters. [Example: 7

struct Point 8 { 9 int x, y; 10

public Point(int x, int y) { 11 this.x = x; 12 this.y = y; 13 } 14 } 15

Given the above declaration, the statements 16

Point p1 = new Point(); 17 Point p2 = new Point(0, 0); 18

both create a Point with x and y initialized to zero. end example] 19

A struct instance constructor is not permitted to include a constructor initializer of the form base(…). 20

The this variable of a struct instance constructor corresponds to an out parameter of the struct type, and 21 similar to an out parameter, this shall be definitely assigned (§12.3) at every location where the 22 constructor returns. [Example: Consider the instance constructor implementation below: 23

struct Point 24 { 25 int x, y; 26

public int X { 27 set { x = value; } 28 } 29

public int Y { 30 set { y = value; } 31 } 32

public Point(int x, int y) { 33 X = x; // error, this is not yet definitely assigned 34 Y = y; // error, this is not yet definitely assigned 35 } 36 } 37

No instance member function (including the set accessors for the properties X and Y) can be called until all 38 fields of the struct being constructed have been definitely assigned. Note, however, that if Point were a 39 class instead of a struct, the instance constructor implementation would be permitted. 40

end example] 41

18.3.9 Destructors 42

A struct is not permitted to declare a destructor. 43

18.3.10 Static constructors 44

Static constructors for structs follow most of the same rules as for classes. The execution of a static 45 constructor for a struct is triggered by the first of the following events to occur within an application 46 domain: 47

• An instance member of the struct is referenced. 48

Page 346: C# Language Specification

C# LANGUAGE SPECIFICATION

326

• A static member of the struct is referenced. 1

• An explicitly declared constructor of the struct is called. 2

[Note: The creation of default values (§18.3.4) of struct types does not trigger the static constructor. (An 3 example of this is the initial value of elements in an array.) end note] 4

18.4 Struct examples 5

This whole subclause is informative. 6

18.4.1 Database integer type 7

The DBInt struct below implements an integer type that can represent the complete set of values of the int 8 type, plus an additional state that indicates an unknown value. A type with these characteristics is commonly 9 used in databases. 10

using System; 11

public struct DBInt 12 { 13 // The Null member represents an unknown DBInt value. 14

public static readonly DBInt Null = new DBInt(); 15

// When the defined field is true, this DBInt represents a 16 // known value which is stored in the value field. When 17 // the defined field is false, this DBInt represents an 18 // unknown value, and the value field is 0. 19

int value; 20 bool defined; 21

// Private instance constructor. Creates a DBInt with a known value. 22

DBInt(int value) { 23 this.value = value; 24 this.defined = true; 25 } 26

// The IsNull property is true if this DBInt represents an unknown 27 // value. 28

public bool IsNull { get { return !defined; } } 29

// The Value property is the known value of this DBInt, or 0 if this 30 // DBInt represents an unknown value. 31

public int Value { get { return value; } } 32

// Implicit conversion from int to DBInt. 33

public static implicit operator DBInt(int x) { 34 return new DBInt(x); 35 } 36

// Explicit conversion from DBInt to int. Throws an exception if the 37 // given DBInt represents an unknown value. 38

public static explicit operator int(DBInt x) { 39 if (!x.defined) throw new InvalidOperationException(); 40 return x.value; 41 } 42

public static DBInt operator +(DBInt x) { 43 return x; 44 } 45

public static DBInt operator -(DBInt x) { 46 return x.defined? -x.value: Null; 47 } 48

public static DBInt operator +(DBInt x, DBInt y) { 49 return x.defined && y.defined? x.value + y.value: Null; 50 } 51

Page 347: C# Language Specification

Chapter 18 Structs

327

public static DBInt operator -(DBInt x, DBInt y) { 1 return x.defined && y.defined? x.value - y.value: Null; 2 } 3

public static DBInt operator *(DBInt x, DBInt y) { 4 return x.defined && y.defined? x.value * y.value: Null; 5 } 6

public static DBInt operator /(DBInt x, DBInt y) { 7 return x.defined && y.defined? x.value / y.value: Null; 8 } 9

public static DBInt operator %(DBInt x, DBInt y) { 10 return x.defined && y.defined? x.value % y.value: Null; 11 } 12

public static DBBool operator ==(DBInt x, DBInt y) { 13 return x.defined && y.defined? x.value == y.value: DBBool.Null; 14 } 15

public static DBBool operator !=(DBInt x, DBInt y) { 16 return x.defined && y.defined? x.value != y.value: DBBool.Null; 17 } 18

public static DBBool operator >(DBInt x, DBInt y) { 19 return x.defined && y.defined? x.value > y.value: DBBool.Null; 20 } 21

public static DBBool operator <(DBInt x, DBInt y) { 22 return x.defined && y.defined? x.value < y.value: DBBool.Null; 23 } 24

public static DBBool operator >=(DBInt x, DBInt y) { 25 return x.defined && y.defined? x.value >= y.value: DBBool.Null; 26 } 27

public static DBBool operator <=(DBInt x, DBInt y) { 28 return x.defined && y.defined? x.value <= y.value: DBBool.Null; 29 } 30 } 31

18.4.2 Database boolean type 32

The DBBool struct below implements a three-valued logical type. The possible values of this type are 33 DBBool.True, DBBool.False, and DBBool.Null, where the Null member indicates an unknown value. 34 Such three-valued logical types are commonly used in databases. 35

using System; 36

public struct DBBool 37 { 38 // The three possible DBBool values. 39

public static readonly DBBool Null = new DBBool(0); 40 public static readonly DBBool False = new DBBool(-1); 41 public static readonly DBBool True = new DBBool(1); 42

// Private field that stores –1, 0, 1 for False, Null, True. 43

sbyte value; 44

// Private instance constructor. The value parameter must be 45 // –1, 0, or 1. 46

DBBool(int value) { 47 this.value = (sbyte)value; 48 } 49

// Properties to examine the value of a DBBool. Return true if this 50 // DBBool has the given value, false otherwise. 51

public bool IsNull { get { return value == 0; } } 52

public bool IsFalse { get { return value < 0; } } 53

public bool IsTrue { get { return value > 0; } } 54

Page 348: C# Language Specification

C# LANGUAGE SPECIFICATION

328

// Implicit conversion from bool to DBBool. Maps true to 1 // DBBool.True and false to DBBool.False. 2

public static implicit operator DBBool(bool x) { 3 return x? True: False; 4 } 5

// Explicit conversion from DBBool to bool. Throws an exception 6 // if the given DBBool is Null, otherwise returns true or false. 7

public static explicit operator bool(DBBool x) { 8 if (x.value == 0) throw new InvalidOperationException(); 9 return x.value > 0; 10 } 11

// Equality operator. Returns Null if either operand is Null, 12 // otherwise returns True or False. 13

public static DBBool operator ==(DBBool x, DBBool y) { 14 if (x.value == 0 || y.value == 0) return Null; 15 return x.value == y.value? True: False; 16 } 17

// Inequality operator. Returns Null if either operand is Null, 18 // otherwise returns True or False. 19

public static DBBool operator !=(DBBool x, DBBool y) { 20 if (x.value == 0 || y.value == 0) return Null; 21 return x.value != y.value? True: False; 22 } 23

// Logical negation operator. Returns True if the operand is False, 24 // Null if the operand is Null, or False if the operand is True. 25

public static DBBool operator !(DBBool x) { 26 return new DBBool(-x.value); 27 } 28

// Logical AND operator. Returns False if either operand is False, 29 // otherwise Null if either operand is Null, otherwise True. 30

public static DBBool operator &(DBBool x, DBBool y) { 31 return new DBBool(x.value < y.value? x.value: y.value); 32 } 33

// Logical OR operator. Returns True if either operand is True, 34 // otherwise Null if either operand is Null, otherwise False. 35

public static DBBool operator |(DBBool x, DBBool y) { 36 return new DBBool(x.value > y.value? x.value: y.value); 37 } 38

// Definitely true operator. Returns true if the operand is True, 39 // false otherwise. 40

public static bool operator true(DBBool x) { 41 return x.value > 0; 42 } 43

// Definitely false operator. Returns true if the operand is False, 44 // false otherwise. 45

public static bool operator false(DBBool x) { 46 return x.value < 0; 47 } 48 } 49

End of informative text. 50

Page 349: C# Language Specification

Chapter 19 Arrays

329

19. Arrays 1

An array is a data structure that contains a number of variables which are accessed through computed 2 indices. The variables contained in an array, also called the elements of the array, are all of the same type, 3 and this type is called the element type of the array. 4

An array has a rank which determines the number of indices associated with each array element. The rank of 5 an array is also referred to as the dimensions of the array. An array with a rank of one is called a single-6 dimensional array. An array with a rank greater than one is called a multi-dimensional array. Specific sized 7 multi-dimensional arrays are often referred to as two-dimensional arrays, three-dimensional arrays, and so 8 on. Each dimension of an array has an associated length which is an integral number greater than or equal to 9 zero. The dimension lengths are not part of the type of the array, but rather are established when an instance 10 of the array type is created at run-time. The length of a dimension determines the valid range of indices for 11 that dimension: For a dimension of length N, indices can range from 0 to N – 1 inclusive. The total number 12 of elements in an array is the product of the lengths of each dimension in the array. If one or more of the 13 dimensions of an array have a length of zero, the array is said to be empty. 14

The element type of an array can be any type, including an array type. 15

19.1 Array types 16

An array type is written as a non-array-type followed by one or more rank-specifiers: 17

array-type: 18 non-array-type rank-specifiers 19

non-array-type: 20 value-type 21 class-type 22 interface-type 23 delegate-type 24 type-parameter 25

rank-specifiers: 26 rank-specifier 27 rank-specifiers rank-specifier 28

rank-specifier: 29 [ dim-separatorsopt ] 30

dim-separators: 31 , 32 dim-separators , 33

A non-array-type is any type that is not itself an array-type. 34

The rank of an array type is given by the leftmost rank-specifier in the array-type: A rank-specifier indicates 35 that the array is an array with a rank of one plus the number of “,” tokens in the rank-specifier. 36

The element type of an array type is the type that results from deleting the leftmost rank-specifier: 37

• An array type of the form T[R] is an array with rank R and a non-array element type T. 38

• An array type of the form T[R][R1]…[RN] is an array with rank R and an element type T[R1]…[RN]. 39

Page 350: C# Language Specification

C# LANGUAGE SPECIFICATION

330

In effect, the rank-specifiers are read from left to right before the final non-array element type. [Example: 1 The type int[][,,][,] is a single-dimensional array of three-dimensional arrays of two-dimensional 2 arrays of int. end example] 3

At run-time, a value of an array type can be null or a reference to an instance of that array type or a 4 covariant array type, as described in §19.5. 5

19.1.1 The System.Array type 6

The type System.Array is the abstract base type of all array types. An implicit reference conversion 7 (§13.1.4) exists from any array type to System.Array and to any interface type implemented by 8 System.Array. An explicit reference conversion (§13.2.3) exists from System.Array and any interface 9 type implemented by System.Array to any array type. System.Array is not itself an array-type. Rather, 10 it is a class-type from which all array-types are derived. 11

At run-time, a value of type System.Array can be null or a reference to an instance of any array type. 12

19.2 Array creation 13

Array instances are created explicitly by array-creation-expressions (§14.5.10.2) or by field or local variable 14 declarations that include an array-initializer (§19.6). Array instances can also be created implicitly by 15 invoking a method in its expanded form (§14.4.1). 16

When an array instance is created, the rank and length of each dimension are established and then remain 17 constant for the entire lifetime of the instance. In other words, it is not possible to change the rank of an 18 existing array instance, nor is it possible to resize its dimensions. 19

An array instance is always of an array type. The System.Array type is an abstract type that cannot be 20 instantiated. 21

Elements of arrays created by array-creation-expressions are always initialized to their default value 22 (§12.2). 23

19.3 Array element access 24

Array elements are accessed using element-access expressions (§14.5.6.1) of the form A[I1, I2, …, IN], 25 where A is an expression of an array type and each IX is an expression of type int, uint, long, ulong, or 26 of a type that can be implicitly converted to one or more of these types. The result of an array element access 27 is a variable, namely the array element selected by the indices. 28

The elements of an array can be enumerated using a foreach statement (§15.8.4). 29

19.4 Array members 30

Every array type inherits the members declared by the System.Array type. 31

19.5 Array covariance 32

For any two reference-types A and B, if an implicit reference conversion (§13.1.4) or explicit reference 33 conversion (§13.2.3) exists from A to B, then the same reference conversion also exists from the array type 34 A[R] to the array type B[R], where R is any given rank-specifier (but the same for both array types). This 35 relationship is known as array covariance. Array covariance, in particular, means that a value of an array 36 type A[R] might actually be a reference to an instance of an array type B[R], provided an implicit reference 37 conversion exists from B to A. 38

Because of array covariance, assignments to elements of reference type arrays include a run-time check 39 which ensures that the value being assigned to the array element is actually of a permitted type (§14.13.1). 40 [Example: 41

Page 351: C# Language Specification

Chapter 19 Arrays

331

class Test 1 { 2 static void Fill(object[] array, int index, int count, object value) { 3 for (int i = index; i < index + count; i++) array[i] = value; 4 } 5

static void Main() { 6 string[] strings = new string[100]; 7 Fill(strings, 0, 100, "Undefined"); 8 Fill(strings, 0, 10, null); 9 Fill(strings, 90, 10, 0); 10 } 11 } 12

The assignment to array[i] in the Fill method implicitly includes a run-time check, which ensures that 13 the object referenced by value is either null or an instance of a type that is compatible with the actual 14 element type of array. In Main, the first two invocations of Fill succeed, but the third invocation causes a 15 System.ArrayTypeMismatchException to be thrown upon executing the first assignment to 16 array[i]. The exception occurs because a boxed int cannot be stored in a string array. end example] 17

Array covariance specifically does not extend to arrays of value-types. For example, no conversion exists 18 that permits an int[] to be treated as an object[]. 19

19.6 Arrays and the generic IList interface 20

A one-dimensional array S[] implements the interface System.Collections.Generic.IList<S> 21 (IList<S> for short) and its base interfaces. Accordingly, there is an implicit conversion from S[] to 22 IList<S> and its base interfaces. In addition, if there is an implicit reference conversion from S to T then 23 S[] implements IList<T> and there is an implicit reference conversion from S[] to IList<T> and its 24 base interfaces (§13.1.4). If there is an explicit reference conversion from S to T then there is an explicit 25 reference conversion from S[] to IList<T> and its base interfaces (§13.2.3). [Example: For example: 26

using System.Collections.Generic; 27

class Test 28 { 29 static void Main() { 30 string[] sa = new string[5]; 31 object[] oa1 = new object[5]; 32 object[] oa2 = sa; 33

IList<string> lst1 = sa; // OK 34 IList<string> lst2 = oa1; // Error – need cast 35 IList<object> lst3 = sa; // OK 36 IList<object> lst4 = oa1; // OK 37

IList<string> lst5 = (IList<string>)oa1; // Exception 38 IList<string> lst6 = (IList<string>)oa2; // OK 39 } 40 } 41

The assignment lst2 = oa1 generates a compile-time error since the conversion from object[] to 42 IList<string> is an explicit conversion, not implicit. The cast (IList<string>)oa1 will cause an 43 exception to be thrown at runtime since oa1 references an object[] and not a string[]. However the 44 cast (IList<string>)oa2 will not cause an exception to be thrown since oa2 references a string[]. 45 end example] 46

Whenever there is an implicit or explicit reference conversion from S[] to IList<T>, there is also an 47 explicit reference conversion from IList<T> and its base interfaces to S[] (§13.2.3). 48

When an array type S[] implements IList<T>, some of the members of the implemented interface may 49 throw exceptions. The precise behavior of the implementation of the interface is beyond the scope of this 50 specification. 51

Page 352: C# Language Specification

C# LANGUAGE SPECIFICATION

332

19.7 Array initializers 1

Array initializers can be specified in field declarations (§17.4), local variable declarations (§15.5.1), and 2 array creation expressions (§14.5.10.2): 3

array-initializer: 4 { variable-initializer-listopt } 5 { variable-initializer-list , } 6

variable-initializer-list: 7 variable-initializer 8 variable-initializer-list , variable-initializer 9

variable-initializer: 10 expression 11 array-initializer 12

An array initializer consists of a sequence of variable initializers, enclosed by “{”and “}” tokens and 13 separated by “,” tokens. Each variable initializer is an expression or, in the case of a multi-dimensional 14 array, a nested array initializer. 15

The context in which an array initializer is used determines the type of the array being initialized. In an array 16 creation expression, the array type immediately precedes the initializer. In a field or variable declaration, the 17 array type is the type of the field or variable being declared. When an array initializer is used in a field or 18 variable declaration, [Example: such as: 19

int[] a = {0, 2, 4, 6, 8}; 20

end example] it is simply shorthand for an equivalent array creation expression: [Example: 21

int[] a = new int[] {0, 2, 4, 6, 8}; 22

end example] 23

For a single-dimensional array, the array initializer shall consist of a sequence of expressions that are 24 assignment compatible with the element type of the array. The expressions initialize array elements in 25 increasing order, starting with the element at index zero. The number of expressions in the array initializer 26 determines the length of the array instance being created. [Example: The array initializer above creates an 27 int[] instance of length 5 and then initializes the instance with the following values: 28

a[0] = 0; a[1] = 2; a[2] = 4; a[3] = 6; a[4] = 8; 29

end example] 30

For a multi-dimensional array, the array initializer shall have as many levels of nesting as there are 31 dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the 32 innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is 33 determined by the number of elements at the corresponding nesting level in the array initializer. For each 34 nested array initializer, the number of elements shall be the same as the other array initializers at the same 35 level. [Example: The example: 36

int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}; 37

creates a two-dimensional array with a length of five for the leftmost dimension and a length of two for the 38 rightmost dimension: 39

int[,] b = new int[5, 2]; 40

and then initializes the array instance with the following values: 41

b[0, 0] = 0; b[0, 1] = 1; 42 b[1, 0] = 2; b[1, 1] = 3; 43 b[2, 0] = 4; b[2, 1] = 5; 44 b[3, 0] = 6; b[3, 1] = 7; 45 b[4, 0] = 8; b[4, 1] = 9; 46

end example] 47

Page 353: C# Language Specification

Chapter 19 Arrays

333

When an array creation expression includes both explicit dimension lengths and an array initializer, the 1 lengths shall be constant expressions and the number of elements at each nesting level shall match the 2 corresponding dimension length. [Example: Here are some examples: 3

int i = 3; 4 int[] x = new int[3] {0, 1, 2}; // OK 5 int[] y = new int[i] {0, 1, 2}; // Error, i not a constant 6 int[] z = new int[3] {0, 1, 2, 3}; // Error, length/initializer mismatch 7

Here, the initializer for y results in a compile-time error because the dimension length expression is not a 8 constant, and the initializer for z results in a compile-time error because the length and the number of 9 elements in the initializer do not agree. end example] 10

[Note: Like Standard C++, C# allows a trailing comma at the end of an array-initializer. This syntax 11 provides flexibility in adding or deleting members from such a list, and simplifies machine generation of 12 such lists. end note] 13

Page 354: C# Language Specification
Page 355: C# Language Specification

Chapter 20 Interfaces

335

20. Interfaces 1

An interface defines a contract. A class or struct that implements an interface shall adhere to its contract. An 2 interface can inherit from multiple base interfaces, and a class or struct can implement multiple interfaces. 3

Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide 4 implementations for the members that it declares. The interface merely specifies the members that shall be 5 supplied by classes or interfaces that implement the interface. 6

20.1 Interface declarations 7

An interface-declaration is a type-declaration (§16.6) that declares a new interface type. 8

interface-declaration: 9 attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt 10 interface-baseopt type-parameter-constraints-clausesopt interface-body ;opt 11

An interface-declaration consists of an optional set of attributes (§24), followed by an optional set of 12 interface-modifiers (§20.1.1), followed by an optional partial modifier (§17.1.4), followed by the 13 keyword interface and an identifier that names the interface, followed by an optional type-parameter-list 14 (§26.1.1), followed by an optional interface-base specification (§20.1.2), followed by an optional type-15 parameter-constraints-clauses (§26.7), followed by an interface-body (§20.1.3), optionally followed by a 16 semicolon. 17

An interface declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-18 parameter-list. 19

An interface declaration that supplies a type-parameter-list is a generic interface declaration (§26.3). 20

20.1.1 Interface modifiers 21

An interface-declaration can optionally include a sequence of interface modifiers: 22

interface-modifiers: 23 interface-modifier 24 interface-modifiers interface-modifier 25

interface-modifier: 26 new 27 public 28 protected 29 internal 30 private 31

It is a compile-time error for the same modifier to appear multiple times in an interface declaration. 32

The new modifier is only permitted on nested interfaces. It specifies that the interface hides an inherited 33 member by the same name, as described in §17.2.2. 34

The public, protected, internal, and private modifiers control the accessibility of the interface. 35 Depending on the context in which the interface declaration occurs, only some of these modifiers might be 36 permitted (§10.5.1). When a partial type declaration (§17.1.4) includes an accessibility specification (via the 37 public, protected, internal, and private modifiers), the rules in §17.1.1 apply. 38

Page 356: C# Language Specification

C# LANGUAGE SPECIFICATION

336

20.1.2 Base interfaces 1

An interface can inherit from one or more interfaces, which are called the explicit base interfaces of the 2 interface. When an interface has one or more explicit base interfaces, then in the declaration of that interface, 3 the interface identifier is followed by a colon and a comma-separated list of base interfaces. 4

interface-base: 5 : interface-type-list 6

The explicit base interfaces can be constructed interface types (§26.5). A base interface cannot be a type 7 parameter on its own, though it can involve the type parameters that are in scope. 8

The explicit base interfaces (and any type arguments) of an interface shall be at least as accessible as the 9 interface itself (§10.5.4). [Note: For example, it is a compile-time error to specify a private or internal 10 interface in the interface-base of a public interface. end note] 11

It is a compile-time error for an interface to directly or indirectly inherit from itself. 12

The base interfaces of an interface are the explicit base interfaces and their base interfaces (after substituting 13 type arguments for type parameters in constructed generic types). In other words, the set of base interfaces is 14 the complete transitive closure of the explicit base interfaces, their explicit base interfaces, and so on. An 15 interface inherits all members of its base interfaces. [Example: In the following code 16

interface IControl 17 { 18 void Paint(); 19 } 20

interface ITextBox: IControl 21 { 22 void SetText(string text); 23 } 24

interface IListBox: IControl 25 { 26 void SetItems(string[] items); 27 } 28

interface IComboBox: ITextBox, IListBox {} 29

the base interfaces of IComboBox are IControl, ITextBox, and IListBox. In other words, the 30 IComboBox interface above inherits members SetText and SetItems as well as Paint. end example] 31

Members inherited from a constructed generic type are inherited after type substitution. That is, any 32 constituent types in the member have the base class declaration’s type parameters replaced with the 33 corresponding type arguments used in the class-base specification. [Example: In the following code 34

interface IBase<T> 35 { 36 T[] Combine(T a, T b); 37 } 38

interface IDerived : IBase<string[,]> 39 { 40 // Inherited: string[][,] Combine(string[,] a, string[,] b); 41 } 42

the interface IDerived inherits the Combine method after the type parameter T is replaced with 43 string[,]. end example] 44

A class or struct that implements an interface also implicitly implements all of the interface’s base 45 interfaces. 46

The handling of interfaces on multiple parts of a partial interface declaration (§17.1.4) are discussed further 47 in §17.1.2.2. 48

20.1.3 Interface body 49

The interface-body of an interface defines the members of the interface. 50

Page 357: C# Language Specification

Chapter 20 Interfaces

337

interface-body: 1 { interface-member-declarationsopt } 2

20.2 Interface members 3

The members of an interface are the members inherited from the base interfaces and the members declared 4 by the interface itself. 5

interface-member-declarations: 6 interface-member-declaration 7 interface-member-declarations interface-member-declaration 8

interface-member-declaration: 9 interface-method-declaration 10 interface-property-declaration 11 interface-event-declaration 12 interface-indexer-declaration 13

An interface declaration can declare zero or more members. The members of an interface shall be methods, 14 properties, events, or indexers. An interface cannot contain constants, fields, operators, instance 15 constructors, destructors, or types, nor can an interface contain static members of any kind. 16

All interface members implicitly have public access. It is a compile-time error for interface member 17 declarations to include any modifiers. In particular, interface members cannot be declared with the modifiers 18 abstract, public, protected, internal, private, virtual, override, or static. 19

[Example: The example 20

public delegate void StringListEventHandler(IStringList sender, 21 ListEventArgs e); 22

public interface IStringList 23 { 24 void Add(string s); 25

int Count { get; } 26

event StringListEventHandler Changed; 27

string this[int index] { get; set; } 28 } 29

declares an interface that contains one each of the possible kinds of members: A method, a property, an 30 event, and an indexer. end example] 31

An interface-declaration creates a new declaration space (§10.3), and the type parameters and interface-32 member-declarations immediately contained by the interface-declaration introduce new members into this 33 declaration space. The following rules apply to interface-member-declarations: 34

• The name of a type parameter in the type-parameter-list of an interface declaration shall differ from the 35 names of all other type parameters in the same type-parameter-list and shall differ from the names of all 36 members of the interface. 37

• The name of a method shall differ from the names of all properties and events declared in the same 38 interface. In addition, the signature (§10.6) of a method shall differ from the signatures of all other 39 methods declared in the same interface, and two methods declared in the same interface shall not have 40 signatures that differ solely by ref and out. 41

• The name of a property or event shall differ from the names of all other members declared in the same 42 interface. 43

• The signature of an indexer shall differ from the signatures of all other indexers declared in the same 44 interface. 45

The inherited members of an interface are specifically not part of the declaration space of the interface. 46 Thus, an interface is allowed to declare a member with the same name or signature as an inherited member. 47

Page 358: C# Language Specification

C# LANGUAGE SPECIFICATION

338

When this occurs, the derived interface member is said to hide the base interface member. Hiding an 1 inherited member is not considered an error, but it does cause the compiler to issue a warning. To suppress 2 the warning, the declaration of the derived interface member shall include a new modifier to indicate that the 3 derived member is intended to hide the base member. This topic is discussed further in §10.7.1.2. 4

If a new modifier is included in a declaration that doesn’t hide an inherited member, a warning is issued to 5 that effect. This warning is suppressed by removing the new modifier. 6

[Note: The members in class object are not, strictly speaking, members of any interface (§20.2). However, 7 the members in class object are available via member lookup in any interface type (§14.3). end note] 8

For a discussion about members of an interface declared in multiple parts (§17.1.4), see §17.2. 9

20.2.1 Interface methods 10

Interface methods are declared using interface-method-declarations: 11

interface-method-declaration: 12 attributesopt newopt return-type identifier type-parameter-listopt 13 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt ; 14

The attributes, return-type, identifier, and formal-parameter-list of an interface method declaration have the 15 same meaning as those of a method declaration in a class (§17.5). An interface method declaration is not 16 permitted to specify a method body; therefore the declaration always ends with a semicolon. An interface-17 method-declaration shall not have type-parameter-constraints-clauses unless it also has a type-parameter-18 list. 19

20.2.2 Interface properties 20

Interface properties are declared using interface-property-declarations: 21

interface-property-declaration: 22 attributesopt newopt type identifier { interface-accessors } 23

interface-accessors: 24 attributesopt get ; 25 attributesopt set ; 26 attributesopt get ; attributesopt set ; 27 attributesopt set ; attributesopt get ; 28

The attributes, type, and identifier of an interface property declaration have the same meaning as those of a 29 property declaration in a class (§17.6). 30

The accessors of an interface property declaration correspond to the accessors of a class property declaration 31 (§17.6.2), except that the accessor body shall always be a semicolon. Thus, the accessors simply indicate 32 whether the property is read-write, read-only, or write-only. 33

20.2.3 Interface events 34

Interface events are declared using interface-event-declarations: 35

interface-event-declaration: 36 attributesopt newopt event type identifier ; 37

The attributes, type, and identifier of an interface event declaration have the same meaning as those of an 38 event declaration in a class (§17.7). 39

20.2.4 Interface indexers 40

Interface indexers are declared using interface-indexer-declarations: 41

interface-indexer-declaration: 42 attributesopt newopt type this [ formal-parameter-list ] { interface-accessors } 43

Page 359: C# Language Specification

Chapter 20 Interfaces

339

The attributes, type, and formal-parameter-list of an interface indexer declaration have the same meaning as 1 those of an indexer declaration in a class (§17.8). 2

The accessors of an interface indexer declaration correspond to the accessors of a class indexer declaration 3 (§17.8), except that the accessor body shall always be a semicolon. Thus, the accessors simply indicate 4 whether the indexer is read-write, read-only, or write-only. 5

20.2.5 Interface member access 6

Interface members are accessed through member access (§14.5.4) and indexer access (§14.5.6.2) expressions 7 of the form I.M and I[A], where I is an expression having an interface type, M is a method, property, or 8 event of that interface type, and A is an indexer argument list. 9

For interfaces that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or 10 one direct base interface), the effects of the member lookup (§14.3), method invocation (§14.5.5.1), and 11 indexer access (§14.5.6.2) rules are exactly the same as for classes and structs: More derived members hide 12 less derived members with the same name or signature. However, for multiple-inheritance interfaces, 13 ambiguities can occur when two or more unrelated base interfaces declare members with the same name or 14 signature. This subclause shows several examples of such situations. In all cases, explicit casts can be used 15 to resolve the ambiguities. 16

[Example: In the following code 17

interface IList 18 { 19 int Count { get; set; } 20 } 21

interface ICounter 22 { 23 void Count(int i); 24 } 25

interface IListCounter: IList, ICounter {} 26

class C 27 { 28 void Test(IListCounter x) { 29 x.Count(1); // Error 30 x.Count = 1; // Error 31 ((IList)x).Count = 1; // Ok, invokes IList.Count.set 32 ((ICounter)x).Count(1); // Ok, invokes ICounter.Count 33 } 34 } 35

the first two statements cause compile-time errors because the member lookup (§14.3) of Count in 36 IListCounter is ambiguous. As illustrated by the example, the ambiguity is resolved by casting x to the 37 appropriate base interface type. Such casts have no run-time costs—they merely consist of viewing the 38 instance as a less derived type at compile-time. end example] 39

[Example: In the following code 40

interface IInteger 41 { 42 void Add(int i); 43 } 44

interface IDouble 45 { 46 void Add(double d); 47 } 48

interface INumber: IInteger, IDouble {} 49

Page 360: C# Language Specification

C# LANGUAGE SPECIFICATION

340

class C 1 { 2 void Test(INumber n) { 3 n.Add(1); // Error, both Add methods are applicable 4 n.Add(1.0); // Ok, only IDouble.Add is applicable 5 ((IInteger)n).Add(1); // Ok, only IInteger.Add is a candidate 6 ((IDouble)n).Add(1); // Ok, only IDouble.Add is a candidate 7 } 8 } 9

the invocation n.Add(1) is ambiguous because a method invocation (§14.5.5.1) requires all overloaded 10 candidate methods to be declared in the same type. However, the invocation n.Add(1.0) is permitted 11 because only IDouble.Add is applicable. When explicit casts are inserted, there is only one candidate 12 method, and thus no ambiguity. end example] 13

[Example: In the following code 14

interface IBase 15 { 16 void F(int i); 17 } 18

interface ILeft: IBase 19 { 20 new void F(int i); 21 } 22

interface IRight: IBase 23 { 24 void G(); 25 } 26

interface IDerived: ILeft, IRight {} 27

class A 28 { 29 void Test(IDerived d) { 30 d.F(1); // Invokes ILeft.F 31 ((IBase)d).F(1); // Invokes IBase.F 32 ((ILeft)d).F(1); // Invokes ILeft.F 33 ((IRight)d).F(1); // Invokes IBase.F 34 } 35 } 36

the IBase.F member is hidden by the ILeft.F member. The invocation d.F(1) thus selects ILeft.F, 37 even though IBase.F appears to not be hidden in the access path that leads through IRight. 38

The intuitive rule for hiding in multiple-inheritance interfaces is simply this: If a member is hidden in any 39 access path, it is hidden in all access paths. Because the access path from IDerived to ILeft to IBase 40 hides IBase.F, the member is also hidden in the access path from IDerived to IRight to IBase. end 41 example] 42

20.3 Fully qualified interface member names 43

An interface member is sometimes referred to by a qualified interface member name. A qualified interface 44 member name consists of a name identifying the interface in which the member is declared, followed by a 45 dot, followed by the name of the member. [Example: Given the declarations 46

interface IControl 47 { 48 void Paint(); 49 } 50

interface ITextBox: IControl 51 { 52 void SetText(string text); 53 } 54

Page 361: C# Language Specification

Chapter 20 Interfaces

341

a qualified interface member name for Paint is IControl.Paint and a qualified interface member name 1 for SetText is ITextBox.SetText. In the example above, it is not possible to refer to Paint as 2 ITextBox.Paint. end example] 3

When an interface is part of a namespace, a qualified interface member name can include the namespace 4 name. [Example: 5

namespace System 6 { 7 public interface ICloneable 8 { 9 object Clone(); 10 } 11 } 12

Within the System namespace, both ICloneable.Clone and System.ICloneable.Clone are qualified 13 interface member names for the Clone method. end example] 14

20.4 Interface implementations 15

Interfaces can be implemented by classes and structs. To indicate that a class or struct implements an 16 interface, the interface is included in the base class list of the class or struct. [Example: 17

interface ICloneable 18 { 19 object Clone(); 20 } 21

interface IComparable 22 { 23 int CompareTo(object other); 24 } 25

class ListEntry: ICloneable, IComparable 26 { 27 public object Clone() {…} 28

public int CompareTo(object other) {…} 29 } 30

end example] 31

A class or struct that implements an interface also implicitly implements all of the interface’s base 32 interfaces. This is true even if the class or struct doesn’t explicitly list all base interfaces in the base class 33 list. [Example: 34

interface IControl 35 { 36 void Paint(); 37 } 38

interface ITextBox: IControl 39 { 40 void SetText(string text); 41 } 42

class TextBox: ITextBox 43 { 44 public void Paint() {…} 45

public void SetText(string text) {…} 46 } 47

Here, class TextBox implements both IControl and ITextBox. end example] 48

20.4.1 Explicit interface member implementations 49

For purposes of implementing interfaces, a class or struct can declare explicit interface member 50 implementations. An explicit interface member implementation is a method, property, event, or indexer 51 declaration that references a qualified interface member name. [Example: 52

Page 362: C# Language Specification

C# LANGUAGE SPECIFICATION

342

interface ICloneable 1 { 2 object Clone(); 3 } 4

interface IComparable 5 { 6 int CompareTo(object other); 7 } 8

class ListEntry: ICloneable, IComparable 9 { 10 object ICloneable.Clone() {…} 11

int IComparable.CompareTo(object other) {…} 12 } 13

Here, ICloneable.Clone and IComparable.CompareTo are explicit interface member 14 implementations. end example] 15

[Example: In some cases, the name of an interface member might not be appropriate for the implementing 16 class, in which case, the interface member can be implemented using explicit interface member 17 implementation. A class implementing a file abstraction, for example, would likely implement a Close 18 member function that has the effect of releasing the file resource, and implement the Dispose method of 19 the IDisposable interface using explicit interface member implementation: 20

interface IDisposable 21 { 22 void Dispose(); 23 } 24

class MyFile: IDisposable 25 { 26 void IDisposable.Dispose() 27 { 28 Close(); 29 } 30

public void Close() 31 { 32 // Do what's necessary to close the file 33 System.GC.SuppressFinalize(this); 34 } 35 } 36

end example] 37

It is not possible to access an explicit interface member implementation through a qualified interface 38 member name in a method invocation, property access, event access, or indexer access. An explicit interface 39 member implementation can only be accessed through an interface instance, and is in that case referenced 40 simply by its member name. 41

It is a compile-time error for an explicit interface member implementation to include any modifiers other 42 than extern (§17.5). It is a compile-time error for an explicit interface method implementation to include 43 type-parameter-constraints-clauses. The constraints for a generic explicit interface method implementation 44 are inherited from the interface method. 45

Explicit interface member implementations have different accessibility characteristics than other members. 46 Because explicit interface member implementations are never accessible through a qualified interface 47 member name in a method invocation or a property access, they are in a sense private. However, since they 48 can be accessed through an interface instance, they are in a sense also public. 49

Explicit interface member implementations serve two primary purposes: 50

• Because explicit interface member implementations are not accessible through class or struct instances, 51 they allow interface implementations to be excluded from the public interface of a class or struct. This is 52 particularly useful when a class or struct implements an internal interface that is of no interest to a 53 consumer of that class or struct. 54

Page 363: C# Language Specification

Chapter 20 Interfaces

343

• Explicit interface member implementations allow disambiguation of interface members with the same 1 signature. Without explicit interface member implementations it would be impossible for a class or 2 struct to have different implementations of interface members with the same signature and return type, 3 as would it be impossible for a class or struct to have any implementation at all of interface members 4 with the same signature but with different return types. 5

For an explicit interface member implementation to be valid, the class or struct shall name an interface in its 6 base class list that contains a member whose containing type, name, type, number of type parameters, and 7 parameter types exactly match those of the explicit interface member implementation. If an interface 8 function member has a parameter array, the corresponding parameter of an associated explicit interface 9 member implementation is allowed, but not required, to have the params modifier. If the interface function 10 member does not have a parameter array then an associated explicit interface member implementation shall 11 not have a parmater array. [Example: Thus, in the following class 12

class Shape: ICloneable 13 { 14 object ICloneable.Clone() {…} 15

int IComparable.CompareTo(object other) {…} // invalid 16 } 17

the declaration of IComparable.CompareTo results in a compile-time error because IComparable is not 18 listed in the base class list of Shape and is not a base interface of ICloneable. Likewise, in the 19 declarations 20

class Shape: ICloneable 21 { 22 object ICloneable.Clone() {…} 23 } 24

class Ellipse: Shape 25 { 26 object ICloneable.Clone() {…} // invalid 27 } 28

the declaration of ICloneable.Clone in Ellipse results in a compile-time error because ICloneable is 29 not explicitly listed in the base class list of Ellipse. end example] 30

The qualified interface member name of explicit interface member implementation shall reference the 31 interface in which the member was declared. [Example: Thus, in the declarations 32

interface IControl 33 { 34 void Paint(); 35 } 36

interface ITextBox: IControl 37 { 38 void SetText(string text); 39 } 40

class TextBox: ITextBox 41 { 42 void IControl.Paint() {…} 43

void ITextBox.SetText(string text) {…} 44 } 45

the explicit interface member implementation of Paint shall be written as IControl.Paint and not as 46 ITextBox.Paint. end example] 47

20.4.2 Interface mapping 48

A class or struct shall provide implementations of all members of the interfaces that are listed in the base 49 class list of the class or struct. The process of locating implementations of interface members in an 50 implementing class or struct is known as interface mapping. 51

Page 364: C# Language Specification

C# LANGUAGE SPECIFICATION

344

Interface mapping for a class or struct C locates an implementation for each member of each interface 1 specified in the base class list of C. The implementation of a particular interface member I.M, where I is the 2 interface in which the member M is declared, is determined by examining each class or struct S, starting with 3 C and repeating for each successive base class of C, until a match is located: 4

• If S contains a declaration of an explicit interface member implementation that matches I and M, then 5 this member is the implementation of I.M. 6

• Otherwise, if S contains a declaration of a non-static public member that matches M, then this member is 7 the implementation of I.M. 8

A compile-time error occurs if implementations cannot be located for all members of all interfaces specified 9 in the base class list of C. The members of an interface include those members that are inherited from base 10 interfaces. 11

Members of a constructed interface type are considered to have any type parameters replaced with the 12 corresponding type arguments as specified in §26.5.4. [Example: For example, given the generic interface 13 declaration: 14

interface I<T> 15 { 16 T F(int x, T[,] y); 17 T this[int y] { get; } 18 } 19

the constructed interface I<string[]> has the members: 20

string[] F(int x, string[,][] y); 21 string[] this[int y] { get; } 22

end example] 23

For purposes of interface mapping, a class member A matches an interface member B when: 24

• A and B are methods, and the name, type, and formal parameter lists of A and B are identical. 25

• A and B are properties, the name and type of A and B are identical, and A has the same accessors as B (A 26 is permitted to have additional accessors if it is not an explicit interface member implementation). 27

• A and B are events, and the name and type of A and B are identical. 28

• A and B are indexers, the type and formal parameter lists of A and B are identical, and A has the same 29 accessors as B (A is permitted to have additional accessors if it is not an explicit interface member 30 implementation). 31

Notable implications of the interface-mapping algorithm are: 32

• Explicit interface member implementations take precedence over other members in the same class or 33 struct when determining the class or struct member that implements an interface member. 34

• Neither non-public nor static members participate in interface mapping. 35

[Example: In the following code 36

interface ICloneable 37 { 38 object Clone(); 39 } 40

class C: ICloneable 41 { 42 object ICloneable.Clone() {…} 43

public object Clone() {…} 44 } 45

the ICloneable.Clone member of C becomes the implementation of Clone in ICloneable because 46 explicit interface member implementations take precedence over other members. end example] 47

Page 365: C# Language Specification

Chapter 20 Interfaces

345

If a class or struct implements two or more interfaces containing a member with the same name, type, and 1 parameter types, it is possible to map each of those interface members onto a single class or struct member. 2 [Example: 3

interface IControl 4 { 5 void Paint(); 6 } 7

interface IForm 8 { 9 void Paint(); 10 } 11

class Page: IControl, IForm 12 { 13 public void Paint() {…} 14 } 15

Here, the Paint methods of both IControl and IForm are mapped onto the Paint method in Page. It is 16 of course also possible to have separate explicit interface member implementations for the two methods. end 17 example] 18

If a class or struct implements an interface that contains hidden members, then some members shall 19 necessarily be implemented through explicit interface member implementations. [Example: 20

interface IBase 21 { 22 int P { get; } 23 } 24

interface IDerived: IBase 25 { 26 new int P(); 27 } 28

An implementation of this interface would require at least one explicit interface member implementation, 29 and would take one of the following forms 30

class C: IDerived 31 { 32 int IBase.P { get {…} } 33

int IDerived.P() {…} 34 } 35

class C: IDerived 36 { 37 public int P { get {…} } 38

int IDerived.P() {…} 39 } 40

class C: IDerived 41 { 42 int IBase.P { get {…} } 43

public int P() {…} 44 } 45

end example] 46

When a class implements multiple interfaces that have the same base interface, there can be only one 47 implementation of the base interface. [Example: In the following code 48

interface IControl 49 { 50 void Paint(); 51 } 52

Page 366: C# Language Specification

C# LANGUAGE SPECIFICATION

346

interface ITextBox: IControl 1 { 2 void SetText(string text); 3 } 4

interface IListBox: IControl 5 { 6 void SetItems(string[] items); 7 } 8

class ComboBox: IControl, ITextBox, IListBox 9 { 10 void IControl.Paint() {…} 11

void ITextBox.SetText(string text) {…} 12

void IListBox.SetItems(string[] items) {…} 13 } 14

it is not possible to have separate implementations for the IControl named in the base class list, the 15 IControl inherited by ITextBox, and the IControl inherited by IListBox. Indeed, there is no notion of 16 a separate identity for these interfaces. Rather, the implementations of ITextBox and IListBox share the 17 same implementation of IControl, and ComboBox is simply considered to implement three interfaces, 18 IControl, ITextBox, and IListBox. end example] 19

The members of a base class participate in interface mapping. [Example: In the following code 20

interface Interface1 21 { 22 void F(); 23 } 24

class Class1 25 { 26 public void F() {} 27

public void G() {} 28 } 29

class Class2: Class1, Interface1 30 { 31 new public void G() {} 32 } 33

the method F in Class1 is used in Class2's implementation of Interface1. end example] 34

20.4.3 Interface implementation inheritance 35

A class inherits all interface implementations provided by its base classes. 36

Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface 37 mappings it inherits from its base classes. [Example: In the declarations 38

interface IControl 39 { 40 void Paint(); 41 } 42

class Control: IControl 43 { 44 public void Paint() {…} 45 } 46

class TextBox: Control 47 { 48 new public void Paint() {…} 49 } 50

the Paint method in TextBox hides the Paint method in Control, but it does not alter the mapping of 51 Control.Paint onto IControl.Paint, and calls to Paint through class instances and interface 52 instances will have the following effects 53

Page 367: C# Language Specification

Chapter 20 Interfaces

347

Control c = new Control(); 1 TextBox t = new TextBox(); 2 IControl ic = c; 3 IControl it = t; 4 c.Paint(); // invokes Control.Paint(); 5 t.Paint(); // invokes TextBox.Paint(); 6 ic.Paint(); // invokes Control.Paint(); 7 it.Paint(); // invokes Control.Paint(); 8

end example] 9

However, when an interface method is mapped onto a virtual method in a class, it is possible for derived 10 classes to override the virtual method and alter the implementation of the interface. [Example: Rewriting the 11 declarations above to 12

interface IControl 13 { 14 void Paint(); 15 } 16

class Control: IControl 17 { 18 public virtual void Paint() {…} 19 } 20

class TextBox: Control 21 { 22 public override void Paint() {…} 23 } 24

the following effects will now be observed 25

Control c = new Control(); 26 TextBox t = new TextBox(); 27 IControl ic = c; 28 IControl it = t; 29 c.Paint(); // invokes Control.Paint(); 30 t.Paint(); // invokes TextBox.Paint(); 31 ic.Paint(); // invokes Control.Paint(); 32 it.Paint(); // invokes TextBox.Paint(); 33

end example] 34

Since explicit interface member implementations cannot be declared virtual, it is not possible to override an 35 explicit interface member implementation. However, it is perfectly valid for an explicit interface member 36 implementation to call another method, and that other method can be declared virtual to allow derived 37 classes to override it. [Example: 38

interface IControl 39 { 40 void Paint(); 41 } 42

class Control: IControl 43 { 44 void IControl.Paint() { PaintControl(); } 45

protected virtual void PaintControl() {…} 46 } 47

class TextBox: Control 48 { 49 protected override void PaintControl() {…} 50 } 51

Here, classes derived from Control can specialize the implementation of IControl.Paint by overriding 52 the PaintControl method. end example] 53

Page 368: C# Language Specification

C# LANGUAGE SPECIFICATION

348

20.4.4 Interface re-implementation 1

A class that inherits an interface implementation is permitted to re-implement the interface by including it in 2 the base class list. 3

A re-implementation of an interface follows exactly the same interface mapping rules as an initial 4 implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the 5 interface mapping established for the re-implementation of the interface. [Example: In the declarations 6

interface IControl 7 { 8 void Paint(); 9 } 10

class Control: IControl 11 { 12 void IControl.Paint() {…} 13 } 14

class MyControl: Control, IControl 15 { 16 public void Paint() {} 17 } 18

the fact that Control maps IControl.Paint onto Control.IControl.Paint doesn’t affect the re-19 implementation in MyControl, which maps IControl.Paint onto MyControl.Paint. end example] 20

Inherited public member declarations and inherited explicit interface member declarations participate in the 21 interface mapping process for re-implemented interfaces. [Example: 22

interface IMethods 23 { 24 void F(); 25 void G(); 26 void H(); 27 void I(); 28 } 29

class Base: IMethods 30 { 31 void IMethods.F() {} 32 void IMethods.G() {} 33 public void H() {} 34 public void I() {} 35 } 36

class Derived: Base, IMethods 37 { 38 public void F() {} 39 void IMethods.H() {} 40 } 41

Here, the implementation of IMethods in Derived maps the interface methods onto Derived.F, 42 Base.IMethods.G, Derived.IMethods.H, and Base.I. end example] 43

When a class implements an interface, it implicitly also implements all of that interface’s base interfaces. 44 Likewise, a re-implementation of an interface is also implicitly a re-implementation of all of the interface’s 45 base interfaces. [Example: 46

interface IBase 47 { 48 void F(); 49 } 50

interface IDerived: IBase 51 { 52 void G(); 53 } 54

class C: IDerived 55 { 56 void IBase.F() {…} 57

Page 369: C# Language Specification

Chapter 20 Interfaces

349

void IDerived.G() {…} 1 } 2

class D: C, IDerived 3 { 4 public void F() {…} 5

public void G() {…} 6 } 7

Here, the re-implementation of IDerived also re-implements IBase, mapping IBase.F onto D.F. end 8 example] 9

20.4.5 Abstract classes and interfaces 10

Like a non-abstract class, an abstract class shall provide implementations of all members of the interfaces 11 that are listed in the base class list of the class. However, an abstract class is permitted to map interface 12 methods onto abstract methods. [Example: 13

interface IMethods 14 { 15 void F(); 16 void G(); 17 } 18

abstract class C: IMethods 19 { 20 public abstract void F(); 21 public abstract void G(); 22 } 23

Here, the implementation of IMethods maps F and G onto abstract methods, which shall be overridden in 24 non-abstract classes that derive from C. end example] 25

Explicit interface member implementations cannot be abstract, but explicit interface member 26 implementations are of course permitted to call abstract methods. [Example: 27

interface IMethods 28 { 29 void F(); 30 void G(); 31 } 32

abstract class C: IMethods 33 { 34 void IMethods.F() { FF(); } 35 void IMethods.G() { GG(); } 36 protected abstract void FF(); 37 protected abstract void GG(); 38 } 39

Here, non-abstract classes that derive from C would be required to override FF and GG, thus providing the 40 actual implementation of IMethods. end example] 41

Page 370: C# Language Specification
Page 371: C# Language Specification

Chapter 21 Enums

351

21. Enums 1

An enum type is a distinct type that declares a set of named constants. [Example: The example 2

enum Color 3 { 4 Red, 5 Green, 6 Blue 7 } 8

declares an enum type named Color with members Red, Green, and Blue. end example] 9

21.1 Enum declarations 10

An enum declaration declares a new enum type. An enum declaration begins with the keyword enum, and 11 defines the name, accessibility, underlying type, and members of the enum. 12

enum-declaration: 13 attributesopt enum-modifiersopt enum identifier enum-baseopt enum-body ;opt 14

enum-base: 15 : integral-type 16

enum-body: 17 { enum-member-declarationsopt } 18 { enum-member-declarations , } 19

Each enum type has a corresponding integral type called the underlying type of the enum type. This 20 underlying type shall be able to represent all the enumerator values defined in the enumeration. An enum 21 declaration can explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or 22 ulong. [Note: char cannot be used as an underlying type. end note] An enum declaration that does not 23 explicitly declare an underlying type has an underlying type of int. 24

[Example: The example 25

enum Color: long 26 { 27 Red, 28 Green, 29 Blue 30 } 31

declares an enum with an underlying type of long. end example] [Note: A developer might choose to use an 32 underlying type of long, as in the example, to enable the use of values that are in the range of long but not 33 in the range of int, or to preserve this option for the future. end note] 34

[Note: C# allows a trailing comma in an enum-body, just like it allows one in an array-initializer (§19.6). 35 end note] 36

21.2 Enum modifiers 37

An enum-declaration can optionally include a sequence of enum modifiers: 38

enum-modifiers: 39 enum-modifier 40 enum-modifiers enum-modifier 41

Page 372: C# Language Specification

C# LANGUAGE SPECIFICATION

352

enum-modifier: 1 new 2 public 3 protected 4 internal 5 private 6

It is a compile-time error for the same modifier to appear multiple times in an enum declaration. 7

The modifiers of an enum declaration have the same meaning as those of a class declaration (§17.1.1). 8 However, the abstract and sealed modifiers are not permitted in an enum declaration. Enums cannot be 9 abstract and do not permit derivation. 10

21.3 Enum members 11

The body of an enum type declaration defines zero or more enum members, which are the named constants 12 of the enum type. No two enum members can have the same name. 13

enum-member-declarations: 14 enum-member-declaration 15 enum-member-declarations , enum-member-declaration 16

enum-member-declaration: 17 attributesopt identifier 18 attributesopt identifier = constant-expression 19

Each enum member has an associated constant value. The type of this value is the underlying type for the 20 containing enum. The constant value for each enum member shall be in the range of the underlying type for 21 the enum. [Example: The example 22

enum Color: uint 23 { 24 Red = -1, 25 Green = -2, 26 Blue = -3 27 } 28

results in a compile-time error because the constant values -1, -2, and –3 are not in the range of the 29 underlying integral type uint. end example] 30

Multiple enum members can share the same associated value. [Example: The example 31

enum Color 32 { 33 Red, 34 Green, 35 Blue, 36 37 Max = Blue 38 } 39

shows an enum that has two enum members—Blue and Max—that have the same associated value. end 40 example] 41

The associated value of an enum member is assigned either implicitly or explicitly. If the declaration of the 42 enum member has a constant-expression initializer, the value of that constant expression, implicitly 43 converted to the underlying type of the enum, is the associated value of the enum member. If the declaration 44 of the enum member has no initializer, its associated value is set implicitly, as follows: 45

• If the enum member is the first enum member declared in the enum type, its associated value is zero. 46

• Otherwise, the associated value of the enum member is obtained by increasing the associated value of 47 the textually preceding enum member by one. This increased value shall be within the range of values 48 that can be represented by the underlying type. 49

Page 373: C# Language Specification

Chapter 21 Enums

353

[Example: The example 1

using System; 2

enum Color 3 { 4 Red, 5 Green = 10, 6 Blue 7 } 8

class Test 9 { 10 static void Main() { 11 Console.WriteLine(StringFromColor(Color.Red)); 12 Console.WriteLine(StringFromColor(Color.Green)); 13 Console.WriteLine(StringFromColor(Color.Blue)); 14 } 15

static string StringFromColor(Color c) { 16 switch (c) { 17 case Color.Red: 18 return String.Format("Red = {0}", (int) c); 19

case Color.Green: 20 return String.Format("Green = {0}", (int) c); 21

case Color.Blue: 22 return String.Format("Blue = {0}", (int) c); 23

default: 24 return "Invalid color"; 25 } 26 } 27 } 28

prints out the enum member names and their associated values. The output is: 29

Red = 0 30 Green = 10 31 Blue = 11 32

for the following reasons: 33

• the enum member Red is automatically assigned the value zero (since it has no initializer and is the first 34 enum member); 35

• the enum member Green is explicitly given the value 10; 36

• and the enum member Blue is automatically assigned the value one greater than the member that 37 textually precedes it. 38

end example] 39

The associated value of an enum member shall not, directly or indirectly, use the value of its own associated 40 enum member. Other than this circularity restriction, enum member initializers can freely refer to other 41 enum member initializers, regardless of their textual position. Within an enum member initializer, values of 42 other enum members are always treated as having the type of their underlying type, so that casts are not 43 necessary when referring to other enum members. 44

[Example: The example 45

enum Circular 46 { 47 A = B, 48 B 49 } 50

results in a compile-time error because the declarations of A and B are circular. A depends on B explicitly, 51 and B depends on A implicitly. end example] 52

Page 374: C# Language Specification

C# LANGUAGE SPECIFICATION

354

Enum members are named and scoped in a manner exactly analogous to fields within classes. The scope of 1 an enum member is the body of its containing enum type. Within that scope, enum members can be referred 2 to by their simple name. From all other code, the name of an enum member shall be qualified with the name 3 of its enum type. Enum members do not have any declared accessibility—an enum member is accessible if 4 its containing enum type is accessible. 5

21.4 The System.Enum type 6

The type System.Enum is the abstract base class of all enum types (this is distinct and different from the 7 underlying type of the enum type), and the members inherited from System.Enum are available in any 8 enum type. A boxing conversion (§11.3.1) exists from any enum type to System.Enum, and an unboxing 9 conversion (§11.3.2) exists from System.Enum to any enum type. 10

Note that System.Enum is not itself an enum-type. Rather, it is a class-type from which all enum-types are 11 derived. The type System.Enum inherits from the type System.ValueType (§11.1.1), which, in turn, 12 inherits from type object. At run-time, a value of type System.Enum can be null or a reference to a 13 boxed value of any enum type. 14

21.5 Enum values and operations 15

Each enum type defines a distinct type; an explicit enumeration conversion (§13.2.2) is required to convert 16 between an enum type and an integral type, or between two enum types. The set of values that an enum type 17 can take on is not limited by its enum members. In particular, any value of the underlying type of an enum 18 can be cast to the enum type, and is a distinct valid value of that enum type. 19

Enum members have the type of their containing enum type (except within other enum member initializers: 20 see §21.3). The value of an enum member declared in enum type E with associated value v is (E)v. 21

The following operators can be used on values of enum types: ==, !=, <, >, <=, >= (§14.9.5), + (§14.7.4), 22 - (§14.7.5), ^, &, | (§14.10.2), ~ (§14.6.4), ++, -- (§14.5.9 and §14.6.5), and sizeof (§25.5.4). 23

Every enum type automatically derives from the class System.Enum (which, in turn, derives from 24 System.ValueType and object). Thus, inherited methods and properties of this class can be used on 25 values of an enum type. 26

Page 375: C# Language Specification

Chapter 22 Delegates

355

22. Delegates 1

[Note: Delegates enable scenarios that some other languages have addressed with function pointers. 2 However, unlike function pointers, delegates are object-oriented and type-safe. A delegate encapsulates both 3 an object instance and a method. end note] 4

A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instance 5 encapsulates one or more methods, each of which is referred to as a callable entity. For instance methods, a 6 callable entity consists of an instance and a method on that instance. For static methods, a callable entity 7 consists of just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all 8 of that delegate instance’s methods with that set of arguments. 9

[Note: An interesting and useful property of a delegate instance is that it does not know or care about the 10 classes of the methods it encapsulates; all that matters is that those methods be consistent (§22.1) with the 11 delegate’s type. This makes delegates perfectly suited for “anonymous” invocation. end note] 12

22.1 Delegate declarations 13

A delegate-declaration is a type-declaration (§16.6) that declares a new delegate type. 14

delegate-declaration: 15 attributesopt delegate-modifiersopt delegate return-type identifier type-parameter-listopt 16 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt ; 17

delegate-modifiers: 18 delegate-modifier 19 delegate-modifiers delegate-modifier 20

delegate-modifier: 21 new 22 public 23 protected 24 internal 25 private 26

It is a compile-time error for the same modifier to appear multiple times in a delegate declaration. 27

A delegate declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-28 parameter-list. 29

A delegate declaration that supplies a type-parameter-list is a generic delegate declaration (§26.4). 30

The new modifier is only permitted on delegates declared within another type, in which case it specifies that 31 such a delegate hides an inherited member by the same name, as described in §17.2.2. 32

The public, protected, internal, and private modifiers control the accessibility of the delegate type. 33 Depending on the context in which the delegate declaration occurs, some of these modifiers might not be 34 permitted (§10.5.1). 35

The delegate’s type name is identifier. 36

The optional formal-parameter-list specifies the parameters of the delegate, and return-type indicates the 37 return type of the delegate. A method and a delegate type are consistent if of the following are true: 38

• For each parameter of the method: 39

Page 376: C# Language Specification

C# LANGUAGE SPECIFICATION

356

o If the parameter has no ref or out modifier, then the corresponding parameter of the delegate type 1 has no ref or out modifier and there is an identity conversion or implicit reference conversion 2 from the corresponding delegate parameter type to the method parameter type. 3

o If the parameter has a ref or out modifier, then the corresponding parameter of the delegate type 4 has the same modifier and the corresponding delegate parameter type is the same as the method 5 parameter type. 6

• There is an identity conversion or implicit reference conversion from the return type of the method to 7 the return type of the delegate type. 8

This definition of consistency allows covariance in return type and contravariance in parameter types. 9

Delegate types in C# are name equivalent, not structurally equivalent. Specifically, two delegate types that 10 have the same parameter lists and return type are considered different delegate types. [Example: 11

delegate int D1(int i, double d); 12

class A 13 { 14 public static int M1(int a, double b) {…} 15 } 16

class B 17 { 18 delegate int D2(int c, double d); 19 public static int M1(int f, double g) {…} 20 public static void M2(int k, double l) {…} 21 public static int M3(int g) {…} 22 public static void M4(int g) {…} 23

delegate object D3(string s); 24 public static object M5(string s) {…} 25 public static int[] M6(object o) {…} 26 } 27

The delegate types D1 and D2 are both consistent with the methods A.M1 and B.M1, since they have the 28 same return type and parameter list; however, these delegate types are two different types, so they are not 29 interchangeable. The delegate types D1 and D2 are not consistent with the methods B.M2, B.M3, and B.M4. 30 The methods B.M5 and B.M6 are both consistent with delegate type D3. end example] 31

The only way to declare a delegate type is via a delegate-declaration. A delegate type is a reference type that 32 is derived from System.Delegate. Delegate types are implicitly sealed, so it is not permissible to derive 33 any type from a delegate type. It is also not permissible to derive a non-delegate class type from 34 System.Delegate. System.Delegate is not itself a delegate type; it is a class type from which all 35 delegate types are derived. 36

C# provides special syntax for delegate instantiation and invocation. Except for instantiation, any operation 37 that can be applied to a class or class instance can also be applied to a delegate class or instance, 38 respectively. In particular, it is possible to access members of the System.Delegate type via the usual 39 member access syntax. 40

The set of methods encapsulated by a delegate instance is called an invocation list. When a delegate instance 41 is created (§22.2) from a single method, it encapsulates that method, and its invocation list contains only one 42 entry. However, when two non-null delegate instances are combined, their invocation lists are 43 concatenated—in the order left operand then right operand—to form a new invocation list, which contains 44 two or more entries. 45

Delegates are combined using the binary + (§14.7.4) and += operators (§14.13.2). A delegate can be 46 removed from a combination of delegates, using the binary - (§14.7.5) and -= operators (§14.13.2). 47 Delegates can be compared for equality (§14.9.8). 48

[Example: The following example shows the instantiation of a number of delegates, and their corresponding 49 invocation lists: 50

Page 377: C# Language Specification

Chapter 22 Delegates

357

delegate void D(int x); 1 class Test 2 { 3 public static void M1(int i) {…} 4 public static void M2(int i) {…} 5 } 6

class Demo 7 { 8 static void Main() { 9 D cd1 = new D(Test.M1); // M1 10 D cd2 = new D(Test.M2); // M2 11 D cd3 = cd1 + cd2; // M1 + M2 12 D cd4 = cd3 + cd1; // M1 + M2 + M1 13 D cd5 = cd4 + cd3; // M1 + M2 + M1 + M1 + M2 14 } 15 } 16

When cd1 and cd2 are instantiated, they each encapsulate one method. When cd3 is instantiated, it has an 17 invocation list of two methods, M1 and M2, in that order. cd4’s invocation list contains M1, M2, and M1, in 18 that order. Finally, cd5’s invocation list contains M1, M2, M1, M1, and M2, in that order. 19

For more examples of combining (as well as removing) delegates, see §22.3. end example] 20

22.2 Delegate instantiation 21

An instance of a delegate is created by a delegate-creation-expression (§14.5.10.3) or by an implicit 22 conversion from an anonymous method or method group to a delegate type (§13.5 and §13.6). The newly 23 created delegate instance then refers to one or more of: 24

• A static method. 25

• A target object (which cannot be null) and instance method. 26

When the argument of a delegate-creation-expression (§14.5.10.3) is a delegate instance the resulting 27 delegate has the same invocation list as the argument, which may contain more than one entry. 28

[Example: 29

delegate void D(int x); 30 class Test 31 { 32 public static void M1(int i) {…} 33 public void M2(int i) {…} 34 } 35

class Demo 36 { 37 static void Main() { 38 D cd1 = new D(Test.M1); // static method 39 Test t = new Test(); 40 D cd2 = new D(t.M2); // instance method 41 D cd3 = new D(cd2); // another delegate 42 } 43 } 44

end example] 45

Once instantiated, delegate instances always refer to the same list of target objects and methods. [Note: 46 Remember, when two delegates are combined, or one is removed from another, a new delegate results with 47 its own invocation list; the invocation lists of the delegates combined or removed remain unchanged. end 48 note] 49

22.3 Delegate invocation 50

C# provides special syntax for invoking a delegate. When a non-null delegate instance whose invocation 51 list contains one entry, is invoked, it invokes the one method with the same arguments it was given, and 52 returns the same value as the referred to method. (See §14.5.5.2 for detailed information on delegate 53

Page 378: C# Language Specification

C# LANGUAGE SPECIFICATION

358

invocation.) If an exception occurs during the invocation of such a delegate, and that exception is not caught 1 within the method that was invoked, the search for an exception catch clause continues in the method that 2 called the delegate, as if that method had directly called the method to which that delegate referred. 3

Invocation of a delegate instance whose invocation list contains multiple entries, proceeds by invoking each 4 of the methods in the invocation list, synchronously, in order. Each method so called is passed the same set 5 of arguments as was given to the delegate instance. If such a delegate invocation includes reference 6 parameters (§17.5.1.2), each method invocation will occur with a reference to the same variable; changes to 7 that variable by one method in the invocation list will be visible to methods further down the invocation list. 8 If the delegate invocation includes output parameters or a return value, their final value will come from the 9 invocation of the last delegate in the list. If an exception occurs during processing of the invocation of such a 10 delegate, and that exception is not caught within the method that was invoked, the search for an exception 11 catch clause continues in the method that called the delegate, and any methods further down the invocation 12 list are not invoked. 13

Attempting to invoke a delegate instance whose value is null results in an exception of type 14 System.NullReferenceException. 15

[Example: The following example shows how to instantiate, combine, remove, and invoke delegates: 16

using System; 17

delegate void D(int x); 18 class Test 19 { 20 public static void M1(int i) { 21 Console.WriteLine("Test.M1: " + i); 22 } 23

public static void M2(int i) { 24 Console.WriteLine("Test.M2: " + i); 25 } 26

public void M3(int i) { 27 Console.WriteLine("Test.M3: " + i); 28 } 29 } 30

class Demo 31 { 32 static void Main() { 33 D cd1 = new D(Test.M1); 34 cd1(-1); // call M1 35

D cd2 = new D(Test.M2); 36 cd2(-2); // call M2 37

D cd3 = cd1 + cd2; 38 cd3(10); // call M1 then M2 39 40 cd3 += cd1; 41 cd3(20); // call M1, M2, then M1 42

Test t = new Test(); 43 D cd4 = new D(t.M3); 44 cd3 += cd4; 45 cd3(30); // call M1, M2, M1, then M3 46

cd3 -= cd1; // remove last M1 47 cd3(40); // call M1, M2, then M3 48

cd3 -= cd4; 49 cd3(50); // call M1 then M2 50

cd3 -= cd2; 51 cd3(60); // call M1 52 cd3 -= cd2; // impossible removal is benign 53 cd3(60); // call M1 54

Page 379: C# Language Specification

Chapter 22 Delegates

359

cd3 -= cd1; // invocation list is empty 1 // cd3(70); // System.NullReferenceException thrown 2 cd3 -= cd1; // impossible removal is benign 3 } 4 } 5

As shown in the statement cd3 += cd1;, a method can be present in an invocation list multiple times. In 6 this case, it is simply invoked once per occurrence. In an invocation list such as this, when that method is 7 removed, the last occurrence in the invocation list is the one actually removed. 8

Immediately prior to the execution of the final statement, cd3 -= cd1;, the delegate cd3 refers to an 9 empty invocation list. Attempting to subtract a delegate from an empty list (or to subtract a non-existent 10 delegate from a non-empty list) is not an error. 11

The output produced is: 12

Test.M1: -1 13 Test.M2: -2 14

Test.M1: 10 15 Test.M2: 10 16

Test.M1: 20 17 Test.M2: 20 18 Test.M1: 20 19

Test.M1: 30 20 Test.M2: 30 21 Test.M1: 30 22 Test.M3: 30 23

Test.M1: 40 24 Test.M2: 40 25 Test.M3: 40 26

Test.M1: 50 27 Test.M2: 50 28

Test.M1: 60 29 Test.M1: 60 30

end example] 31

Page 380: C# Language Specification
Page 381: C# Language Specification

Chapter 23 Exceptions

361

23. Exceptions 1

Exceptions in C# provide a structured, uniform, and type-safe way of handling both system level and 2 application-level error conditions. [Note: The exception mechanism in C# is quite similar to that of C++, 3 with a few important differences: 4

• In C#, all exceptions shall be represented by an instance of a class type derived from 5 System.Exception. In C++, any value of any type can be used to represent an exception. 6

• In C#, a finally block (§15.10) can be used to write termination code that executes in both normal 7 execution and exceptional conditions. C++ has no equivalent construct. 8

• In C#, system-level exceptions such as overflow, divide-by-zero, and null dereferences have well 9 defined exception classes and are on a par with application-level error conditions. 10

end note] 11

23.1 Causes of exceptions 12

Exception can be thrown in two different ways. 13

• A throw statement (§15.9.5) throws an exception immediately and unconditionally. Control never 14 reaches the statement immediately following the throw. 15

• Certain exceptional conditions that arise during the processing of C# statements and expression cause an 16 exception in certain circumstances when the operation cannot be completed normally. [Example: An 17 integer division operation (§14.7.2) throws a System.DivideByZeroException if the denominator is 18 zero. end example] See §23.4 for a list of the various exceptions that can occur in this way. 19

23.2 The System.Exception class 20

The System.Exception class is the base type of all exceptions. This class has a few notable properties 21 that all exceptions share: 22

• Message is a read-only property of type string that contains a human-readable description of the 23 reason for the exception. 24

• InnerException is a read-only property of type Exception. If its value is non-null, it refers to the 25 exception that caused the current exception. (That is, the current exception was raised in a catch block 26 handling the type InnerException.) Otherwise, its value is null, indicating that this exception was not 27 caused by another exception. (The number of exception objects chained together in this manner can be 28 arbitrary.) 29

The value of these properties can be specified in calls to the instance constructor for System.Exception. 30

[Note: The Standard Library provides two types that inherit directly from System.Exception: 31 System.SystemException and System.ApplicationException. These classes are provided as a 32 means to differentiate between exceptions defined by the system versus exceptions defined by applications, 33 respectively. As such, it is recommended that exception classes be derived from one of these classes as 34 appropriate, rather than from System.Exception directly. end note] 35

23.3 How exceptions are handled 36

Exceptions are handled by a try statement (§15.10). 37

When an exception occurs, the system searches for the nearest catch clause that can handle the exception, 38 as determined by the run-time type of the exception. First, the current method is searched for a lexically 39

Page 382: C# Language Specification

C# LANGUAGE SPECIFICATION

362

enclosing try statement, and the associated catch clauses of the try statement are considered in order. If 1 that fails, the method that called the current method is searched for a lexically enclosing try statement that 2 encloses the point of the call to the current method. This search continues until a catch clause is found that 3 can handle the current exception, by naming an exception class that is of the same class, or a base class, of 4 the run-time type of the exception being thrown. A catch clause that doesn’t name an exception class can 5 handle any exception. 6

Once a matching catch clause is found, the system prepares to transfer control to the first statement of the 7 catch clause. Before execution of the catch clause begins, the system first executes, in order, any 8 finally clauses that were associated with try statements more nested that than the one that caught the 9 exception. 10

If no matching catch clause is found, one of two things occurs: 11

• If the search for a matching catch clause reaches a static constructor (§17.11) or static field initializer, 12 then a System.TypeInitializationException is thrown at the point that triggered the invocation 13 of the static constructor. The inner exception of the System.TypeInitializationException 14 contains the exception that was originally thrown. 15

• If the search for matching catch clauses reaches the code that initially started the thread, then execution 16 of the thread is terminated. The impact of such termination is implementation-defined. 17

Exceptions that occur during destructor execution are worth special mention. If an exception occurs during 18 destructor execution, and that exception is not caught, then the execution of that destructor is terminated and 19 the destructor of the base class (if any) is called. If there is no base class (as in the case of the object type) 20 or if there is no base class destructor, then the exception is discarded. 21

23.4 Common Exception Classes 22

The following exceptions are thrown by certain C# operations. 23

24

Page 383: C# Language Specification

Chapter 23 Exceptions

363

System.ArithmeticException A base class for exceptions that occur during arithmetic operations, such as System.DivideByZeroException and System.OverflowException.

System.ArrayTypeMismatchException Thrown when a store into an array fails because the actual type of the stored element is incompatible with the actual type of the array.

System.DivideByZeroException Thrown when an attempt to divide an integral value by zero occurs.

System.IndexOutOfRangeException Thrown when an attempt to index an array via an index that is less than zero or outside the bounds of the array.

System.InvalidCastException Thrown when an explicit conversion from a base type or interface to a derived type fails at run time.

System.NullReferenceException Thrown when a null reference is used in a way that causes the referenced object to be required.

System.OutOfMemoryException Thrown when an attempt to allocate memory (via new) fails.

System.OverflowException Thrown when an arithmetic operation in a checked context overflows.

System.StackOverflowException Thrown when the execution stack is exhausted by having too many pending method calls; typically indicative of very deep or unbounded recursion.

System.TypeInitializationException Thrown when a static constructor throws an exception, and no catch clause exists to catch it.

1

Page 384: C# Language Specification
Page 385: C# Language Specification

Chapter 24 Attributes

365

24. Attributes 1

[Note: Much of the C# language enables the programmer to specify declarative information about the 2 entities defined in the program. For example, the accessibility of a method in a class is specified by 3 decorating it with the method-modifiers public, protected, internal, and private. end note] 4

C# enables programmers to invent new kinds of declarative information, called attributes. Programmers can 5 then attach attributes to various program entities, and retrieve attribute information in a run-time 6 environment. [Note: For instance, a framework might define a HelpAttribute attribute that can be placed 7 on certain program elements (such as classes and methods) to provide a mapping from those program 8 elements to their documentation. end note] 9

Attributes are defined through the declaration of attribute classes (§24.1), which can have positional and 10 named parameters (§24.1.2). Attributes are attached to entities in a C# program using attribute specifications 11 (§24.2), and can be retrieved at run-time as attribute instances (§24.3). 12

24.1 Attribute classes 13

A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an 14 attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a 15 declaration. By convention, attribute classes are named with a suffix of Attribute. Uses of an attribute can 16 either include or omit this suffix. 17

A generic class declaration shall not use System.Attribute as a direct or indirect base class. [Example: 18

using System; 19

public class B : Attribute {} 20

public class C<T> : B {} // Error – generic cannot be an attribute 21

end example] 22

24.1.1 Attribute usage 23

The attribute AttributeUsage (§24.4.1) is used to describe how an attribute class can be used. 24

AttributeUsage has a positional parameter (§24.1.2) that enables an attribute class to specify the kinds of 25 declarations on which it can be used. [Example: The example 26

using System; 27

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] 28 public class SimpleAttribute: Attribute 29 {} 30

defines an attribute class named SimpleAttribute that can be placed on class-declarations and interface-31 declarations only. The example 32

[Simple] class Class1 {…} 33

[Simple] interface Interface1 {…} 34

shows several uses of the Simple attribute. Although this attribute is defined with the name 35 SimpleAttribute, when this attribute is used, the Attribute suffix can be omitted, resulting in the short 36 name Simple. Thus, the example above is semantically equivalent to the following 37

[SimpleAttribute] class Class1 {…} 38

[SimpleAttribute] interface Interface1 {…} 39

end example] 40

Page 386: C# Language Specification

C# LANGUAGE SPECIFICATION

366

AttributeUsage has a named parameter (§24.1.2), called AllowMultiple, which indicates whether the 1 attribute can be specified more than once for a given entity. If AllowMultiple for an attribute class is true, 2 then that class is a multi-use attribute class, and can be specified more than once on an entity. If 3 AllowMultiple for an attribute class is false or it is unspecified, then that class is a single-use attribute 4 class, and can be specified at most once on an entity. 5

[Example: The example 6

using System; 7

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] 8 public class AuthorAttribute: Attribute 9 { 10 public AuthorAttribute(string name) { 11 this.name = name; 12 } 13 public string Name { get { return name;} } 14 private string name; 15 } 16

defines a multi-use attribute class named AuthorAttribute. The example 17

[Author("Brian Kernighan"), Author("Dennis Ritchie")] 18 class Class1 {…} 19

shows a class declaration with two uses of the Author attribute. end example] 20

AttributeUsage has another named parameter (§24.1.2), called Inherited, which indicates whether the 21 attribute, when specified on a base class, is also inherited by classes that derive from that base class. If 22 Inherited for an attribute class is true, then that attribute is inherited. If Inherited for an attribute class 23 is false then that attribute is not inherited. If it is unspecified, its default value is true. 24

An attribute class X not having an AttributeUsage attribute attached to it, as in 25

using System; 26

class X: Attribute { … } 27

is equivalent to the following: 28

using System; 29

[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = 30 true)] 31 class X: Attribute { … } 32

24.1.2 Positional and named parameters 33

Attribute classes can have positional parameters and named parameters. Each public instance constructor 34 for an attribute class defines a valid sequence of positional parameters for that attribute class. Each non-35 static public read-write field and property for an attribute class defines a named parameter for the attribute 36 class. Both accessors of a property need to be public for the property to define a named parameter. 37

[Example: The example 38

using System; 39

[AttributeUsage(AttributeTargets.Class)] 40 public class HelpAttribute: Attribute 41 { 42

public HelpAttribute(string url) { // url is a positional parameter 43 … 44 } 45

public string Topic { // Topic is a named parameter 46 get {…} 47 set {…} 48 } 49

public string Url { get {…} } 50 } 51

Page 387: C# Language Specification

Chapter 24 Attributes

367

defines an attribute class named HelpAttribute that has one positional parameter (string url) and one 1 named parameter (string Topic). Although it is non-static and public, the property Url does not define a 2 named parameter, since it is not read-write. 3

This attribute class might be used as follows: 4

[Help("http://www.mycompany.com/…/Class1.htm")] 5 class Class1 6 { 7 } 8

[Help("http://www.mycompany.com/…/Misc.htm", Topic ="Class2")] 9 class Class2 10 { 11 } 12

end example] 13

24.1.3 Attribute parameter types 14

The types of positional and named parameters for an attribute class are limited to the attribute parameter 15 types, which are: 16

• One of the following types: bool, byte, char, double, float, int, long, short, string. 17

• The type object. 18

• The type System.Type. 19

• An enum type, provided it has public accessibility and the types in which it is nested (if any) also have 20 public accessibility. 21

• Single-dimensional arrays of the above types. 22

24.2 Attribute specification 23

Attribute specification is the application of a previously defined attribute to a declaration. An attribute is a 24 piece of additional declarative information that is specified for a declaration. Attributes can be specified at 25 global scope (to specify attributes on the containing assembly) and for type-declarations (§16.6), class-26 member-declarations (§17.1.4), struct-member-declarations (§18.2), interface-member-declarations 27 (§20.2), enum-member-declarations (§21.1), accessor-declarations (§17.6.2), event-accessor-declarations 28 (§17.7), elements of formal-parameter-lists (§17.5.1), and elements of type-parameter-lists (§26.1.1). 29

Attributes are specified in attribute sections. An attribute section consists of a pair of square brackets, which 30 surround a comma-separated list of one or more attributes. The order in which attributes are specified in 31 such a list, and the order in which sections attached to the same program entity are arranged, is not 32 significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent. 33

global-attributes: 34 global-attribute-sections 35

global-attribute-sections: 36 global-attribute-section 37 global-attribute-sections global-attribute-section 38

global-attribute-section: 39 [ global-attribute-target-specifier attribute-list ] 40 [ global-attribute-target-specifier attribute-list , ] 41

global-attribute-target-specifier: 42 global-attribute-target : 43

global-attribute-target: 44 identifier-or-keyword 45

Page 388: C# Language Specification

C# LANGUAGE SPECIFICATION

368

attributes: 1 attribute-sections 2

attribute-sections: 3 attribute-section 4 attribute-sections attribute-section 5

attribute-section: 6 [ attribute-target-specifieropt attribute-list ] 7 [ attribute-target-specifieropt attribute-list , ] 8

attribute-target-specifier: 9 attribute-target : 10

attribute-target: 11 identifier-or-keyword 12

attribute-list: 13 attribute 14 attribute-list , attribute 15

attribute: 16 attribute-name attribute-argumentsopt 17

attribute-name: 18 type-name 19

attribute-arguments: 20 ( positional-argument-listopt ) 21 ( positional-argument-list , named-argument-list ) 22 ( named-argument-list ) 23

positional-argument-list: 24 positional-argument 25 positional-argument-list , positional-argument 26

positional-argument: 27 attribute-argument-expression 28

named-argument-list: 29 named-argument 30 named-argument-list , named-argument 31

named-argument: 32 identifier = attribute-argument-expression 33

attribute-argument-expression: 34 expression 35

An attribute consists of an attribute-name and an optional list of positional and named arguments. The 36 positional arguments (if any) precede the named arguments. A positional argument consists of an attribute-37 argument-expression; a named argument consists of a name, followed by an equal sign, followed by an 38 attribute-argument-expression, which, together, are constrained by the same rules as simple assignment. The 39 order of named arguments is not significant. 40

[Note: For convenience, a trailing comma is allowed in a global-attribute-section and an attribute-section, 41 just as one is allowed in an array-initializer (§19.6). end note] 42

The attribute-name identifies an attribute class. type-name shall refer to an attribute class. Otherwise, a 43 compile-time error occurs. [Example: The example 44

class Class1 {} 45

[Class1] class Class2 {} // Error 46

Page 389: C# Language Specification

Chapter 24 Attributes

369

results in a compile-time error because it attempts to use Class1 as an attribute class when Class1 is not 1 an attribute class. end example] 2

When an attribute is placed at the global level, a global-attribute-target-specifier is required. The only 3 standardized global-attribute-target name is assembly. This target name shall only be used in the context 4 of an assembly. 5

The only standardized attribute-target names are event, field, method, param, property, return, and 6 type. These target names shall only be used in the following contexts: 7

• event — an event. 8

• field — a field. A field-like event (i.e., one without accessors) can also have an attribute with this 9 target. 10

• method — a constructor, destructor, method, operator, property get and set accessors, and event add and 11 remove accessors. A field-like event (i.e., one without accessors) can also have an attribute with this 12 target. 13

• param — a property set accessor, event add and remove accessors, and a parameter in a constructor, 14 method, and operator. 15

• property — a property. 16

• return — a delegate, method, operator, and property get accessor. 17

• type — a delegate, class, struct, and interface. 18

• typevar — a type parameter. 19

Certain contexts permit the specification of an attribute on more than one target. A program can explicitly 20 specify the target by including an attribute-target-specifier. In the absence of an attribute-target-specifier, a 21 reasonable default is applied, but an attribute-target-specifier can be used to affirm or override the default in 22 certain ambiguous cases (or to just affirm the default in non-ambiguous cases). Thus, typically, attribute-23 target-specifiers can be omitted. The potentially ambiguous contexts are resolved as follows (using equality 24 as defined in §9.4.2): 25

• An attribute specified on a delegate declaration can apply either to the delegate being declared or to its 26 return value. In the absence of an attribute-target-specifier, the attribute applies to the delegate. An 27 attribute-target equal to type indicates that the attribute applies to the delegate; an attribute-target 28 equal to return indicates that the attribute applies to the return value. 29

• An attribute specified on a method declaration can apply either to the method being declared or to its 30 return value. In the absence of an attribute-target-specifier, the attribute applies to the method. An 31 attribute-target equal to method indicates that the attribute applies to the method; an attribute-target 32 equal to return indicates that the attribute applies to the return value. 33

• An attribute specified on an operator declaration can apply either to the operator being declared or to its 34 return value. In the absence of an attribute-target-specifier, the attribute applies to the operator. An 35 attribute-target equal to method indicates that the attribute applies to the operator; an attribute-target 36 equal to return indicates that the attribute applies to the return value. 37

• An attribute specified on a get accessor declaration for a property or indexer declaration can apply either 38 to the associated method or to its return value. In the absence of an attribute-target-specifier, the 39 attribute applies to the method. An attribute-target equal to method indicates that the attribute applies to 40 the method; an attribute-target equal to return indicates that the attribute applies to the return value. 41

• An attribute specified on a set accessor for a property or indexer declaration can apply either to the 42 associated method or to its lone implicit parameter. In the absence of an attribute-target-specifier, the 43 attribute applies to the method. An attribute-target equal to method indicates that the attribute applies to 44 the method; the presence of an attribute-target equal to param indicates that the attribute applies to the 45 parameter. 46

Page 390: C# Language Specification

C# LANGUAGE SPECIFICATION

370

• An attribute specified on an event declaration that omits event-accessor-declarations can apply to the 1 event being declared, to the associated field (if the event is not abstract), or to the associated add and 2 remove methods. In the absence of an attribute-target-specifier, the attribute applies to the event 3 declaration. An attribute-target equal to event indicates that the attribute applies to the event; an 4 attribute-target equal to field indicates that the attribute applies to the field; and an attribute-target 5 equal to method indicates that the attribute applies to the methods. 6

• In the case of an event declaration that does not omit event-accessor-declarations, an attribute specified 7 on an add or remove accessor declaration for an event declaration can apply either to the associated 8 method or to its lone parameter. In the absence of an attribute-target-specifier, the attribute applies to 9 the method. An attribute-target equal to method indicates that the attribute applies to the method; an 10 attribute-target equal to param indicates that the attribute applies to the parameter. 11

An implementation can accept other attribute target specifiers, the purposes of which are implementation-12 defined. However, an implementation that does not recognize such a target, shall issue a warning. 13

By convention, attribute classes are named with a suffix of Attribute. An attribute-name can either 14 include or omit this suffix. Specifically, an attribute-name is resolved as follows: 15

• If the right-most identifier of the attribute-name is a verbatim identifier (§9.4.2), then the attribute-name 16 is resolved as a type-name (§10.8). If the result is not a type derived from System.Attribute, a 17 compile-time error occurs. 18

• Otherwise, 19

o The attribute-name is resolved as a type-name (§10.8) except any errors are suppressed. If this 20 resolution is successful and results in a type derived from System.Attribute then the type is the 21 result of this step. 22

o The characters Attribute are appended to the right-most identifier in the attribute-name and the 23 resulting string of tokens is resolved as a type-name (§10.8) except any errors are suppressed. If this 24 resolution is successful and results in a type derived from System.Attribute then the type is the 25 result of this step. 26

If exactly one of the two steps above results in a type derived from System.Attribute, then that type 27 is the result of the attribute-name. Otherwise a compile-time error occurs. 28

[Example: Informally, when attempting to resolve an attribute-name, if an attribute class is found both with 29 and without the Attribute suffix, an ambiguity is present, and a compile-time error is issued. If the 30 attribute-name is spelled such that its right-most identifier is a verbatim identifier (§9.4.2), then only an 31 attribute without a suffix is matched, thus enabling such an ambiguity to be resolved. The example 32

using System; 33

[AttributeUsage(AttributeTargets.All)] 34 public class X: Attribute 35 {} 36

[AttributeUsage(AttributeTargets.All)] 37 public class XAttribute: Attribute 38 {} 39

[X] // error: ambiguity 40 class Class1 {} 41

[XAttribute] // refers to XAttribute 42 class Class2 {} 43

[@X] // refers to X 44 class Class3 {} 45

[@XAttribute] // refers to XAttribute 46 class Class4 {} 47

shows two attribute classes named X and XAttribute. The attribute reference [X] is ambiguous, since it 48 could refer to either X or XAttribute. Using a verbatim identifier allows the exact intent to be specified in 49 such rare cases. The attribute reference [XAttribute] is not ambiguous (although it would be if there were 50

Page 391: C# Language Specification

Chapter 24 Attributes

371

an attribute class named XAttributeAttribute!). If the declaration for class X is removed, then both 1 attributes refer to the attribute class named XAttribute, as follows: 2

using System; 3

[AttributeUsage(AttributeTargets.All)] 4 public class XAttribute: Attribute 5 {} 6

[X] // refers to XAttribute 7 class Class1 {} 8

[XAttribute] // refers to XAttribute 9 class Class2 {} 10

[@X] // error: no attribute named “X” 11 class Class3 {} 12

end example] 13

It is a compile-time error to use a single-use attribute class more than once on the same entity. [Example: 14 The example 15

using System; 16

[AttributeUsage(AttributeTargets.Class)] 17 public class HelpStringAttribute: Attribute 18 { 19 string value; 20

public HelpStringAttribute(string value) { 21 this.value = value; 22 } 23

public string Value { get {…} } 24 } 25

[HelpString("Description of Class1")] 26 [HelpString("Another description of Class1")] 27 public class Class1 {} 28

results in a compile-time error because it attempts to use HelpString, which is a single-use attribute class, 29 more than once on the declaration of Class1. end example] 30

An expression E is an attribute-argument-expression if all of the following statements are true: 31

• The type of E is an attribute parameter type (§24.1.3). 32

• At compile-time, the value of E can be resolved to one of the following: 33

o A constant value. 34

o Atypeof-expression (§14.5.11) specifying a non-generic type, a closed constructed type (§26.5.2), or 35 an unbound generic type (§26.5). 36

o A one-dimensional array of attribute-argument-expressions. 37

[Example: 38

using System; 39

[AttributeUsage(AttributeTargets.Class)] 40 public class MyAttribute: Attribute 41 { 42 public int P1 { 43 get {…} 44 set {…} 45 } 46

public Type P2 { 47 get {…} 48 set {…} 49 } 50

Page 392: C# Language Specification

C# LANGUAGE SPECIFICATION

372

public object P3 { 1 get {…} 2 set {…} 3 } 4 } 5

[My(P1 = 1234, P3 = new int[]{1, 3, 5}, P2 = typeof(float))] 6 class MyClass {} 7

class C<T> { 8 [My(P2 = typeof(T))] // Error – T not a closed type. 9 int x1; 10

[My(P2 = typeof(C<T>))] // Error – C<T> not a closed type. 11 int x2; 12

[My(P2 = typeof(C<int>))] // Ok 13 int x3; 14

[My(P2 = typeof(C<>))] // Ok 15 int x4; 16 } 17

end example] 18

The attributes of a type declared in multiple parts are determined by combining, in an unspecified order, the 19 attributes of each of its parts. If the same attribute is placed on multiple parts, it is equivalent to specifying 20 that attribute multiple times on the type. [Example: The two parts: 21

[Attr1, Attr2("hello")] 22 partial class A {} 23

[Attr3, Attr2("goodbye")] 24 partial class A {} 25

are equivalent to the following single declaration: 26

[Attr1, Attr2("hello"), Attr3, Attr2("goodbye")] 27 class A {} 28

end example] 29

Attributes on type parameters combine in the same way. 30

24.3 Attribute instances 31

An attribute instance is an instance that represents an attribute at run-time. An attribute is defined with an 32 attribute class, positional arguments, and named arguments. An attribute instance is an instance of the 33 attribute class that is initialized with the positional and named arguments. 34

Retrieval of an attribute instance involves both compile-time and run-time processing, as described in the 35 following subclauses. 36

24.3.1 Compilation of an attribute 37

The compilation of an attribute with attribute class T, positional-argument-list P and named-argument-list N, 38 consists of the following steps: 39

• Follow the compile-time processing steps for compiling an object-creation-expression of the form 40 new T(P). These steps either result in a compile-time error, or determine an instance constructor on T 41 that can be invoked at run-time. Call this instance constructor C. 42

• If C does not have public accessibility, then a compile-time error occurs. 43

• For each named-argument Arg in N: 44

o Let Name be the identifier of the named-argument Arg. 45

o Name shall identify a non-static read-write public field or property on T. If T has no such field or 46 property, then a compile-time error occurs. 47

Page 393: C# Language Specification

Chapter 24 Attributes

373

• Keep the following information for run-time instantiation of the attribute: the attribute class T, the 1 instance constructor C on T, the positional-argument-list P and the named-argument-list N, with the 2 values resolved completely at compile-time. 3

24.3.2 Run-time retrieval of an attribute instance 4

Compilation of an attribute yields an attribute class T, an instance constructor C on T, a positional-argument-5 list P, and a named-argument-list N. Given this information, an attribute instance can be retrieved at run-time 6 using the following steps: 7

• Follow the run-time processing steps for executing an object-creation-expression of the form 8 new T(P), using the instance constructor C and values as determined at compile-time. These steps 9 either result in an exception, or produce an instance of T. Call this instance O. 10

• For each named-argument Arg in N, in order: 11

o Let Name be the identifier of the named-argument Arg. If Name does not identify a non-static public 12 read-write field or property on O, then an exception is thrown. 13

o Let Value be the result of evaluating the attribute-argument-expression of Arg. 14

o If Name identifies a field on O, then set this field to the value Value. 15

o Otherwise, Name identifies a property on O. Set this property to the value Value. 16

o The result is O, an instance of the attribute class T that has been initialized with the positional-17 argument-list P and the named-argument-list N. 18

24.4 Reserved attributes 19

The following attributes affect the language, as stated: 20

• System.AttributeUsageAttribute (§24.4.1), which is used to describe the ways in which an 21 attribute class can be used. 22

• System.ConditionalAttribute (§24.4.2), which is used to define conditional methods and 23 attributes. 24

• System.ObsoleteAttribute (§24.4.3), which is used to mark a member as obsolete. 25

24.4.1 The AttributeUsage attribute 26

The attribute AttributeUsage is used to describe the manner in which the attribute class can be used. 27

A class that is decorated with the AttributeUsage attribute shall derive from System.Attribute, either 28 directly or indirectly. Otherwise, a compile-time error occurs. 29

[Note: For an example of using this attribute, see §24.1.1. end note] 30

24.4.2 The Conditional attribute 31

The attribute Conditional enables the definition of conditional methods and conditional attribute 32 classes. 33

24.4.2.1 Conditional Methods 34

A method decorated with the Conditional attribute is said to be a conditional method.The Conditional 35 attribute indicates a condition by testing a conditional compilation symbol. Calls to a conditional method are 36 either included or omitted depending on whether this symbol is defined at the point of the call. If the symbol 37 is defined, the call is included; otherwise, the call is omitted. 38

A conditional method is subject to the following restrictions: 39

Page 394: C# Language Specification

C# LANGUAGE SPECIFICATION

374

• The conditional method shall be a method in a class-declaration or struct-declaration. A compile-time 1 error occurs if the Conditional attribute is specified on an interface method. 2

• The conditional method shall have a return type of void. 3

• The conditional method shall not be marked with the override modifier. A conditional method can be 4 marked with the virtual modifier, however. Overrides of such a method are implicitly conditional, 5 and shall not be explicitly marked with a Conditional attribute. 6

• The conditional method shall not be an implementation of an interface method. Otherwise, a compile-7 time error occurs. 8

In addition, a compile-time error occurs if a conditional method is used in a delegate-creation-expression. 9

[Example: The example 10

#define DEBUG 11

using System; 12 using System.Diagnostics; 13

class Class1 14 { 15 [Conditional("DEBUG")] 16 public static void M() { 17 Console.WriteLine("Executed Class1.M"); 18 } 19 } 20

class Class2 21 { 22 public static void Test() { 23 Class1.M(); 24 } 25 } 26

declares Class1.M as a conditional method. Class2's Test method calls this method. Since the 27 conditional compilation symbol DEBUG is defined, if Class2.Test is called, it will call M. If the symbol 28 DEBUG had not been defined, then Class2.Test would not call Class1.M. end example] 29

It is important to understand that the inclusion or exclusion of a call to a conditional method is controlled by 30 the conditional compilation symbols at the point of the call. [Example: In the following code 31

// Begin class1.cs 32

using System; 33 using System.Diagnostics; 34

class Class1 35 { 36 [Conditional("DEBUG")] 37 public static void F() { 38 Console.WriteLine("Executed Class1.F"); 39 } 40 } 41

// End class1.cs 42

43

// Begin class2.cs 44

#define DEBUG 45

class Class2 46 { 47 public static void G() { 48 Class1.F(); // F is called 49 } 50 } 51

// End class2.cs 52

Page 395: C# Language Specification

Chapter 24 Attributes

375

1

// Begin class3.cs 2

#undef DEBUG 3

class Class3 4 { 5 public static void H() { 6 Class1.F(); // F is not called 7 } 8 } 9

// End class3.cs 10

the classes Class2 and Class3 each contain calls to the conditional method Class1.F, which is 11 conditional based on whether or not DEBUG is defined. Since this symbol is defined in the context of Class2 12 but not Class3, the call to F in Class2 is included, while the call to F in Class3 is omitted. end example] 13

The use of conditional methods in an inheritance chain can be confusing. Calls made to a conditional 14 method through base, of the form base.M, are subject to the normal conditional method call rules. 15 [Example: In the following code 16

// Begin class1.cs 17

using System; 18 using System.Diagnostics; 19

class Class1 20 { 21 [Conditional("DEBUG")] 22 public virtual void M() { 23 Console.WriteLine("Class1.M executed"); 24 } 25 } 26

// End class1.cs 27

28

// Begin class2.cs 29

using System; 30

class Class2: Class1 31 { 32 public override void M() { 33 Console.WriteLine("Class2.M executed"); 34 base.M(); // base.M is not called! 35 } 36 } 37

// End class2.cs 38

39

// Begin class3.cs 40

#define DEBUG 41

using System; 42

class Class3 43 { 44 public static void Test() { 45 Class2 c = new Class2(); 46 c.M(); // M is called 47 } 48 } 49

// End class3.cs 50

Class2 includes a call to the M defined in its base class. This call is omitted because the base method is 51 conditional based on the presence of the symbol DEBUG, which is undefined. Thus, the method writes to the 52

Page 396: C# Language Specification

C# LANGUAGE SPECIFICATION

376

console “Class2.M executed” only. Judicious use of pp-declarations can eliminate such problems. end 1 example] 2

24.4.2.2 Conditional Attribute Classes 3

An attribute class (§24.1) decorated with the Conditional attribute is said to be a conditional attribute 4 class. The Conditional attribute indicates a condition by testing a conditional compilation symbol. 5 Attribute specifications (§24.2) of a conditional attribute class are either included or omitted depending on 6 whether this symbol is defined at the point of specification. If the symbol is defined, the attribute 7 specification is included; otherwise the attribute specification is omitted. 8

[Example: The example 9

#define DEBUG 10

using System; 11 using System.Diagnostics; 12

[Conditional(“DEBUG”)] 13

public class TestAttribute : Attribute {} 14

[Test] 15

class Class1 {} 16

declares attribute class TestAttribute as a conditional attribute class. The attribute section applies the 17 Test attribute to Class1. Since the conditional compilation symbol DEBUG is defined, retrieving the 18 attributes applied to Class1 at runtime will result in an instance of the TestAttribute class. If the 19 symbol DEBUG had not been defined, then retrieving the attributes applied to Class1 at runtime would not 20 result in an instance of the TestAttribute class. end example] 21

It is important to note that the inclusion or exclusion of an attribute specification of a conditional attribute 22 class is controlled by the conditional compilation symbols at the point of the specification. [Example: In the 23 example 24

File test.cs: 25

using System; 26 using System.Diagnostics; 27

[Conditional(“DEBUG”)] 28

public class TestAttribute : Attribute {} 29

File class1.cs: 30

#define DEBUG 31

[Test] // TestAttribute is specified 32

class Class1 {} 33

File class2.cs: 34

#undef DEBUG 35

[Test] // TestAttribute is not specified 36

class Class2 {} 37

the classes Class1 and Class2 are each decorated with attribute Test, which is conditional based on 38 whether or not DEBUG is defined. Since this symbol is defined in the context of Class1 but not Class2, the 39 specification of the Test attribute on Class1 is included, while the specification of the Test attribute on 40 Class2 is omitted. end example] 41

24.4.3 The Obsolete attribute 42

The attribute Obsolete is used to mark types and members of types that should no longer be used. 43

Page 397: C# Language Specification

Chapter 24 Attributes

377

If a program uses a type or member that is decorated with the Obsolete attribute, then the compiler shall 1 issue a warning or error in order to alert the developer, so the offending code can be fixed. Specifically, the 2 compiler shall issue a warning if no error parameter is provided, or if the error parameter is provided and has 3 the value false. The compiler shall issue a compile-time error if the error parameter is specified and has the 4 value true. 5

[Example: In the following code 6

[Obsolete("This class is obsolete; use class B instead")] 7 class A 8 { 9 public void F() {} 10 } 11

class B 12 { 13 public void F() {} 14 } 15

class Test 16 { 17 static void Main() { 18 A a = new A(); // warning 19 a.F(); 20 } 21 } 22

the class A is decorated with the Obsolete attribute. Each use of A in Main results in a warning that 23 includes the specified message, “This class is obsolete; use class B instead.” 24

end example] 25

Page 398: C# Language Specification
Page 399: C# Language Specification

Chapter 25 Unsafe code

379

25. Unsafe code 1

An implementation that does not support unsafe code is required to diagnose any usage of the keyword 2 unsafe. 3

The remainder of this clause, including all of its subclauses, is conditionally normative. 4

[Note: The core C# language, as defined in the preceding clauses, differs notably from C and C++ in its 5 omission of pointers as a data type. Instead, C# provides references and the ability to create objects that are 6 managed by a garbage collector. This design, coupled with other features, makes C# a much safer language 7 than C or C++. In the core C# language it is simply not possible to have an uninitialized variable, a 8 “dangling” pointer, or an expression that indexes an array beyond its bounds. Whole categories of bugs that 9 routinely plague C and C++ programs are thus eliminated. 10

While practically every pointer type construct in C or C++ has a reference type counterpart in C#, 11 nonetheless, there are situations where access to pointer types becomes a necessity. For example, interfacing 12 with the underlying operating system, accessing a memory-mapped device, or implementing a time-critical 13 algorithm might not be possible or practical without access to pointers. To address this need, C# provides the 14 ability to write unsafe code. 15

In unsafe code it is possible to declare and operate on pointers, to perform conversions between pointers and 16 integral types, to take the address of variables, and so forth. In a sense, writing unsafe code is much like 17 writing C code within a C# program. 18

Unsafe code is in fact a “safe” feature from the perspective of both developers and users. Unsafe code shall 19 be clearly marked with the modifier unsafe, so developers can’t possibly use unsafe features accidentally, 20 and the execution engine works to ensure that unsafe code cannot be executed in an untrusted environment. 21 end note] 22

25.1 Unsafe contexts 23

The unsafe features of C# are available only in unsafe contexts. An unsafe context is introduced by including 24 an unsafe modifier in the declaration of a type or member, or by employing an unsafe-statement: 25

• A declaration of a class, struct, interface, or delegate can include an unsafe modifier, in which case, the 26 extent of that type-declaration is considered an unsafe context. [Note: If the type-declaration is partial, 27 only that part is an unsafe context. end note] 28

• A declaration of a field, method, property, event, indexer, operator, instance constructor, destructor, or 29 static constructor can include an unsafe modifier, in which case, the entire textual extent of that 30 member declaration is considered an unsafe context. 31

• An unsafe-statement enables the use of an unsafe context within a block. The entire textual extent of the 32 associated block is considered an unsafe context. 33

The associated grammar extensions are shown below. For brevity, ellipses (...) are used to represent 34 productions that appear in preceding clauses. 35

class-modifier: 36 ... 37 unsafe 38

struct-modifier: 39 ... 40 unsafe 41

Page 400: C# Language Specification

C# LANGUAGE SPECIFICATION

380

interface-modifier: 1 ... 2 unsafe 3

delegate-modifier: 4 ... 5 unsafe 6

field-modifier: 7 ... 8 unsafe 9

method-modifier: 10 ... 11 unsafe 12

property-modifier: 13 ... 14 unsafe 15

event-modifier: 16 ... 17 unsafe 18

indexer-modifier: 19 ... 20 unsafe 21

operator-modifier: 22 ... 23 unsafe 24

constructor-modifier: 25 ... 26 unsafe 27

destructor-declaration: 28 attributesopt externopt unsafeopt ~ identifier ( ) destructor-body 29 attributesopt unsafeopt externopt ~ identifier ( ) destructor-body 30

static-constructor-modifiers: 31 externopt unsafeopt static 32 unsafeopt externopt static 33 externopt static unsafeopt 34 unsafeopt static externopt 35 static externopt unsafeopt 36 static unsafeopt externopt 37

embedded-statement: 38 ... 39 unsafe-statement 40

unsafe-statement: 41 unsafe block 42

[Example: In the following code 43

public unsafe struct Node 44 { 45 public int Value; 46 public Node* Left; 47 public Node* Right; 48 } 49

Page 401: C# Language Specification

Chapter 25 Unsafe code

381

the unsafe modifier specified in the struct declaration causes the entire textual extent of the struct 1 declaration to become an unsafe context. Thus, it is possible to declare the Left and Right fields to be of a 2 pointer type. The example above could also be written 3

public struct Node 4 { 5 public int Value; 6 public unsafe Node* Left; 7 public unsafe Node* Right; 8 } 9

Here, the unsafe modifiers in the field declarations cause those declarations to be considered unsafe 10 contexts. end example] 11

Other than establishing an unsafe context, thus permitting the use of pointer types, the unsafe modifier has 12 no effect on a type or a member. [Example: In the following code 13

public class A 14 { 15 public unsafe virtual void F() { 16 char* p; 17 … 18 } 19 } 20

public class B: A 21 { 22 public override void F() { 23 base.F(); 24 … 25 } 26 } 27

the unsafe modifier on the F method in A simply causes the textual extent of F to become an unsafe 28 context in which the unsafe features of the language can be used. In the override of F in B, there is no need 29 to re-specify the unsafe modifier—unless, of course, the F method in B itself needs access to unsafe 30 features. 31

The situation is slightly different when a pointer type is part of the method’s signature 32

public unsafe class A 33 { 34 public virtual void F(char* p) {…} 35 } 36

public class B: A 37 { 38 public unsafe override void F(char* p) {…} 39 } 40

Here, because F’s signature includes a pointer type, it can only be written in an unsafe context. However, the 41 unsafe context can be introduced by either making the entire class unsafe, as is the case in A, or by including 42 an unsafe modifier in the method declaration, as is the case in B. end example] 43

When the unsafe modifier is used on a partial type declaration (§17.1.4), only that particular part is 44 considered an unsafe context. 45

25.2 Pointer types 46

In an unsafe context, a type (§11) can be a pointer-type as well as a value-type, a reference-type, or a type-47 parameter. 48

type: 49 value-type 50 reference-type 51 type-parameter 52 pointer-type 53

Page 402: C# Language Specification

C# LANGUAGE SPECIFICATION

382

A pointer-type is written as an unmanaged-type or the keyword void, followed by a * token: 1

pointer-type: 2 unmanaged-type * 3 void * 4

unmanaged-type: 5 type 6

The type specified before the * in a pointer type is called the referent type of the pointer type. It represents 7 the type of the variable to which a value of the pointer type points. 8

Unlike references (values of reference types), pointers are not tracked by the garbage collector—the garbage 9 collector has no knowledge of pointers and the data to which they point. For this reason a pointer is not 10 permitted to point to a reference or to a struct that contains references, and the referent type of a pointer shall 11 be an unmanaged-type. 12

An unmanaged-type is any type that isn’t a reference-type, a type-parameter, or a generic struct-type and 13 contains no fields whose type is not an unmanaged-type. In other words, an unmanaged-type is one of the 14 following: 15

• sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool. 16

• Any enum-type. 17

• Any pointer-type. 18

• Any non-generic user-defined struct-type that contains fields of unmanaged-types only. 19

[Note: Constructed types and type-parameters are never unmanaged-types. end note] 20

The intuitive rule for mixing of pointers and references is that referents of references (objects) are permitted 21 to contain pointers, but referents of pointers are not permitted to contain references. 22

[Example: Some examples of pointer types are given in the table below: 23

24

Example Description byte* Pointer to byte char* Pointer to char int** Pointer to pointer to int int*[] Single-dimensional array of pointers to int void* Pointer to unknown type

25 end example] 26

For a given implementation, all pointer types shall have the same size and representation. 27

[Note: Unlike C and C++, when multiple pointers are declared in the same declaration, in C# the * is written 28 along with the underlying type only, not as a prefix punctuator on each pointer name. For example: 29

int* pi, pj; // NOT as int *pi, *pj; 30

end note] 31

The value of a pointer having type T* represents the address of a variable of type T. The pointer indirection 32 operator * (§25.5.1) can be used to access this variable. [Example: Given a variable P of type int*, the 33 expression *P denotes the int variable found at the address contained in P. end example] 34

Like an object reference, a pointer can be null. Applying the indirection operator to a null pointer results 35 in implementation-defined behavior. A pointer with value null is represented by all-bits-zero. 36

Page 403: C# Language Specification

Chapter 25 Unsafe code

383

The void* type represents a pointer to an unknown type. Because the referent type is unknown, the 1 indirection operator cannot be applied to a pointer of type void*, nor can any arithmetic be performed on 2 such a pointer. However, a pointer of type void* can be cast to any other pointer type (and vice versa) and 3 compared to values of other pointer types. 4

Pointer types are a separate category of types. Unlike reference types and value types, pointer types do not 5 inherit from object and no conversions exist between pointer types and object. In particular, boxing and 6 unboxing (§11.3) are not supported for pointers. However, conversions are permitted between different 7 pointer types and between pointer types and the integral types. This is described in §25.4. 8

A pointer-type can be used as the type of a volatile field (§17.4.3). 9

[Note: Although pointers can be passed as ref or out parameters, doing so can cause undefined behavior, 10 since the pointer might well be set to point to a local variable which no longer exists when the called method 11 returns, or the fixed object to which it used to point, is no longer fixed. For example: 12

using System; 13

class Test 14 { 15 static int value = 20; 16

unsafe static void F(out int* pi1, ref int* pi2) { 17 int i = 10; 18 pi1 = &i; 19 fixed (int* pj = &value) { 20 // ... 21 pi2 = pj; 22 } 23 } 24

static void Main() { 25 int i = 10; 26 unsafe { 27 int* px1; 28 int* px2 = &i; 29 F(out px1, ref px2); 30 Console.WriteLine("*px1 = {0}, *px2 = {1}", 31 *px1, *px2); // undefined behavior 32 } 33 } 34 } 35

end note] 36

A method can return a value of some type, and that type can be a pointer. [Example: When given a pointer to 37 a contiguous sequence of ints, that sequence's element count, and some other int value, the following 38 method returns the address of that value in that sequence, if a match occurs; otherwise it returns null: 39

unsafe static int* Find(int* pi, int size, int value) { 40 for (int i = 0; i < size; ++i) { 41 if (*pi == value) { 42 return pi; 43 } 44 ++pi; 45 } 46 return null; 47 } 48

end example] 49

In an unsafe context, several constructs are available for operating on pointers: 50

• The unary * operator can be used to perform pointer indirection (§25.5.1). 51

• The -> operator can be used to access a member of a struct through a pointer (§25.5.2). 52

• The [] operator can be used to index a pointer (§25.5.3). 53

• The unary & operator can be used to obtain the address of a variable (§25.5.4). 54

Page 404: C# Language Specification

C# LANGUAGE SPECIFICATION

384

• The ++ and -- operators can be used to increment and decrement pointers (§25.5.5). 1

• The binary + and - operators can be used to perform pointer arithmetic (§25.5.6). 2

• The ==, !=, <, >, <=, and => operators can be used to compare pointers (§25.5.7). 3

• The stackalloc operator can be used to allocate memory from the call stack (§25.7). 4

• The fixed statement can be used to temporarily fix a variable so its address can be obtained (§25.6). 5

25.3 Fixed and moveable variables 6

The address-of operator (§25.5.4) and the fixed statement (§25.6) divide variables into two categories: 7 Fixed variables and moveable variables. 8

Fixed variables reside in storage locations that are unaffected by operation of the garbage collector. 9 (Examples of fixed variables include local variables, value parameters, and variables created by 10 dereferencing pointers.) On the other hand, moveable variables reside in storage locations that are subject to 11 relocation or disposal by the garbage collector. (Examples of moveable variables include fields in objects 12 and elements of arrays.) 13

The & operator (§25.5.4) permits the address of a fixed variable to be obtained without restrictions. 14 However, because a moveable variable is subject to relocation or disposal by the garbage collector, the 15 address of a moveable variable can only be obtained using a fixed statement (§25.6), and that address 16 remains valid only for the duration of that fixed statement. 17

In precise terms, a fixed variable is one of the following: 18

• A variable resulting from a simple-name (§14.5.2) that refers to a local variable or a value parameter, as 19 long as the variable is not captured by an anonymous method (§14.5.14.3.1). 20

• A variable resulting from a member-access (§14.5.4) of the form V.I, where V is a fixed variable of a 21 struct-type. 22

• A variable resulting from a pointer-indirection-expression (§25.5.1) of the form *P, a pointer-member-23 access (§25.5.2) of the form P->I, or a pointer-element-access (§25.5.3) of the form P[E]. 24

All other variables are classified as moveable variables. 25

A static field is classified as a moveable variable. Also, a ref or out parameter is classified as a moveable 26 variable, even if the argument given for the parameter is a fixed variable. Finally, a variable produced by 27 dereferencing a pointer is always classified as a fixed variable. 28

25.4 Pointer conversions 29

In an unsafe context, the set of available implicit conversions (§13.1) is extended to include the following 30 implicit pointer conversions: 31

• From any pointer-type to the type void*. 32

• From the null type to any pointer-type. 33

Additionally, in an unsafe context, the set of available explicit conversions (§13.2) is extended to include the 34 following explicit pointer conversions: 35

• From any pointer-type to any other pointer-type. 36

• From sbyte, byte, short, ushort, int, uint, long, or ulong to any pointer-type. 37

• From any pointer-type to sbyte, byte, short, ushort, int, uint, long, or ulong. 38

Finally, in an unsafe context, the set of standard implicit conversions (§13.3.1) includes the following 39 pointer conversion: 40

• From any pointer-type to the type void*. 41

Page 405: C# Language Specification

Chapter 25 Unsafe code

385

Conversions between two pointer types never change the actual pointer value. In other words, a conversion 1 from one pointer type to another has no effect on the underlying address given by the pointer. 2

When one pointer type is converted to another, if the resulting pointer is not correctly aligned for the 3 pointed-to type, the behavior is undefined if the result is dereferenced. In general, the concept “correctly 4 aligned” is transitive: if a pointer to type A is correctly aligned for a pointer to type B, which, in turn, is 5 correctly aligned for a pointer to type C, then a pointer to type A is correctly aligned for a pointer to type C. 6 [Example: Consider the following case in which a variable having one type is accessed via a pointer to a 7 different type: 8

char c = 'A'; 9 char* pc = &c; 10 void* pv = pc; 11 int* pi = (int*)pv; 12 int i = *pi; // undefined 13 *pi = 123456; // undefined 14

end example] 15

When a pointer type is converted to a pointer to byte, the result points to the lowest addressed byte of the 16 variable. Successive increments of the result, up to the size of the variable, yield pointers to the remaining 17 bytes of that variable. [Example: The following method displays each of the eight bytes in a double as a 18 hexadecimal value: 19

using System; 20

class Test 21 { 22 static void Main() { 23 double d = 123.456e23; 24 unsafe { 25 byte* pb = (byte*)&d; 26 for (int i = 0; i < sizeof(double); ++i) 27 Console.Write(" {0,2:X}", (uint)(*pb++)); 28 Console.WriteLine(); 29 } 30 } 31 } 32

Of course, the output produced depends on byte ordering. end example] 33

Mappings between pointers and integers are implementation-defined. [Note: However, on 32- and 64-bit 34 CPU architectures with a linear address space, conversions of pointers to or from integral types typically 35 behave exactly like conversions of uint or ulong values, respectively, to or from those integral types. end 36 note] 37

25.5 Pointers in expressions 38

In an unsafe context, an expression can yield a result of a pointer type, but outside an unsafe context it is a 39 compile-time error for an expression to be of a pointer type. In precise terms, outside an unsafe context a 40 compile-time error occurs if any simple-name (§14.5.2), member-access (§14.5.4), invocation-expression 41 (§14.5.5), or element-access (§14.5.6) is of a pointer type. 42

The primary-no-array-creation-expression (§14.5) productions permit the following additional construct: 43

primary-no-array-creation-expression: 44 … 45 sizeof-expression 46

In an unsafe context, the primary-no-array-creation-expression (§14.5) and unary-expression (§14.5.13) 47 productions permit the following additional constructs: 48

primary-no-array-creation-expression: 49 … 50 pointer-member-access 51 pointer-element-access 52

Page 406: C# Language Specification

C# LANGUAGE SPECIFICATION

386

unary-expression: 1 … 2 pointer-indirection-expression 3 addressof-expression 4

These constructs are described in the following subclauses. 5

There are also several predefined unary and binary operators for pointers. When applying unary or binary 6 operator overload resolution, if none of the actual operands is a pointer type, then any predefined unary or 7 binary operator with a pointer parameter type is removed from consideration. 8

[Note: The precedence and associativity of the unsafe operators is implied by the grammar. end note] 9

25.5.1 Pointer indirection 10

A pointer-indirection-expression consists of an asterisk (*) followed by a unary-expression. 11

pointer-indirection-expression: 12 * unary-expression 13

The unary * operator denotes pointer indirection and is used to obtain the variable to which a pointer points. 14 The result of evaluating *P, where P is an expression of a pointer type T*, is a variable of type T. It is a 15 compile-time error to apply the unary * operator to an expression of type void* or to an expression that 16 isn’t of a pointer type. 17

The effect of applying the unary * operator to a null pointer is implementation-defined. In particular, there 18 is no guarantee that this operation throws a System.NullReferenceException. 19

If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined. [Note: 20 Among the invalid values for dereferencing a pointer by the unary * operator are an address inappropriately 21 aligned for the type pointed to (see example in §25.4), and the address of a variable after the end of its 22 lifetime. end note] 23

For purposes of definite assignment analysis, a variable produced by evaluating an expression of the form 24 *P is considered initially assigned (§12.3.1). 25

25.5.2 Pointer member access 26

A pointer-member-access consists of a primary-expression, followed by a “->” token, followed by an 27 identifier. 28

pointer-member-access: 29 primary-expression -> identifier type-argument-listopt 30

In a pointer member access of the form P->I, P shall be an expression of a pointer type other than void*, 31 and I shall denote an accessible member of the type to which P points. 32

A pointer member access of the form P->I is evaluated exactly as (*P).I. For a description of the pointer 33 indirection operator (*), see §25.5.1. For a description of the member access operator (.), see §14.5.4. 34

[Example: In the following code 35

struct Point 36 { 37 public int x; 38 public int y; 39

public override string ToString() { 40 return "(" + x + "," + y + ")"; 41 } 42 } 43

using System; 44

Page 407: C# Language Specification

Chapter 25 Unsafe code

387

class Test 1 { 2 static void Main() { 3 Point point; 4 unsafe { 5 Point* p = &point; 6 p->x = 10; 7 p->y = 20; 8 Console.WriteLine(p->ToString()); 9 } 10 } 11 } 12

the -> operator is used to access fields and invoke a method of a struct through a pointer. Because the 13 operation P->I is precisely equivalent to (*P).I, the Main method could equally well have been written: 14

using System; 15

class Test 16 { 17 static void Main() { 18 Point point; 19 unsafe { 20 Point* p = &point; 21 (*p).x = 10; 22 (*p).y = 20; 23 Console.WriteLine((*p).ToString()); 24 } 25 } 26 } 27

end example] 28

25.5.3 Pointer element access 29

A pointer-element-access consists of a primary-no-array-creation-expression followed by an expression 30 enclosed in “[” and “]”. 31

pointer-element-access: 32 primary-no-array-creation-expression [ expression ] 33

In a pointer element access of the form P[E], P shall be an expression of a pointer type other than void*, 34 and E shall be an expression of a type that can be implicitly converted to int, uint, long, or ulong. 35

A pointer element access of the form P[E] is evaluated exactly as *(P + E). For a description of the pointer 36 indirection operator (*), see §25.5.1. For a description of the pointer addition operator (+), see §25.5.6. 37

[Example: In the following code 38

class Test 39 { 40 static void Main() { 41 unsafe { 42 char* p = stackalloc char[256]; 43 for (int i = 0; i < 256; i++) p[i] = (char)i; 44 } 45 } 46 } 47

a pointer element access is used to initialize the character buffer in a for loop. Because the operation P[E] 48 is precisely equivalent to *(P + E), the example could equally well have been written: 49

Page 408: C# Language Specification

C# LANGUAGE SPECIFICATION

388

class Test 1 { 2 static void Main() { 3 unsafe { 4 char* p = stackalloc char[256]; 5 for (int i = 0; i < 256; i++) *(p + i) = (char)i; 6 } 7 } 8 } 9

end example] 10

The pointer element access operator does not check for out-of-bounds errors and the behavior when 11 accessing an out-of-bounds element is undefined. [Note: This is the same as C and C++. end note] 12

25.5.4 The address-of operator 13

An addressof-expression consists of an ampersand (&) followed by a unary-expression. 14

addressof-expression: 15 & unary-expression 16

Given an expression E which is of a type T and is classified as a fixed variable (§25.3), the construct &E 17 computes the address of the variable given by E. The type of the result is T* and is classified as a value. A 18 compile-time error occurs if E is not classified as a variable, if E is classified as a volatile field (§17.4.3), or 19 if E denotes a moveable variable. In the last case, a fixed statement (§25.6) can be used to temporarily “fix” 20 the variable before obtaining its address. 21

The & operator does not require its argument to be definitely assigned, but following an & operation, the 22 variable to which the operator is applied is considered definitely assigned in the execution path in which the 23 operation occurs. It is the responsibility of the programmer to ensure that correct initialization of the variable 24 actually does take place in this situation. 25

[Example: In the following code 26

using System; 27

class Test 28 { 29 static void Main() { 30 int i; 31 unsafe { 32 int* p = &i; 33 *p = 123; 34 } 35 Console.WriteLine(i); 36 } 37 } 38

i is considered definitely assigned following the &i operation used to initialize p. The assignment to *p in 39 effect initializes i, but the inclusion of this initialization is the responsibility of the programmer, and no 40 compile-time error would occur if the assignment were removed. end example] 41

[Note: The rules of definite assignment for the & operator exist such that redundant initialization of local 42 variables can be avoided. For example, many external APIs take a pointer to a structure which is filled in by 43 the API. Calls to such APIs typically pass the address of a local struct variable, and without the rule, 44 redundant initialization of the struct variable would be required. end note] 45

[Note: As stated in §14.5.4, outside an instance constructor or static constructor for a struct or class that 46 defines a readonly field, that field is considered a value, not a variable. As such, its address cannot be taken. 47 Similarly, the address of a constant cannot be taken. end note] 48

When a local variable, value parameter, or parameter array is captured by an anonymous method 49 (§14.5.14.3.1), that local variable, parameter, or parameter array is no longer considered to be a fixed 50 variable (§25.3), but is instead considered to be a moveable variable. Thus it is an error for any unsafe 51

Page 409: C# Language Specification

Chapter 25 Unsafe code

389

code to take the address of a local variable, value parameter, or parameter array which has been captured by 1 an anonymous method. 2

25.5.5 Pointer increment and decrement 3

In an unsafe context, the ++ and -- operators (§14.5.9 and §14.6.5) can be applied to pointer variables of all 4 types except void*. Thus, for every pointer type T*, the following operators are implicitly defined: 5

T* operator ++(T* x); 6

T* operator --(T* x); 7

The operators produce the same results as x+1 and x-1, respectively (§25.5.6). In other words, for a pointer 8 variable of type T*, the ++ operator adds sizeof(T) to the address contained in the variable, and the 9 -- operator subtracts sizeof(T) from the address contained in the variable. 10

If a pointer increment or decrement operation overflows the domain of the pointer type, the result is 11 implementation-defined, but no exceptions are produced. 12

25.5.6 Pointer arithmetic 13

In an unsafe context, the + operator (§14.7.4) and – operator (§14.7.5) can be applied to values of all 14 pointer types except void*. Thus, for every pointer type T*, the following operators are implicitly defined: 15

T* operator +(T* x, int y); 16 T* operator +(T* x, uint y); 17 T* operator +(T* x, long y); 18 T* operator +(T* x, ulong y); 19

T* operator +(int x, T* y); 20 T* operator +(uint x, T* y); 21 T* operator +(long x, T* y); 22 T* operator +(ulong x, T* y); 23

T* operator –(T* x, int y); 24 T* operator –(T* x, uint y); 25 T* operator –(T* x, long y); 26 T* operator –(T* x, ulong y); 27

long operator –(T* x, T* y); 28

Given an expression P of a pointer type T* and an expression N of type int, uint, long, or ulong, the 29 expressions P + N and N + P compute the pointer value of type T* that results from adding 30 N * sizeof(T) to the address given by P. Likewise, the expression P – N computes the pointer value of 31 type T* that results from subtracting N * sizeof(T) from the address given by P. 32

Given two expressions, P and Q, of a pointer type T*, the expression P – Q computes the difference 33 between the addresses given by P and Q and then divides that difference by sizeof(T). The type of the 34 result is always long. In effect, P - Q is computed as ((long)(P) - (long)(Q)) / sizeof(T). 35 [Example: 36

using System; 37

class Test 38 { 39 static void Main() { 40 unsafe { 41 int* values = stackalloc int[20]; 42

int* p = &values[1]; 43 int* q = &values[15]; 44

Console.WriteLine("p - q = {0}", p - q); 45 Console.WriteLine("q - p = {0}", q - p); 46 } 47 } 48 } 49

which produces the output: 50

Page 410: C# Language Specification

C# LANGUAGE SPECIFICATION

390

p - q = -14 1 q - p = 14 2

end example] 3

If a pointer arithmetic operation overflows the domain of the pointer type, the result is truncated in an 4 implementation-defined fashion, but no exceptions are produced. 5

25.5.7 Pointer comparison 6

In an unsafe context, the ==, !=, <, >, <=, and => operators (§14.9) can be applied to values of all pointer 7 types. The pointer comparison operators are: 8

bool operator ==(void* x, void* y); 9 bool operator !=(void* x, void* y); 10

bool operator <(void* x, void* y); 11 bool operator >(void* x, void* y); 12 bool operator <=(void* x, void* y); 13 bool operator >=(void* x, void* y); 14

Because an implicit conversion exists from any pointer type to the void* type, operands of any pointer type 15 can be compared using these operators. The comparison operators compare the addresses given by the two 16 operands as if they were unsigned integers. 17

25.5.8 The sizeof operator 18

The sizeof operator returns the number of 8-bit bytes occupied by a variable of a given type. The type 19 specified as an operand to sizeof shall be an unmanaged-type (§25.2). 20

sizeof-expression: 21 sizeof ( unmanaged-type ) 22

The result of the sizeof operator is a value of type int. For certain predefined types, the sizeof operator 23 yields a constant value as shown in the table below. 24 25

Expression Result sizeof(sbyte) 1

sizeof(byte) 1

sizeof(short) 2

sizeof(ushort) 2

sizeof(int) 4

sizeof(uint) 4

sizeof(long) 8

sizeof(ulong) 8

sizeof(char) 2

sizeof(float) 4

sizeof(double) 8

sizeof(bool) 1

sizeof(decimal) 16

26 For all other types, the result of the sizeof operator is implementation-defined and is classified as a value, 27 not a constant. 28

When the sizeof-expression is not a constant value, the sizeof-expression shall be inside an unsafe context. 29 When the sizeof-expression is a constant value, the sizeof-expression is allowed both inside and outside an 30 unsafe context. 31

Page 411: C# Language Specification

Chapter 25 Unsafe code

391

The order in which members are packed into a struct is unspecified. 1

For alignment purposes, there can be unnamed padding at the beginning of a struct, within a struct, and at 2 the end of the struct. The contents of the bits used as padding are indeterminate. 3

When applied to an operand that has struct type, the result is the total number of bytes in a variable of that 4 type, including any padding. 5

25.6 The fixed statement 6

In an unsafe context, the embedded-statement (§15) production permits an additional construct, the fixed 7 statement, which is used to “fix” a moveable variable such that its address remains constant for the duration 8 of the statement. 9

embedded-statement: 10 ... 11 fixed-statement 12

fixed-statement: 13 fixed ( pointer-type fixed-pointer-declarators ) embedded-statement 14

fixed-pointer-declarators: 15 fixed-pointer-declarator 16 fixed-pointer-declarators , fixed-pointer-declarator 17

fixed-pointer-declarator: 18 identifier = fixed-pointer-initializer 19 20

fixed-pointer-initializer: 21 & variable-reference 22 expression 23

Each fixed-pointer-declarator declares a local variable of the given pointer-type and initializes that local 24 variable with the address computed by the corresponding fixed-pointer-initializer. A local variable declared 25 in a fixed statement is accessible in any fixed-pointer-initializers occurring to the right of that variable’s 26 declaration, and in the embedded-statement of the fixed statement. A local variable declared by a fixed 27 statement is considered read-only. A compile-time error occurs if the embedded statement attempts to 28 modify this local variable (via assignment or the ++ and -- operators) or pass it as a ref or out parameter. 29

It is an error to use a captured local variable (§14.5.14.3.1), value parameter, or parameter array in a fixed 30 statement. 31

A fixed-pointer-initializer can be one of the following: 32

• The token “&” followed by a variable-reference (§12.3.3.27) to a moveable variable (§25.3) of an 33 unmanaged type T, provided the type T* is implicitly convertible to the pointer type given in the fixed 34 statement. In this case, the initializer computes the address of the given variable, and the variable is 35 guaranteed to remain at a fixed address for the duration of the fixed statement. 36

• An expression of an array-type with elements of an unmanaged type T, provided the type T* is 37 implicitly convertible to the pointer type given in the fixed statement. In this case, the initializer 38 computes the address of the first element in the array, and the entire array is guaranteed to remain at a 39 fixed address for the duration of the fixed statement. The behavior of the fixed statement is 40 implementation-defined if the array expression is null or if the array has zero elements. 41

• An expression of type string, provided the type char* is implicitly convertible to the pointer type 42 given in the fixed statement. In this case, the initializer computes the address of the first character in 43 the string, and the entire string is guaranteed to remain at a fixed address for the duration of the fixed 44 statement. The behavior of the fixed statement is implementation-defined if the string expression is 45 null. 46

Page 412: C# Language Specification

C# LANGUAGE SPECIFICATION

392

For each address computed by a fixed-pointer-initializer the fixed statement ensures that the variable 1 referenced by the address is not subject to relocation or disposal by the garbage collector for the duration of 2 the fixed statement. [Example: If the address computed by a fixed-pointer-initializer references a field of 3 an object or an element of an array instance, the fixed statement guarantees that the containing object 4 instance is not relocated or disposed of during the lifetime of the statement. end example] 5

It is the programmer's responsibility to ensure that pointers created by fixed statements do not survive 6 beyond execution of those statements. [Example: When pointers created by fixed statements are passed to 7 external APIs, it is the programmer’s responsibility to ensure that the APIs retain no memory of these 8 pointers. end example] 9

Fixed objects can cause fragmentation of the heap (because they can’t be moved). For that reason, objects 10 should be fixed only when absolutely necessary and then only for the shortest amount of time possible. 11 [Example: The example 12

class Test 13 { 14 static int x; 15 int y; 16

unsafe static void F(int* p) { 17 *p = 1; 18 } 19

static void Main() { 20 Test t = new Test(); 21 int[] a = new int[10]; 22 unsafe { 23 fixed (int* p = &x) F(p); 24 fixed (int* p = &t.y) F(p); 25 fixed (int* p = &a[0]) F(p); 26 fixed (int* p = a) F(p); 27 } 28 } 29 } 30

demonstrates several uses of the fixed statement. The first statement fixes and obtains the address of a 31 static field, the second statement fixes and obtains the address of an instance field, and the third statement 32 fixes and obtains the address of an array element. In each case it would have been an error to use the regular 33 & operator since the variables are all classified as moveable variables. 34

The third and fourth fixed statements in the example above produce identical results. In general, for an 35 array instance a, specifying &a[0] in a fixed statement is the same as simply specifying a. 36

Here’s another example of the fixed statement, this time using string: 37

class Test 38 { 39 static string name = "xx"; 40

unsafe static void F(char* p) { 41 for (int i = 0; p[i] != '\0'; ++i) 42 Console.WriteLine(p[i]); 43 } 44

static void Main() { 45 unsafe { 46 fixed (char* p = name) F(p); 47 fixed (char* p = "xx") F(p); 48 } 49 } 50 } 51

end example] 52

In an unsafe context array elements of single-dimensional arrays are stored in increasing index order, 53 starting with index 0 and ending with index Length – 1. For multi-dimensional arrays, array elements are 54

Page 413: C# Language Specification

Chapter 25 Unsafe code

393

stored such that the indices of the rightmost dimension are increased first, then the next left dimension, and 1 so on to the left. 2

Within a fixed statement that obtains a pointer p to an array instance a, the pointer values ranging from p 3 to p + a.Length - 1 represent addresses of the elements in the array. Likewise, the variables ranging from 4 p[0] to p[a.Length - 1] represent the actual array elements. Given the way in which arrays are stored , 5 we can treat an array of any dimension as though it were linear. [Example: 6

using System; 7

class Test 8 { 9 static void Main() { 10 int[,,] a = new int[2,3,4]; 11

unsafe { 12 fixed (int* p = a) { 13 for (int i = 0; i < a.Length; ++i) // treat as linear 14 p[i] = i; 15 } 16 } 17

for (int i = 0; i < 2; ++i) 18 for (int j = 0; j < 3; ++j) { 19 for (int k = 0; k < 4; ++k) 20 Console.Write("[{0},{1},{2}] = {3,2} ", i, j, k, 21 a[i,j,k]); 22 Console.WriteLine(); 23 } 24 } 25 } 26

which produces the output: 27

[0,0,0] = 0 [0,0,1] = 1 [0,0,2] = 2 [0,0,3] = 3 28 [0,1,0] = 4 [0,1,1] = 5 [0,1,2] = 6 [0,1,3] = 7 29 [0,2,0] = 8 [0,2,1] = 9 [0,2,2] = 10 [0,2,3] = 11 30 [1,0,0] = 12 [1,0,1] = 13 [1,0,2] = 14 [1,0,3] = 15 31 [1,1,0] = 16 [1,1,1] = 17 [1,1,2] = 18 [1,1,3] = 19 32 [1,2,0] = 20 [1,2,1] = 21 [1,2,2] = 22 [1,2,3] = 23 33

end example] 34

[Example: In the following code 35

class Test 36 { 37 unsafe static void Fill(int* p, int count, int value) { 38 for (; count != 0; count--) *p++ = value; 39 } 40

static void Main() { 41 int[] a = new int[100]; 42 unsafe { 43 fixed (int* p = a) Fill(p, 100, -1); 44 } 45 } 46 } 47

a fixed statement is used to fix an array so its address can be passed to a method that takes a pointer. end 48 example] 49

A char* value produced by fixing a non-null string instance always points to a null-terminated string. 50 Within a fixed statement that obtains a pointer p to a string instance s, the pointer values ranging from p to 51 p + s.Length - 1 represent addresses of the characters in the string, and the pointer value 52 p + s.Length always points to a null character (the character with value '\0'). 53

Modifying objects of managed type through fixed pointers can result in undefined behavior. [Note: For 54 example, because strings are immutable, it is the programmer’s responsibility to ensure that the characters 55 referenced by a pointer to a fixed string are not modified. end note] 56

Page 414: C# Language Specification

C# LANGUAGE SPECIFICATION

394

[Note: The automatic null-termination of strings is particularly convenient when calling external APIs that 1 expect “C-style” strings. Note, however, that a string instance is permitted to contain null characters. If such 2 null characters are present, the string will appear truncated when treated as a null-terminated char*. end 3 note] 4

25.7 Stack allocation 5

In an unsafe context, a local variable declaration (§15.5.1) can include a stack allocation initializer, which 6 allocates memory from the call stack. 7

local-variable-initializer: 8 expression 9 array-initializer 10 stackalloc-initializer 11

stackalloc-initializer: 12 stackalloc unmanaged-type [ expression ] 13

The unmanaged-type indicates the type of the items that will be stored in the newly allocated location, and 14 the expression indicates the number of these items. Taken together, these specify the required allocation 15 size. Since the size of a stack allocation cannot be negative, it is a compile-time error to specify the number 16 of items as a constant-expression that evaluates to a negative value. 17

A stack allocation initializer of the form stackalloc T[E] requires T to be an unmanaged type (§25.2) and 18 E to be an expression convertible to type int. The construct allocates E * sizeof(T) bytes from the call 19 stack and returns a pointer, of type T*, to the newly allocated block. If E is a negative value, then the 20 behavior is undefined. If E is zero, then no allocation is made, and the pointer returned is implementation-21 defined. If there is not enough memory available to allocate a block of the given size, a 22 System.StackOverflowException is thrown. 23

The content of the newly allocated memory is undefined. 24

Stack allocation initializers are not permitted in catch or finally blocks (§15.10). 25

[Note: There is no way to explicitly free memory allocated using stackalloc. end note] All stack-26 allocated memory blocks created during the execution of a function member are automatically discarded 27 when that function member returns. [Note: This corresponds to the alloca function, an extension 28 commonly found in C and C++ implementations. end note] 29

[Example: In the following code 30

using System; 31

class Test 32 { 33 static string IntToString(int value) { 34 int n = value >= 0 ? value : -value; 35 unsafe { 36 char* buffer = stackalloc char[16]; 37 char* p = buffer + 16; 38 do { 39 *--p = (char)(n % 10 + '0'); 40 n /= 10; 41 } while (n != 0); 42 if (value < 0) *--p = '-'; 43 return new string(p, 0, (int)(buffer + 16 - p)); 44 } 45 } 46

static void Main() { 47 Console.WriteLine(IntToString(12345)); 48 Console.WriteLine(IntToString(-999)); 49 } 50 } 51

Page 415: C# Language Specification

Chapter 25 Unsafe code

395

a stackalloc initializer is used in the IntToString method to allocate a buffer of 16 characters on the 1 stack. The buffer is automatically discarded when the method returns. end example] 2

25.8 Dynamic memory allocation 3

Except for the stackalloc operator, C# provides no predefined constructs for managing non-garbage 4 collected memory. Such services are typically provided by supporting class libraries or imported directly 5 from the underlying operating system. [Example: The Memory static class below illustrates how the heap 6 functions of an underlying operating system might be accessed from C#: 7

using System; 8 using System.Runtime.InteropServices; 9

public unsafe static class Memory 10 { 11 // Handle for the process heap. This handle is used in all calls to 12 // the HeapXXX APIs in the methods below. 13

static int ph = GetProcessHeap(); 14

// Allocates a memory block of the given size. The allocated memory is 15 // automatically initialized to zero. 16

public static void* Alloc(int size) { 17 void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size); 18 if (result == null) throw new OutOfMemoryException(); 19 return result; 20 } 21

// Copies count bytes from src to dst. The source and destination 22 // blocks are permitted to overlap. 23

public static void Copy(void* src, void* dst, int count) { 24 byte* ps = (byte*)src; 25 byte* pd = (byte*)dst; 26 if (ps > pd) { 27 for (; count != 0; count--) *pd++ = *ps++; 28 } 29 else if (ps < pd) { 30 for (ps += count, pd += count; count != 0; count--) 31 *--pd = *--ps; 32 } 33 } 34

// Frees a memory block. 35

public static void Free(void* block) { 36 if (!HeapFree(ph, 0, block)) throw new InvalidOperationException(); 37 } 38

// Re-allocates a memory block. If the reallocation request is for a 39 // larger size, the additional region of memory is automatically 40 // initialized to zero. 41

public static void* ReAlloc(void* block, int size) { 42 void* result = HeapReAlloc(ph, HEAP_ZERO_MEMORY, block, size); 43 if (result == null) throw new OutOfMemoryException(); 44 return result; 45 } 46

// Returns the size of a memory block. 47

public static int SizeOf(void* block) { 48 int result = HeapSize(ph, 0, block); 49 if (result == -1) throw new InvalidOperationException(); 50 return result; 51 } 52

// Heap API flags 53

const int HEAP_ZERO_MEMORY = 0x00000008; 54

// Heap API functions 55

Page 416: C# Language Specification

C# LANGUAGE SPECIFICATION

396

[DllImport("kernel32")] 1 static extern int GetProcessHeap(); 2

[DllImport("kernel32")] 3 static extern void* HeapAlloc(int hHeap, int flags, int size); 4

[DllImport("kernel32")] 5 static extern bool HeapFree(int hHeap, int flags, void* block); 6

[DllImport("kernel32")] 7 static extern void* HeapReAlloc(int hHeap, int flags, 8 void* block, int size); 9

[DllImport("kernel32")] 10 static extern int HeapSize(int hHeap, int flags, void* block); 11 } 12

An example that uses the Memory class is given below: 13

class Test 14 { 15 static void Main() { 16 unsafe { 17 byte* buffer = (byte*)Memory.Alloc(256); 18 for (int i = 0; i < 256; i++) buffer[i] = (byte)i; 19 byte[] array = new byte[256]; 20 fixed (byte* p = array) Memory.Copy(buffer, p, 256); 21 Memory.Free(buffer); 22 for (int i = 0; i < 256; i++) Console.WriteLine(array[i]); 23 } 24 } 25 } 26

The example allocates 256 bytes of memory through Memory.Alloc and initializes the memory block with 27 values increasing from 0 to 255. It then allocates a 256-element byte array and uses Memory.Copy to copy 28 the contents of the memory block into the byte array. Finally, the memory block is freed using 29 Memory.Free and the contents of the byte array are output on the console. end example] 30

End of conditionally normative text. 31

Page 417: C# Language Specification

Chapter 26 Generics

397

26. Generics 1

26.1 Generic class declarations 2

A generic class declaration is a declaration of a class that requires type arguments to be supplied in order to 3 form actual types. 4

[Note: A class declaration (§17.1), can optionally define type parameters and their associated constraints: 5

class-declaration: 6 attributesopt class-modifiersopt partialopt class identifier type-parameter-listopt 7 class-baseopt type-parameter-constraints-clausesopt class-body ;opt 8

A class declaration shall not supply type-parameter-constraints-clauses (§26.7) unless it also supplies a 9 type-parameter-list (§26.1.1). 10

A class declaration that supplies a type-parameter-list is a generic class declaration. end note] 11

Any class nested inside a generic class declaration or a generic struct declaration (§26.2) is itself a generic 12 class declaration, since type parameters for the containing type shall be supplied to create a constructed type. 13

Generic class declarations follow the same rules as non-generic class declarations except where noted. 14 Generic class declarations can be nested inside non-generic class declarations. 15

A generic class is referenced using a constructed type (§26.5). [Example: Given the generic class declaration 16

class List<T> {} 17

some examples of constructed types are List<T>, List<int> and List<List<string>>. end example] 18 A constructed type that uses one or more type parameters, such as List<T>, is called an open constructed 19 type (§26.5). A constructed type that uses no type parameters, such as List<int>, is called a closed 20 constructed type (§26.5). 21

Generic types can be “overloaded” on the number of type parameters; that is two type declarations within 22 the same namespace or outer type declaration can use the same identifier as long as they have a different 23 number of type parameters. 24

class C {} 25 class C<V> {} // OK 26 struct C<U,V> {} // OK 27 class C<A,B> {} // Error, C with two type parameters defined twice 28

The type lookup rules used during type name resolution (§10.8), simple name resolution (§14.5.2) and 29 member access (§14.5.4) respect the number of type parameters. 30

The base interfaces of a generic class declaration shall satisfy the uniqueness rule described in §26.3.1. 31

26.1.1 Type parameters 32

Type parameters can be supplied in a class declaration. Each type parameter is a simple identifier that 33 denotes a placeholder for a type argument supplied to create a constructed type. A type parameter is a formal 34 placeholder for a type that will be supplied later. By constrast, a type argument (§26.5.1) is the actual type 35 that is substituted for the type parameter when a constructed type is created. 36

type-parameter-list: 37 < type-parameters > 38

type-parameters: 39 attributesopt type-parameter 40 type-parameters , attributesopt type-parameter 41

Page 418: C# Language Specification

C# LANGUAGE SPECIFICATION

398

type-parameter: 1 identifier 2

Each type parameter in a class declaration defines a name in the declaration space (§10.3) of that class. 3 Thus, it cannot have the same name as another type parameter or a member declared in that class. A type 4 parameter shall not have the same name as the type itself. 5

The scope (§10.7) of a type parameter on a class includes the class-base, type-parameter-constraints-6 clauses, and class-body. Unlike members of a class, this scope does not extend to derived classes. Within its 7 scope, a type parameter can be used as a type. 8

Since a type parameter can be instantiated with many different actual type arguments, type parameters have 9 slightly different operations and restrictions than other types. [Note: These include: 10

• A type parameter cannot be used directly to declare a base class or interface (§17.1.2). 11

• The rules for member lookup on type parameters depend on the constraints, if any, applied to the type 12 parameter. They are detailed in §26.7.2. 13

• The available conversions for a type parameter depend on the constraints, if any, applied to the type 14 parameter. They are detailed in §26.7.4. 15

• The literal null cannot be converted to a type given by a type parameter, except if the type parameter is 16 known to be a reference type (§26.7.4). However, a default value expression (§14.5.13) can be used 17 instead. In addition, a value with a type given by a type parameter can be compared with null using == 18 and != (§14.9.6) unless the type parameter has the value type constraint (§26.7.4). 19

• A new expression (§14.5.10.1) can only be used with a type parameter if the type parameter is 20 constrained by a constructor-constraint or the value type constraint (§26.7). 21

• A type parameter cannot be used anywhere within an attribute (§24.2). 22

• A type parameter cannot be used in a member access or type name to identify a static member or a 23 nested type (§10.8, §14.5.4). 24

• In unsafe code, a type parameter cannot be used as an unmanaged-type (§25.2). 25

end note] 26

As a type, type parameters are purely a compile-time construct. At run-time, each type parameter is bound to 27 a run-time type that was specified by supplying a type argument to the generic type declaration. Thus, the 28 type of a variable declared with a type parameter will, at run-time, be a closed constructed type (§26.5.2). 29 The run-time execution of all statements and expressions involving type parameters uses the actual type that 30 was supplied as the type argument for that parameter. 31

26.1.2 The instance type 32

Each class declaration has an associated constructed type, the instance type. For a generic class declaration, 33 the instance type is formed by creating a constructed type (§26.5) from the type declaration, with each of the 34 supplied type arguments being the corresponding type parameter. Since the instance type uses the type 35 parameters, it can only be used where the type parameters are in scope; that is, inside the class declaration. 36 The instance type is the type of this for code written inside the class declaration. For non-generic classes, 37 the instance type is simply the declared class. [Example: The following shows several class declarations 38 along with their instance types: 39

class A<T> // instance type: A<T> 40 { 41 class B {} // instance type: A<T>.B 42 class C<U> {} // instance type: A<T>.C<U> 43 } 44 class D {} // instance type: D 45

end example] 46

Page 419: C# Language Specification

Chapter 26 Generics

399

26.1.3 Members of generic classes 1

All members of a generic class can use type parameters from any enclosing class, either directly or as part of 2 a constructed type. When a particular closed constructed type (§26.5.2) is used at run-time, each use of a 3 type parameter is replaced with the actual type argument supplied to the constructed type. [Example: 4

class C<V> 5 { 6 public V F1; 7 public C<V> F2 = null; 8

public C(V x) { 9 this.F1 = x; 10 this.F2 = this; 11 } 12 } 13

class Application 14 { 15 static void Main() { 16 C<int> x1 = new C<int>(1); 17 Console.WriteLine(x1.F1); // Prints 1 18

C<double> x2 = new C<double>(3.1415); 19 Console.WriteLine(x2.F1); // Prints 3.1415 20 } 21 } 22

end example] 23

Within instance function members, the type of this is the instance type (§26.1.2) of the containing 24 declaration. 25

Apart from the use of type parameters as types, members in generic class declarations follow the same rules 26 as members of non-generic classes. Additional rules that apply to particular kinds of members are discussed 27 in the following subclauses. 28

26.1.4 Static fields in generic classes 29

A static variable in a generic class declaration is shared amongst all instances of the same closed constructed 30 type (§26.5.2), but is not shared amongst instances of different closed constructed types. These rules apply 31 regardless of whether the type of the static variable involves any type parameters or not. 32

[Example: 33

class C<V> 34 { 35 static int count = 0; 36

public C() { 37 count++; 38 } 39

public static int Count { 40 get { return count; } 41 } 42 } 43

44

class Application 45 { 46 static void Main() { 47 C<int> x1 = new C<int>(); 48 Console.WriteLine(C<int>.Count); // Prints 1 49

C<double> x2 = new C<double>(); 50 Console.WriteLine(C<double>.Count); // Prints 1 51 Console.WriteLine(C<int>.Count); // Prints 1 52

Page 420: C# Language Specification

C# LANGUAGE SPECIFICATION

400

C<int> x3 = new C<int>(); 1 Console.WriteLine(C<int>.Count); // Prints 2 2 } 3 } 4

end example] 5

26.1.5 Static constructors in generic classes 6

A static constructor in a generic class is used to initialize static fields and to perform other initialization for 7 each different closed constructed type that is created from that generic class declaration. The type parameters 8 of the generic type declaration are in scope, and can be used, within the body of the static constructor. 9

A new closed constructed class type is initialized the first time that either: 10

• An instance of the closed constructed type is created. 11

• Any of the static members of the closed constructed type are referenced. 12

To initialize a new closed constructed class type, first a new set of static fields (§26.1.4) for that particular 13 closed constructed type is created. Each of the static fields is initialized to its default value (§12.2). Next, the 14 static field initializers (§17.4.5.1) are executed for those static fields. Finally, the static constructor is 15 executed. 16

Because the static constructor is executed exactly once for each closed constructed class type, it is a 17 convenient place to enforce run-time checks on the type parameter that cannot be checked at compile-time 18 via constraints (§26.7). [Example: The following type uses a static constructor to enforce that the type 19 argument is an enum: 20

class Gen<T> where T: struct 21 { 22 static Gen() { 23 if (!typeof(T).IsEnum) { 24 throw new ArgumentException("T must be an enum"); 25 } 26 } 27 } 28

end example] 29

26.1.6 Accessing protected members 30

[Note: The accessibility domain (§10.5.2) of a protected member declared in a generic class includes the 31 program text of all class declarations derived from any type constructed from that generic class. In the 32 example: 33

class C<T> 34 { 35 protected static T x; 36 } 37 class D: C<string> 38 { 39 static void Main() { 40 C<int>.x = 5; 41 } 42 } 43

The reference to protected member C<int>.x in D is valid even though the class D derives from 44 C<string>. 45

end note] 46

Within a generic class declaration, access to inherited protected instance members (§10.5.3) is permitted 47 through an instance of any class type constructed from the generic class. [Example: In the following code 48

Page 421: C# Language Specification

Chapter 26 Generics

401

class C<T> 1 { 2 protected T x; 3 } 4

class D<T>: C<T> 5 { 6 static void F() { 7 D<T> dt = new D<T>(); 8 D<int> di = new D<int>(); 9 D<string> ds = new D<string>(); 10 dt.x = default(T); 11 di.x = 123; 12 ds.x = "test"; 13 } 14 } 15

the three assignments to x are permitted because they all take place through instances of class types 16 constructed from the generic type. end example] 17

26.1.7 Overloading in generic classes 18

Methods, constructors, indexers, and operators within a generic class declaration can be overloaded; 19 however, overloading is constrained so that ambiguities cannot occur within constructed classes. Two 20 function members declared with the same names in the same generic class declaration shall have parameter 21 types such that no closed constructed type could have two members with the same name and signature. 22 When considering all possible closed constructed types, this rule includes type arguments that do not 23 currently exist in the current program, but could be written. Type constraints (§26.7) on the type parameter 24 are ignored for the purpose of this rule. 25

[Example: The following examples show overloads that are valid and invalid according to this rule: 26

interface I1<T> {…} 27

interface I2<T> {…} 28

class G1<U> 29 { 30 long F1(U u) {…} // Invalid overload, G<int> would have two 31 int F1(int i) {…} // members with the same signature 32

void F2(U u1, U u2) {…} // Valid overload, no type argument for U 33 void F2(int i, string s) {…} 34 // could be int and string simultaneously 35

void F3(I1<U> a) {…} // Valid overload 36 void F3(I2<U> a) {…} 37

void F4(U a) {…} // Valid overload 38 void F4(U[] a) {…} 39 } 40

class G2<U, V> 41 { 42 void F5(U u, V v) {…} // Invalid overload, G2<int, int> would 43 void F5(V v, U u) {…} // have two members with the same 44 // signature 45

void F6(U u, I1<V> v) {…} // Invalid overload, G2<I1<int>, int> 46 // would have two members with the 47 void F6(I1<V> v, U u) {…} // same signature 48

void F7(U u1, I1<V> v2) {…} // Valid overload, U cannot be V <V> 49 void F7(V v1, U u2) {…} // and I1 simultaneously 50

void F8(ref U u) {…} // Invalid overload 51 void F8(out V v) {…} 52 } 53

class C1 {…} 54

class C2 {…} 55

Page 422: C# Language Specification

C# LANGUAGE SPECIFICATION

402

class G3<U, V> where U: C1 where V: C2 1 { 2 void F9(U u) {…} // Invalid overload, constraints on U and V 3 void F9(V v) {…} // are ignored when checking overloads 4 } 5

end example] 6

26.1.8 Parameter array methods and type parameters 7

Type parameters can be used in the type of a parameter array. [Example: Given the declaration 8

class C<V> 9 { 10 static void F(int x, int y, params V[] args) { … } 11 } 12

the following invocations of the expanded form of the method: 13

C<int>.F(10, 20); 14 C<object>.F(10, 20, 30, 40); 15 C<string>.F(10, 20, "hello", "goodbye"); 16

correspond exactly to: 17

C<int>.F(10, 20, new int[] {}); 18 C<object>.F(10, 20, new object[] {30, 40}); 19 C<string>.F(10, 20, new string[] {"hello", "goodbye"} ); 20

end example] 21

26.1.9 Overriding and generic classes 22

Function members in generic classes can override function members in base classes, as usual. If the base 23 class is a non-generic type or a closed constructed type, then any overriding function member cannot have 24 constituent types that involve type parameters. When determining the overridden base member, the members 25 of the base classes shall be determined by substituting type arguments, as described in §26.5.4. Once the 26 members of the base classes are determined, the rules for overriding are the same as for non-generic classes. 27

[Example: The following example demonstrates how the overriding rules work in the presence of generics: 28

abstract class C<T> 29 { 30 public virtual T F() {…} 31 public virtual C<T> G() {…} 32 public virtual void H(C<T> x) {…} 33 } 34

class D: C<string> 35 { 36 public override string F() {…} // Ok 37 public override C<string> G() {…} // Ok 38 public override void H(C<int> x) {…} // Error, should be C<string> 39 } 40

class E<T, U>: C<U> 41 { 42 public override U F() {…} // Ok 43 public override C<U> G() {…} // Ok 44 public override void H(C<T> x) {…} // Error, should be C<U> 45 } 46

end example] 47

26.1.10 Operators in generic classes 48

Generic class declarations can define operators, following the same rules as non-generic class declarations. 49 The instance type (§26.1.2) of the class declaration shall be used in the declaration of operators in a manner 50 analogous to the normal rules for operators, as follows: 51

Page 423: C# Language Specification

Chapter 26 Generics

403

• A unary operator shall take a single parameter of the instance type. The unary ++ and -- operators shall 1 return the instance type or a type derived from the instance type. 2

• At least one of the parameters of a binary operator shall be of the instance type. 3

• Either the parameter type or the return type of a conversion operator shall be of the instance type. 4

[Example: The following shows some examples of valid operator declarations in a generic class: 5

class X<T> 6 { 7 public static X<T> operator ++(X<T> operand) {…} 8 public static int operator *(X<T> op1, int op2) {…} 9 public static explicit operator X<T>(T value) {…} 10 } 11

end example] 12

For a conversion operator that converts from a source type S to a target type T, when the rules specified in 13 §17.9.3 are applied, any type parameters associated with S or T are considered to be unique types that have 14 no inheritance relationship with other types, and any constraints on those type parameters are ignored. 15

[Example: In the following code 16

class C<T> {…} 17

class D<T>: C<T> 18 { 19 public static implicit operator C<int>(D<T> value) {…} // Ok 20 public static implicit operator C<T>(D<T> value) {…} // Error 21 } 22

the first operator declaration is permitted because, for the purposes of §17.9.3, T and int are considered 23 unique types with no relationship. However, the second operator is an error because C<T> is the base class 24 of D<T>. end example] 25

It is possible to declare operators that, for some type arguments, specify conversions that already exist as 26 pre-defined conversions. [Example: In the following code 27

struct Convertible<T> 28 { 29 public static implicit operator Convertible<T>(T value) {…} 30 public static explicit operator T(Convertible<T> value) {…} 31 } 32

when type object is specified as a type argument for T, the second operator declares a conversion that 33 already exists (an implicit, and therefore also an explicit, conversion exists from any type to type object). 34 end example] 35

In cases where a pre-defined conversion exists between two types, any user-defined conversions between 36 those types are ignored. Specifically: 37

• If a pre-defined implicit conversion (§13.1) exists from type S to type T, all user-defined conversions 38 (implicit or explicit) from S to T are ignored. 39

• If a pre-defined explicit conversion (§13.2) exists from type S to type T, any user-defined explicit 40 conversions from S to T are ignored. However, user-defined implicit conversions from S to T are still 41 considered. 42

[Example: For all types but object, the operators declared by the Convertible<T> type above do not 43 conflict with pre-defined conversions. For example: 44

void F(int i, Convertible<int> n) { 45 i = n; // Error 46 i = (int)n; // User-defined explicit conversion 47 n = i; // User-defined implicit conversion 48 n = (Convertible<int>)i; // User-defined implicit conversion 49 } 50

Page 424: C# Language Specification

C# LANGUAGE SPECIFICATION

404

However, for type object, pre-defined conversions hide the user-defined conversions in all cases but one: 1

void F(object o, Convertible<object> n) { 2 o = n; // Pre-defined boxing conversion 3 o = (object)n; // Pre-defined boxing conversion 4 n = o; // User-defined implicit conversion 5 n = (Convertible<object>)o; // Pre-defined unboxing conversion 6 } 7

end example] 8

26.1.11 Nested types in generic classes 9

A generic class declaration can contain nested type declarations. The type parameters of the enclosing class 10 can be used within the nested types. A nested type declaration can contain additional type parameters that 11 apply only to the nested type. 12

Every type declaration contained within a generic class declaration is implicitly a generic type declaration. 13 When writing a reference to a type nested within a generic type, the containing constructed type, including 14 its type arguments, shall be named. However, from within the outer class, the nested type can be used 15 without qualification; the instance type of the outer class can be implicitly used when constructing the nested 16 type. [Example: The following example shows three different correct ways to refer to a constructed type 17 created from Inner; the first two are equivalent: 18

class Outer<T> 19 { 20 class Inner<U> 21 { 22 public static void F(T t, U u) {…} 23 } 24

static void F(T t) { 25 Outer<T>.Inner<string>.F(t, "abc"); // These two statements have 26 Inner<string>.F(t, "abc"); // the same effect 27

Outer<int>.Inner<string>.F(3, "abc"); // This type is different 28

Outer.Inner<string>.F(t, "abc"); // Error, Outer needs type arg 29 } 30 } 31

end example] 32

Although it is bad programming style, a type parameter in a nested type can hide a member or type 33 parameter declared in the outer type. [Example: 34

class Outer<T> 35 { 36 class Inner<T> // Valid, hides Outer’s T 37 { 38 public T t; // Refers to Inner’s T 39 } 40 } 41

end example] 42

26.2 Generic struct declarations 43

[Note: A struct declaration (§18.1), can optionally define type parameters and their associated constraints: 44

struct-declaration: 45 attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt 46

struct-interfacesopt type-parameter-constraints-clausesopt struct-body ;opt 47

end note] 48

The rules for generic class declarations (§26.1 and its subclauses) apply equally to generic struct 49 declarations, as do the exceptions noted in §18.3. 50

Page 425: C# Language Specification

Chapter 26 Generics

405

26.3 Generic interface declarations 1

[Note: An interface declaration (§20.1), can optionally define type parameters and their associated 2 constraints: 3

interface-declaration: 4 attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt 5 interface-baseopt type-parameter-constraints-clausesopt interface-body ;opt 6

end note] 7

Except where noted below, generic interface declarations follow the same rules as non-generic interface 8 declarations. 9

Each type parameter in an interface declaration defines a name in the declaration space (§10.3) of that 10 interface. The scope (§10.7) of a type parameter on an interface includes the interface-base, type-parameter-11 constraints-clauses, and interface-body. Within its scope, a type parameter can be used as a type. The same 12 restrictions apply to type parameters on interfaces as apply to type parameter on classes (§26.1.1). 13

26.3.1 Uniqueness of implemented interfaces 14

The interfaces implemented by a generic type declaration shall remain unique for all possible constructed 15 types. Without this rule, it would be impossible to determine the correct method to call for certain 16 constructed types. [Example: Suppose a generic class declaration were permitted to be written as follows: 17

interface I<T> 18 { 19 void F(); 20 } 21

class X<U, V>: I<U>, I<V> // Error: I<U> and I<V> conflict 22 { 23 void I<U>.F() {…} 24 void I<V>.F() {…} 25 } 26

Were this permitted, it would be impossible to determine which code to execute in the following case: 27

I<int> x = new X<int,int>(); 28 x.F(); 29

end example] 30

To determine if the interface list of a generic type declaration is valid, the following steps are performed: 31

• Let L be the list of interfaces directly specified in a generic class, struct, or interface declaration C. 32

• Add to L any base interfaces of the interfaces already in L. 33

• Remove any duplicates from L. 34

• If any possible constructed type created from C would, after type arguments are substituted into L, cause 35 two interfaces in L to be identical, then the declaration of C is invalid. Constraint declarations are not 36 considered when determining all possible constructed types. 37

[Note: In the class declaration X above, the interface list L consists of I<U> and I<V>. The declaration is 38 invalid because any constructed type with U and V being the same type would cause these two interfaces to 39 be identical types. end note] 40

[Example: It is possible for interfaces specified at different inheritance levels to unify: 41

interface I<T> 42 { 43 void F(); 44 } 45

Page 426: C# Language Specification

C# LANGUAGE SPECIFICATION

406

class Base<U>: I<U> 1 { 2 void I<U>.F() {…} 3 } 4

class Derived<U, V>: Base<U>, I<V> // Ok 5 { 6 void I<V>.F() {…} 7 } 8

This code is valid even though Derived<U, V> implements both I<U> and I<V>. The code 9

I<int> x = new X<int,int>(); 10 x.F(); 11

invokes the method in Derived, since Derived<int, int> effectively re-implements I<int> (§20.4.4). 12 end example] 13

26.3.2 Explicit interface member implementations 14

Explicit interface member implementations work with constructed interface types in essentially the same 15 way as with simple interface types. As usual, an explicit interface member implementation shall be qualified 16 by an interface-type indicating which interface is being implemented. This type can be a simple interface or 17 a constructed interface [Example: 18

interface IList<ElementType> 19 { 20 ElementType[] GetElements(); 21 } 22

interface IDictionary<KeyType,ElementType> 23 { 24 ElementType this[KeyType key] { get; } 25 void Add(KeyType key, ElementType value); 26 } 27

class List<ElementType>: IList<ElementType>, IDictionary<int,ElementType> 28 { 29 ElementType[] IList<ElementType>.GetElements() {…} 30

// Return the element at index 31 ElementType IDictionary<int,ElementType>.this[int index] {…} 32

// Add value at the index 33 void IDictionary<int,ElementType>.Add(int index, ElementType value) 34 {…} 35 } 36

end example] 37

26.4 Generic delegate declarations 38

[Note: A delegate declaration (§22.1), can optionally define type parameters and their associated constraints: 39

delegate-declaration: 40 attributesopt delegate-modifiersopt delegate return-type identifier type-parameter-listopt 41 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt ; 42

end note] 43

Generic delegate declarations follow the same rules as non-generic delegate declarations, except where 44 noted below. Each type parameter in a generic delegate declaration defines a name in a special declaration 45 space (§10.3) that is associated with that delegate declaration. The scope (§10.7) of a type parameter in a 46 delegate declaration includes the return-type, formal-parameter-list, and type-parameter-constraints-47 clauses. 48

Like other generic type declarations, type arguments shall be given to form a constructed delegate type. The 49 parameter types and return type of a constructed delegate type are formed by substituting, for each type 50 parameter in the delegate declaration, the corresponding type argument of the constructed delegate type. The 51

Page 427: C# Language Specification

Chapter 26 Generics

407

resulting return type and parameter types are used in determining what methods are consistent (§22.1) with a 1 constructed delegate type. [Example: 2

delegate bool Predicate<T>(T value); 3

class X 4 { 5 static void Main() { 6 Predicate<int> p1 = 7 delegate(int i) { 8 //... 9 }; 10

Predicate<string> p2 = 11 delegate(string s) { 12 //... 13 }; 14 } 15 } 16

end example] 17

26.5 Constructed types 18

A generic type declaration, by itself, denotes an unbound generic type that is used as a “blueprint” to form 19 many different types, by way of applying type arguments. The type arguments are written within angle 20 brackets (< and >) immediately following the name of the generic type declaration. A type that is named 21 with at least one type argument is called a constructed type. A constructed type can be used in most places 22 in the language in which a type name can appear. An unbound generic type can only be used within a typeof-23 expression (§14.5.11). 24

Constructed types can also be used in expressions as simple names (§14.5.2) or when accessing a member 25 (§14.5.4). 26

When a namespace-or-type-name is evaluated, only generic types with the correct number of type 27 parameters are considered. Thus, it is possible to use the same identifier to identify different types, as long as 28 the types have different numbers of type parameters. This is useful when mixing generic and non-generic 29 classes in the same program. [Example: 30

namespace Widgets 31 { 32 class Queue {…} 33 class Queue<ElementType> {…} 34 } 35

namespace MyApplication 36 { 37 using Widgets; 38

class X 39 { 40 Queue q1; //Non-generic Widgets.Queue 41 Queue<int> q2; // Generic Widgets.Queue 42 } 43 } 44

end example] 45

The detailed rules for name lookup in the namespace-or-typename productions is described in §10.8. The 46 resolution of ambiguities in these productions is described in §9.2.3. 47

A type-name might identify a constructed type even though it doesn’t specify type parameters directly. This 48 can occur where a type is nested within a generic class declaration, and the instance type of the containing 49 declaration is implicitly used for name lookup (§26.1.11). [Example: 50

Page 428: C# Language Specification

C# LANGUAGE SPECIFICATION

408

class Outer<T> 1 { 2 public class Inner {…} 3 public Inner i; // Type of i is Outer<T>.Inner 4 } 5

end example] 6

[Note: In unsafe code, a constructed type shall not be used as an unmanaged-type (§25.2). end note] 7

26.5.1 Type arguments 8

Each argument in a type argument list is simply a type. 9

type-argument-list: 10 < type-arguments > 11

type-arguments: 12 type-argument 13 type-arguments , type-argument 14

type-argument: 15 type 16

Type arguments can be constructed types or type parameters. [Note: In unsafe code (§25), a type-argument 17 shall not be a pointer-type. end note] Each type argument shall satisfy any constraints on the corresponding 18 type parameter (§26.7.1). 19

26.5.2 Open and closed types 20

All types can be classified as either open types or closed types. An open type is a type that involves type 21 parameters. More specifically: 22

• A type parameter defines an open type. 23

• An array type is an open type if and only if its element type is an open type. 24

• A constructed type is an open type if and only if one or more of its type arguments is an open type. A 25 constructed nested type is an open type if and only if one or more of its type arguments or the type 26 arguments of its containing type(s) is an open type. 27

A closed type is a type that is not an open type. 28

At run-time, all of the code within a generic type declaration is executed in the context of a closed 29 constructed type that was created by applying type arguments to the generic declaration. Each type 30 parameter within the generic type is bound to a particular run-time type. The run-time processing of all 31 statements and expressions always occurs with closed types, and open types occur only during compile-time 32 processing. 33

Each closed constructed type has its own set of static variables, which are not shared with any other closed 34 constructed types. Since an open type does not exist at run-time, there are no static variables associated with 35 an open type. Two closed constructed types are the same type if they are constructed from the same unbound 36 generic type, and their corresponding type arguments are the same type. 37

26.5.3 Base classes and interfaces of a constructed type 38

A constructed class type has a direct base class, just like a simple class type. If the generic class declaration 39 does not specify a base class, the base class is object. If a base class is specified in the generic class 40 declaration, the base class of the constructed type is obtained by substituting, for each type-parameter in the 41 base class declaration, the corresponding type-argument of the constructed type. [Example: Given the 42 generic class declarations 43

class B<U,V> {…} 44

class G<T>: B<string,T[]> {…} 45

Page 429: C# Language Specification

Chapter 26 Generics

409

the base class of the constructed type G<int> would be B<string,int[]>. end example] 1

Similarly, constructed class, struct, and interface types have a set of explicit base interfaces. The explicit 2 base interfaces are formed by taking the explicit base interface declarations on the generic type declaration, 3 and substituting, for each type-parameter in the base interface declaration, the corresponding type-argument 4 of the constructed type. 5

The set of all base classes and base interfaces for a type is formed, as usual, by recursively getting the base 6 classes and interfaces of the immediate base classes and interfaces. [Example: Given the generic class 7 declarations: 8

class A {…} 9

class B<T>: A {…} 10

class C<T>: B<IComparable<T>> {…} 11

class D<T>: C<T[]> {…} 12

the base classes of D<int> are C<int[]>, B<IComparable<int[]>>, A, and object. end example] 13

26.5.4 Members of a constructed type 14

The non-inherited members of a constructed type are obtained by substituting, for each type-parameter in 15 the member declaration, the corresponding type-argument of the constructed type. The substitution process 16 is based on the semantic meaning of type declarations, and is not simply textual substitution. 17

[Example: Given the generic class declaration 18

class Gen<T,U> 19 { 20 public T[,] a; 21

public void G(int i, T t, Gen<U,T> gt) {…} 22

public U Prop { get {…} set {…} } 23

public int H(double d) {…} 24 } 25

the constructed type Gen<int[],IComparable<string>> has the following members: 26

public int[,][] a; 27

public void G(int i, int[] t, Gen<IComparable<string>,int[]> gt) {…} 28

public IComparable<string> Prop { get {…} set {…} } 29

public int H(double d) {…} 30

The type of the member a in the generic class declaration Gen is “two-dimensional array of T”, so the type 31 of the member a in the constructed type above is “two-dimensional array of one-dimensional array of int”, 32 or int[,][]. end example] 33

The inherited members of a constructed type are obtained in a similar way. First, all the members of the 34 immediate base class are determined. If the base class is itself a constructed type, this might involve a 35 recursive application of the current rule. Then, each of the inherited members is transformed by substituting, 36 for each type-parameter in the member declaration, the corresponding type-argument of the constructed 37 type. [Example: 38

class B<U> 39 { 40 public U F(long index) {…} 41 } 42

class D<T>: B<T[]> 43 { 44 public T G(string s) {…} 45 } 46

In the above example, the constructed type D<int> has a non-inherited member public int G(string 47 s) obtained by substituting the type argument int for the type parameter T. D<int> also has an inherited 48

Page 430: C# Language Specification

C# LANGUAGE SPECIFICATION

410

member from the class declaration B. This inherited member is determined by first determining the members 1 of the constructed type B<T[]> by substituting T[] for U, yielding public T[] F(long index). Then, the 2 type argument int is substituted for the type parameter T, yielding the inherited member public int[] 3 F(long index). end example] 4

26.5.5 Accessibility of a constructed type 5

A constructed type C<T1, ...,TN> is accessible when all of its components C, T1, ..., TN are accessible. More 6 precisely, the accessibility domain for a constructed type is the intersection of the accessibility domain of the 7 unbound generic type and the accessibility domains of the type arguments. 8

26.5.6 Conversions 9

Constructed types follow the same conversion rules (§13) as do non-generic types. When applying these 10 rules, the base classes and interfaces of constructed types shall be determined as described in §26.5.3. 11

No special conversions exist between constructed reference types other than those described in §13. In 12 particular, unlike array types, constructed reference types do not exhibit “co-variant” conversions. This 13 means that a type List<B> has no conversion (either implicit or explicit) to List<A> even if B is derived 14 from A. Likewise, no conversion exists from List<B> to List<object>. 15

[Note: The rationale for this is simple: if a conversion to List<A> is permitted, then apparently one can 16 store values of type A into the list. But this would break the invariant that every object in a list of type 17 List<B> is always a value of type B, or else unexpected failures can occur when assigning into collection 18 classes. end note] 19

[Example: The behavior of conversions and runtime type checks is illustrated below: 20

class A {…} 21

class B: A {…} 22

class Collection {…} 23

class List<T>: Collection {…} 24

class Test 25 { 26 void F() { 27 List<A> listA = new List<A>(); 28 List<B> listB = new List<B>(); 29

Collection c1 = listA; // Ok, List<A> is a Collection 30 Collection c2 = listB; // Ok, List<B> is a Collection 31

List<A> a1 = listB; // Error, no implicit conversion 32 List<A> a2 = (List<A>)listB; // Error, no explicit conversion 33 } 34 } 35

end example] 36

26.5.7 Using alias directives 37

Using aliases can name a closed constructed type, but shall not name a generic type declaration without 38 supplying type arguments. [Example: 39

namespace N1 40 { 41 class A<T> 42 { 43 class B {} 44 } 45 } 46

namespace N2 47 { 48 using W = N1.A; // Error, cannot name generic type 49

Page 431: C# Language Specification

Chapter 26 Generics

411

using X = N1.A.B; // Error, cannot name generic type 1

using Y = N1.A<int>; // Ok, can name closed constructed type 2 } 3

end example] 4

26.6 Generic methods 5

A generic method is a method whose declaration includes a type-parameter-list (§17.5 and §20.2.1). Generic 6 methods can be declared inside class, struct, or interface declarations, which can themselves be either 7 generic or non-generic. If a generic method is declared inside a generic type declaration, the body of the 8 method can refer to both the type parameters of the method, and the type parameters of the containing 9 declaration. 10

The type-parameter-list and type-parameter-constraints-clauses of a generic method declaration have the 11 same syntax and purpose as in a generic type declaration. The method’s type-parameters are in scope 12 throughout the method-declaration, and can be used to form types throughout that scope in return-type, 13 method-body, and type-parameter-constraints-clauses but not in attributes. 14

The name of a method type parameter cannot be the same as the name of an ordinary parameter in the same 15 method. 16

[Example: The following example finds the first element in an array, if any, that satisfies the given test 17 delegate. (Generic delegates are described in §26.4.) 18

public delegate bool Test<T>(T item); 19

public class Finder 20 { 21 public static T Find<T>(T[] items, Test<T> test) { 22 foreach (T item in items) { 23 if (test(item)) 24 return item; 25 } 26 throw new InvalidOperationException("Item not found"); 27 } 28 } 29

end example] 30

26.6.1 Generic method signatures 31

For the purposes of signature comparisons any type-parameter-constraints-clauses are ignored, as are the 32 names of the method’s type-parameters, but the number of generic type parameters is relevant, as are the 33 ordinal positions of type-parameters in left-to-right ordering (§10.6). [Example: The following example 34 shows how method signatures are affected by this rule: 35

class A {} 36

class B {} 37

interface IX 38 { 39 T F1<T>(T[] a, int i); // Error, both declarations have the same 40 void F1<U>(U[] a, int i); // signature because return type and type 41 // parameter names are not significant 42

void F2<T>(int x); // Ok, the number of type parameters is 43 void F2(int x); // part of the signature 44

void F3<T>(T t) where T: A; // Error, constraints are not 45 void F3<T>(T t) where T: B; // considered in signatures 46 } 47

end example] 48

Page 432: C# Language Specification

C# LANGUAGE SPECIFICATION

412

26.6.2 Virtual generic methods 1

Generic methods can be declared using the abstract, virtual, and override modifiers. The signature 2 matching rules described in §10.6 are used when matching methods for overriding or interface 3 implementation. When a generic method overrides a generic method declared in a base class, or is an 4 explicit interface member implementation of a method in a base interface, the method shall not specify any 5 type-parameter-constraints-clauses. In these cases, the type parameters of the method inherit constraints 6 from the method being overridden or implemented. [Example: 7

abstract class Base 8 { 9 public abstract T F<T,U>(T t, U u) where U : T; 10

public abstract T G<T>(T t) where T: IComparable; 11 } 12

interface I 13 { 14 bool M<T>(T a, T, b) where T : class; 15 } 16

class Derived: Base, I 17 { 18 19

public override X F<X,Y>(X x, Y y) // Ok 20 { 21 // The implicit constraint Y : X is inherited 22 // so y is implicitly convertible to type X. 23 return y; 24 } 25

public override T G<T>(T t) 26 where T: IComparable // Error – constraints not allowed 27 {…} 28

bool I.M<U>(U a, U b) 29 { 30 // The implicit constraint U : class is inherited 31 // so a and b can be compared using the reference type 32 // equality operators. 33 return a == b; 34 } 35 } 36

The override of F is valid because type parameter names are permitted to differ. Within Derived.F, the 37 type parameter Y implicitly has the constraint Y : X as inherited from Base.F. The override of G is invalid 38 because overrides are not permitted to specify type parameter constraints. The explicit method 39 implementation I.M in Derived implicitly inherits the U : class constraint from the interface method. 40

When a generic method implicitly implements an interface method, the constraints given for each method 41 type parameter shall be equivalent in both declarations (after any interface type parameters are replaced with 42 the appropriate type arguments), where method type parameters are identified by ordinal positions, left to 43 right. [Example: 44

interface I<A, B, C> 45 { 46 void F<T>(T t) where T : A; 47 void G<T>(T t) where T : B; 48 void H<T>(T t) where T : C; 49 } 50

class Cls : I<object,Cls,string> 51 { 52 public void F<T>(T t) {…} // Ok 53 public void G<T>(T t) where T : Cls {…} // Ok 54 public void H<T>(T t) where T : string {…} // Error 55 } 56

Page 433: C# Language Specification

Chapter 26 Generics

413

The method Cls.F<T> implicitly implements I<object,Cls,string>.F<T>. In this case, Cls.F<T> is 1 not required (nor permitted) to specify the constraint T : object since object is an implicit constraint on 2 all type parameters. The method Cls.G<T> implicitly implements I<object,Cls,string>.G<T> 3 because the constraints match those in the interface, after the interface type parameters are replaced with the 4 corresponding type arguments. The constraint for method Cls.H<T> is an error since sealed types (string in 5 this case) cannot be used as constraints. Omitting the constraint would also be an error since constraints of 6 implicit interface method implementations are required to match. Thus, it’s impossible to implicitly 7 implement I<object,Cls,string>.H<T>. This interface method needs to be implemented using an 8 explicit interface member implementation: 9

class Cls : I<object,Cls,string> 10 { 11 … 12 public void H<U>(U u) where U : class {…} 13 void I<object,Cls,string>.H<T>(T t) 14 { 15 string s = t; // Ok 16 H<T>(t); 17 } 18 } 19

In this example, the explicit interface member implementation invokes a public method having strictly 20 weaker constraints. Note that the assignment from t to s is valid since T inherits a constraint of 21 T : string, even though this constraint is not expressible in source code. end example] 22

26.6.3 Calling generic methods 23

A generic method invocation can explicitly specify a type argument list, or it can omit the type argument list 24 and rely on type inference to determine the type arguments. The exact compile-time processing of a method 25 invocation, including a generic method invocation, is described in §14.5.5.1. When a generic method is 26 invoked without a type argument list, type inference takes place as described in §26.6.4. 27

[Example: The following example shows how overload resolution occurs after type inference and after type 28 arguments are substituted into the parameter list: 29

class Test 30 { 31 static void F<T>(int x, T y) { 32 Console.WriteLine("one"); 33 } 34

static void F<T>(T x, long y) { 35 Console.WriteLine("two"); 36 } 37

static void Main() { 38 F<int>(5, 324); // Ok, prints "one" 39 F<byte>(5, 324); // Ok, prints "two" 40 F<double>(5, 324); // Error, ambiguous 41

F(5, 324); // Ok, prints "one" 42 F(5, 324L); // Error, ambiguous 43 } 44 } 45

end example] 46

26.6.4 Inference of type arguments 47

When a generic method is called without specifying type arguments, a type inference process attempts to 48 infer type arguments for the call. The presence of type inference allows a more convenient syntax to be used 49 for calling a generic method, and allows the programmer to avoid specifying redundant type information. 50 [Example: Given the method declaration: 51

class Chooser 52 { 53 static Random rand = new Random(); 54

Page 434: C# Language Specification

C# LANGUAGE SPECIFICATION

414

public static T Choose<T>(T first, T second) { 1 return (rand.Next(2) == 0) ? first : second; 2 } 3 } 4

it is possible to invoke the Choose method without explicitly specifying a type argument: 5

int i = Chooser.Choose(5, 213); // Calls Choose<int> 6

string s = Chooser.Choose("foo", "bar"); // Calls Choose<string> 7

Through type inference, the type arguments int and string are determined from the arguments to the 8 method. end example] 9

Type inference occurs as part of the compile-time processing of a method invocation (§14.5.5.1) and takes 10 place before the overload resolution step of the invocation. When a particular method group is specified in a 11 method invocation, and no type arguments are specified as part of the method invocation, type inference is 12 applied to each generic method in the method group. If type inference succeeds, then the inferred type 13 arguments are used to determine the types of arguments for subsequent overload resolution. If overload 14 resolution chooses a generic method as the one to invoke, then the inferred type arguments are used as the 15 actual type arguments for the invocation. If type inference for a particular method fails, that method does not 16 participate in overload resolution. The failure of type inference, in and of itself, does not cause a compile-17 time error. However, it often leads to a compile-time error when overload resolution then fails to find any 18 applicable methods. 19

If the supplied number of arguments is different than the number of parameters in the method, then inference 20 immediately fails. Otherwise, type inference first occurs independently for each regular argument that is 21 supplied to the method. Assume this argument has type A, and the corresponding parameter has type P. Type 22 inferences are produced by relating the types A and P according to the following steps: 23

• Nothing is inferred from the argument (but type inference succeeds) if any of the following are true: 24

o P does not involve any method type parameters. 25

o The argument is the null literal. 26

o The argument is an anonymous method. 27

o The argument is a method group. 28

• If P is an array type, and A is an array type of the same rank, then replace A and P, respectively, with the 29 element types of A and P, and repeat this step. 30

• If P is an array type, and A is not an array type of the same rank, then type inference fails for the generic 31 method. 32

• If P is a method type parameter, then type inference succeeds for this argument, and A is the type 33 inferred for that type parameter. 34

• Otherwise, P shall be a constructed type. If, for each method type parameter MX that occurs in P, exactly 35 one type TX can be determined such that replacing each MX with each TX produces a type to which A is 36 convertible by a standard implicit conversion, then inferencing succeeds for this argument, and each TX 37 is the type inferred for each MX. Method type parameter constraints, if any, are ignored for the purpose of 38 type inference. If, for a given MX, no TX exists, or more than one TX exists, then type inference fails for 39 the generic method (a situation where more than one TX exists can only occur if P is a generic interface 40 type and A implements multiple constructed versions of that interface). 41

If all of the method arguments are processed successfully by the above algorithm, then all inferences that 42 were produced from the arguments are pooled. Type inference is said to have succeeded for the given 43 generic method and argument list if both of the following are true:: 44

• Each type parameter of the method had a type argument inferred for it (in short, the set of inferences is 45 complete). 46

Page 435: C# Language Specification

Chapter 26 Generics

415

• For each type parameter, all of the inferences for that type parameter infer the same type argument (in 1 short, the set of inferences is consistent). 2

If the generic method was declared with a parameter array (§17.5.1.4), then type inference is first performed 3 against the method in its normal form. If type inference succeeds, and the resultant method is applicable, 4 then the method is eligible for overload resolution in its normal form. Otherwise, type inference is 5 performed against the method in its expanded form (§14.4.2.1). 6

26.6.5 Using a generic method with a delegate 7

An instance of a delegate can be created that refers to a generic method declaration. The exact compile-time 8 processing of a delegate creation expression, including a delegate creation expression that refers to a generic 9 method, is described in §14.5.10.3. 10

The type arguments used when invoking a generic method through a delegate are determined when the 11 delegate is instantiated. The type arguments can be given explicitly via a type-argument-list, or determined 12 by type inference (§26.6.4). If type inference is used, the parameter types of the delegate are used as 13 argument types in the inference process. The return type of the delegate is not used for inference. [Example: 14 The following example shows both ways of supplying a type argument to a delegate instantiation 15 expression: 16

delegate int D(string s, int i); 17

delegate int E(); 18

class X 19 { 20 public static T F<T>(string s, T t) {…} 21

public static T G<T>() {…} 22

static void Main() { 23 D d1 = new D(F<int>); // Ok, type argument given explicitly 24 D d2 = new D(F); // Ok, int inferred as type argument 25

E e1 = new E(G<int>); // Ok, type argument given explicitly 26 E e2 = new E(G); // Error, cannot infer from return type 27 } 28 } 29

end example] 30

Whenever a generic method is used to create delegate instance, type arguments are given or inferred when 31 the delegate instance is created, and a type-argument-list shall not be supplied when the delegate is invoked 32 (§22.3). 33

26.6.6 No generic properties, events, indexers, operators, constructors, or destructors 34

Properties, events, indexers, operators, constructors, and destructors shall not themselves have type 35 parameters (although they can occur in generic types, and use the type parameters from an enclosing type). 36

26.7 Constraints 37

Generic type and method declarations can optionally specify type parameter constraints by including a type-38 parameter-constraints-clauses in the declaration. 39

type-parameter-constraints-clauses: 40 type-parameter-constraints-clause 41 type-parameter-constraints-clauses type-parameter-constraints-clause 42

type-parameter-constraints-clause: 43 where type-parameter : type-parameter-constraints 44

Page 436: C# Language Specification

C# LANGUAGE SPECIFICATION

416

type-parameter-constraints: 1 primary-constraint 2 secondary-constraints 3 constructor-constraint 4 primary-constraint , secondary-constraints 5 primary-constraint , constructor-constraint 6 secondary-constraints , constructor-constraint 7 primary-constraint , secondary-constraints , constructor-constraint 8

primary-constraint: 9 class-type 10 class 11 struct 12

secondary-constraints: 13 interface-type 14 type-parameter 15 secondary-constraints , interface-type 16 secondary-constraints , type-parameter 17

constructor-constraint: 18 new ( ) 19

Each type-parameter-constraints-clause consists of the token where, followed by the name of a type 20 parameter, followed by a colon and the list of constraints for that type parameter. There shall be at most one 21 where clause for each type parameter, and the where clauses can be listed in any order. Like the get and 22 set tokens in a property accessor, the where token is not a keyword. 23

The list of constraints given in a where clause can include any of the following components, in this order: a 24 single primary constraint, one or more secondary constraints, and the constructor constraint, new(). 25

A primary constraint can be a class type or the reference type constraint class or the value type constraint 26 struct. A secondary constraint can be a type-parameter or interface-type. 27

The reference type constraint specifies that a type argument used for the type parameter must be a reference 28 type (§26.7.1). Any class type, interface type, delegate type, array type, or type parameter known to be a 29 reference type (as defined below) satisfies this constraint. 30

The value type constraint specifies that a type argument used for the type parameter must be a value type 31 (§26.7.1). Any struct type, enum type, or type parameter having the value type constraint satisfies this 32 constraint. A type parameter having the value type constraint shall not also have the constructor-constraint. 33

Pointer types are never allowed to be type arguments so are not considered to satisfy either the reference 34 type or value type constraints. 35

If a constraint is a class type, an interface type, or a type parameter, that type specifies a minimal “base type” 36 that every type argument used for that type parameter shall support. Whenever a constructed type or generic 37 method is used, the type argument is checked against the constraints on the type parameter at compile-time. 38 The type argument supplied shall derive from or implement all of the constraints given for that type 39 parameter. 40

A class-type constraint shall satisfy the following rules: 41

• The type shall be a class type. 42

• The type shall not be sealed. 43

• The type shall not be one of the following types: System.Array, System.Delegate, System.Enum, 44 or System.ValueType. 45

• The type shall not be object. [Note: Since all types derive from object, such a constraint would have 46 no effect if it were permitted. end note] 47

Page 437: C# Language Specification

Chapter 26 Generics

417

• At most one constraint for a given type parameter can be a class type. 1

A type specified as an interface-type constraint shall satisfy the following rules: 2

• The type shall be an interface type. 3

• A type shall not be specified more than once in a given where clause. 4

In either case, the constraint can involve any of the type parameters of the associated type or method 5 declaration as part of a constructed type, and can involve the type being declared. 6

Any class or interface type specified as a type parameter constraint shall be at least as accessible (§10.5.4) as 7 the generic type or method being declared. 8

A type specified as a type-parameter constraint shall satisfy the following rules: 9

• The type shall be a type parameter. 10

• A type shall not be specified more than once in a given where clause. 11

In addition there shall be no cycles in the dependency graph of type parameters, where dependency is a 12 transitive relation defined by: 13

• If a type parameter T is used as a constraint for type parameter S then S depends on T. 14

• If a type parameter S depends on a type parameter T and T depends on a type paremeter U then S 15 depends on U. 16

Given this relation, it is a compile-time error for a type parameter to depend on itself (directly or indirectly). 17

Any constraints must be consistent among dependent type parameters. If type parameter S depends on type 18 parameter T then: 19

• T must not have the value type constraint. Otherwise, T is effectively sealed so S would be forced to be 20 the same type as T, eliminating the need for two type parameters. 21

• If S has the value type constraint then T must not have a class-type constraint. 22

• If S has a class-type constraint A and T has a class-type constraint B then there must be an identity 23 conversion or implicit reference conversion from A to B or an implicit reference conversion from B to A. 24

• If S also depends on type parameter U and U has a class-type constraint A and T has a class-type 25 constraint B then there must be an identity conversion or implicit reference conversion from A to B or an 26 implicit reference conversion from B to A. 27

It is valid for S to have the value type constraint and T to have the reference type constraint. Effectively this 28 limits T to the types System.Object, System.ValueType, System.Enum and any interface type. 29

If the where clause for a type parameter includes a constructor constraint (which has the form new()), it is 30 possible to use the new operator to create instances of the type (§14.5.10.1). Any type argument used for a 31 type parameter with a constructor constraint shall have a public parameterless constructor (this includes all 32 value types) or be a type parameter having the value type constraint or constructor constraint (see §26.7.1 for 33 details). 34

[Example: The following are examples of constraints: 35

interface IPrintable 36 { 37 void Print(); 38 } 39

interface IComparable<T> 40 { 41 int CompareTo(T value); 42 } 43

interface IKeyProvider<T> 44 { 45

Page 438: C# Language Specification

C# LANGUAGE SPECIFICATION

418

T GetKey(); 1 } 2

class Printer<T> where T: IPrintable {…} 3

class SortedList<T> where T: IComparable<T> {…} 4

class Dictionary<K, V> 5 where K: IComparable<K> 6 where V: IPrintable, IKeyProvider<K>, new() 7 { 8 … 9 } 10

The following example is in error because it causes a circularity in the dependency graph of the type 11 parameters: 12

class Circular<S, T> 13 where S: T 14 where T: S // Error – circularity in dependency graph 15 {...} 16

The following examples illustrate additional invalid situations: 17

class Sealed<S, T> 18 where S: T 19 where T: struct // Error – T is sealed 20 {...} 21

class A {...} 22

class B {...} 23

class Incompat<S, T> 24 where S: A, T 25 where T: B // Error – incompatible class-type constraints 26 {...} 27

class StructWithClass<S, T, U> 28 where S: struct, T 29 where T: U 30 where U: A // Error – A incompatible with struct 31 {...} 32

end example] 33

The effective base class of a type parameter T is defined as follows: 34

• If T has no primary constraints or type parameter constraints, its effective base class is object. 35

• If T has the value type constraint, its effective base class is System.ValueType. 36

• If T has a class-type constraint C but no type-parameter constraints, its effective base class is C. 37

• If T has no class-type constraint but has one or more type-parameter constraints, its effective base class 38 is the most encompassed type (§13.4.2) in the set of effective base classes of its type-parameter 39 constraints. The consistency rules ensure that such a most encompassed type exists. 40

• If T has both a class-type constraint and one or more type-parameter constraints, its effective base class 41 is the most encompassed type (§13.4.2) in the set consisting of the class-type constraint of T and the 42 effective base classes of its type-parameter constraints. The consistency rules ensure that such a most 43 encompassed type exists. 44

The effective interface set of a type parameter T is defined as follows: 45

• If T has no secondary-constraints, its effective interface set is empty. 46

• If T has interface-type constraints but no type-parameter constraints, its effective interface set is its set 47 of interface-type constraints. 48

• If T has no interface-type constraints but has type-parameter constraints, its effective interface set is the 49 union of the effective interface sets of its type-parameter constraints. 50

Page 439: C# Language Specification

Chapter 26 Generics

419

• If T has both interface-type constraints and type-parameter constraints, its effective interface set is the 1 union of its set of interface-type constraints and the effective interface sets of its type-parameter 2 constraints. 3

A type parameter is known to be a reference type if it has the reference type constraint or its effective base 4 class is not object or System.ValueType. 5

Values of a constrained type parameter type can be used to access the instance members implied by the 6 constraints. [Example: In the following code 7

interface IPrintable 8 { 9 void Print(); 10 } 11

class Printer<T> where T: IPrintable 12 { 13 void PrintOne(T x) { 14 x.Print(); 15 } 16 } 17

the methods of IPrintable can be invoked directly on x because T is constrained to always implement 18 IPrintable. end example] 19

Two generic partial type declarations (in the same program) contribute to the same unbound generic type if 20 they have the same fully qualified name (which includes the number of type parameters) (§10.3). Two such 21 partial type declarations shall specify the same name for each type parameter, in order. 22

When a partial generic type declaration includes constraints, the constraints shall agree with all other parts 23 that include constraints. Specifically, each part that includes constraints shall have constraints for the same 24 set of type parameters, and for each type parameter, the sets of primary, secondary, and constructor 25 constraints shall be equivalent. Two sets of constraints are equivalent if they contain the same members. If 26 no part of a partial generic type specifies type parameter constraints, the type parameters are considered 27 unconstrained. [Example: 28

partial class Map<K,V> 29 where K: IComparable<K> 30 where V: IKeyProvider<K>, new() 31 { 32 ... 33 } 34

partial class Map<K,V> 35 where V: IKeyProvider<K>, new() 36 where K: IComparable<K> 37 { 38 ... 39 } 40

partial class Map<K,V> 41 { 42 ... 43 } 44

is correct because those parts that include constraints (the first two) effectively specify the same set of 45 primary, secondary, and constructor constraints for the same set of type parameters, respectively. end 46 example] 47

26.7.1 Satisfying constraints 48

Whenever a constructed type or generic method is referenced, the supplied type arguments are checked 49 against the type parameter constraints declared on the generic type or method. For each where clause, the 50 type argument A that corresponds to the named type parameter is checked against each constraint as follows: 51

Page 440: C# Language Specification

C# LANGUAGE SPECIFICATION

420

• If the constraint is a class type, an interface type, or a type parameter, let C represent that constraint with 1 the supplied type arguments substituted for any type parameters that appear in the constraint. To satisfy 2 the constraint, it shall be the case that type A is convertible to type C by one of the following: 3

o An identity conversion (§13.1.1) 4

o An implicit reference conversion (§13.1.4) 5

o A boxing conversion (§13.1.5) 6

o An implicit conversion from a type parameter A to C (§26.7.4). 7

• If the constraint is the reference type constraint, the type A must satisfy one of the following: 8

o A is an interface type, class type, delegate type or array type. [Note: Note that System.ValueType 9 and System.Enum are reference types so satisfy this constraint. end note] 10

o A is a type parameter that is known to be a reference type (§26.7). 11

• If the constraint is the value type constraint, the type A must satisfy one of the following: 12

o A is a struct type or enum type. [Note: Note that System.ValueType and System.Enum are 13 reference types so do not satisfy this constraint. end note] 14

o A is a type parameter having the value type constraint (§26.7). 15

• If the constraint is the constructor constraint new(), the type argument A shall not be abstract and 16 shall have a public parameterless constructor. This is satisfied if one of the following is true: 17

o A is a value type, since all value types have a public default constructor (§11.1.2). 18

o A is a type parameter having the value type constraint (§26.7). 19

o A is a class that is not abstract, A contains an explicitly declared public constructor with no 20 parameters. 21

o A is not abstract and has a default constructor (§17.10.4). 22

A compile-time error occurs if one or more of a type parameter’s constraints are not satisfied by the given 23 type arguments. 24

Since type parameters are not inherited, constraints are never inherited either. [Example: In the code below, 25 D needs to specify the constraint on its type parameter T, so that T satisfies the constraint imposed by the 26 base class B<T>. In contrast, class E need not specify a constraint, because List<T> implements 27 IEnumerable for any T. 28

class B<T> where T: IEnumerable {…} 29

class D<T>: B<T> where T: IEnumerable {…} 30

class E<T>: B<List<T>> {…} 31

end example] 32

26.7.2 Member lookup on type parameters 33

The results of member lookup in a type given by a type parameter T depends on the constraints, if any, 34 specified for T. If T has no class-type, interface-type or type-parameter constraints, then member lookup on 35 T returns the same set of members as member lookup on object. Otherwise, the first stage of member 36 lookup (§14.3) considers all the members in the effective base class of T and all the members in each 37 interface in the effective interface set of T. After performing the first stage of member lookup for each of 38 theses types, the results are combined, and then hidden members are removed from the combined results. 39

Before the advent of generics, member lookup always returned either a set of members declared solely in 40 classes, or a set of members declared solely in interfaces and possibly the type object. Member lookup on 41 type parameters changes this somewhat. When a type parameter has both an effective base class other than 42 object and a non-empty effective interface set, member lookup can return a set of members, some of which 43

Page 441: C# Language Specification

Chapter 26 Generics

421

were declared in a class, and others of which were declared in an interface. The following additional rules 1 handle this case. 2

• As specified in §14.3, during member lookup, members declared in a class other than object hide 3 members declared in interfaces. 4

• During overload resolution of methods (§14.5.5.1) and indexers (§14.5.6.2), if any applicable member 5 was declared in a class other than object, all members declared in an interface are removed from the 6 set of considered members. 7

These rules only have effect when doing binding on a type parameter with both an effective base class other 8 than object and a non-empty effective interface set. [Note: Informally, members defined in a class 9 constraint are preferred over members in an interface constraint. end note] 10

26.7.3 Type parameters and boxing 11

When a struct type overrides a virtual method inherited from System.Object (such as Equals, 12 GetHashCode, or ToString), invocation of the virtual method through an instance of the struct type 13 doesn’t cause boxing to occur. This is true even when the struct is used as a type parameter and the 14 invocation occurs through an instance of the type parameter type. [Example: 15

using System; 16

struct Counter 17 { 18 int value; 19

public override string ToString() { 20 value++; 21 return value.ToString(); 22 } 23 } 24

class Program 25 { 26 static void Test<T>() where T: new() { 27 T x = new T(); 28 Console.WriteLine(x.ToString()); 29 Console.WriteLine(x.ToString()); 30 Console.WriteLine(x.ToString()); 31 } 32

static void Main() { 33 Test<Counter>(); 34 } 35 } 36

The output of the program is: 37

1 38 2 39 3 40

Although it is bad style for ToString to have side effects, the example demonstrates that no boxing 41 occurred for the three invocations of x.ToString(). end example] 42

Similarly, boxing never implicitly occurs when accessing a member on a constrained type parameter. 43 [Example: Suppose an interface ICounter contains a method Increment which can be used to modify a 44 value. If ICounter is used as a constraint, the implementation of the Increment method is called with a 45 reference to the variable that Increment was called on, never a boxed copy. This behaviour is different 46 than the non-generic case. When making a call to an interface implementation on a struct type the argument 47 is always boxed. 48

using System; 49

interface ICounter 50 { 51 void Increment(); 52 } 53

Page 442: C# Language Specification

C# LANGUAGE SPECIFICATION

422

struct Counter: ICounter 1 { 2 int value; 3

public override string ToString() { 4 return value.ToString(); 5 } 6

void ICounter.Increment() { 7 value++; 8 } 9 } 10

class Program 11 { 12 static void Test<T>() where T: ICounter, new() { 13 T x = new T(); 14 Console.WriteLine(x); 15 x.Increment(); // Modify x 16 Console.WriteLine(x); 17 ((ICounter)x).Increment(); // Modify boxed copy of x 18 Console.WriteLine(x); 19 } 20

static void Main() { 21 Test<Counter>(); 22 } 23 } 24

The first call to Increment modifies the value in the variable x. This is not equivalent to the second call to 25 Increment, which modifies the value in a boxed copy of x. Thus, the output of the program is: 26

0 27 1 28 1 29

end example] 30

26.7.4 Conversions involving type parameters 31

The conversions that are allowed on a type parameter T depend on the constraints specified for T and are 32 detailed in §13. 33

The conversion rules do not permit a direct explicit conversion from an unconstrained type parameter to an 34 arbitrary non-interface type, which might be surprising. The reason for this rule is to prevent confusion and 35 make the semantics of such conversions clear. [Example: Consider the following declaration: 36

class X<T> 37 { 38 public static long F(T t) { 39 return (long)t; // Error, explicit conversion not permitted 40 } 41 } 42

If the direct explicit conversion of t to long were permitted, one might easily expect that X<int>.F(7) 43 would return 7L. However, it would not, because the standard numeric conversions are only considered 44 when the types are known to be numeric at compile time. In order to make the semantics clear, the above 45 example should be written: 46

class X<T> 47 { 48 public static long F(T t) { 49 return (long)(object)t; // OK, conversions permitted 50 } 51 } 52

This code will now compile but executing X<int>.F(7) would then throw an exception at runtime, since a 53 boxed int cannot be converted directly to a long. 54

end example] 55

Page 443: C# Language Specification

Chapter 27 Iterators

423

27. Iterators 1

An iterator is a means of implementing a function member whose return type is an enumerator interface or 2 enumerable interface. The function member returns the ordered sequence of values as yielded by the iterator. 3

[Example: The following Stack<T> class implements its GetEnumerator method using an iterator. The 4 iterator enumerates the elements of the stack in top to bottom order. 5

using System.Collections.Generic; 6

public class Stack<T>: IEnumerable<T> 7 { 8 T[] items; 9 int count; 10

public void Push(T data) {…} 11

public T Pop() {…} 12

public IEnumerator<T> GetEnumerator() { 13 for (int i = count – 1; i >= 0; --i) { 14 yield return items[i]; 15 } 16 } 17 } 18

The type IEnumerator<T> is an enumerator interface which allows the GetEnumerator method to be 19 implemented using an iterator. The presence of the GetEnumerator method makes Stack<T> an 20 enumerable type, allowing instances of Stack<T> to be used in a foreach statement. The following 21 example pushes the values 0 through 9 onto an integer stack and then uses a foreach loop to display the 22 values in top to bottom order. 23

using System; 24

class Test 25 { 26 static void Main() { 27 Stack<int> s = new Stack<int>(); 28 for (int i = 0; i < 10; i++) s.Push(i); 29 foreach (int i in s) Console.Write("{0} ", i); 30 Console.WriteLine(); 31 } 32 } 33

The output of the example is: 34

9 8 7 6 5 4 3 2 1 0 35

end example] 36

27.1 Iterator blocks 37

An iterator block is a block (§15.2) that yields an ordered sequence of values. An iterator block is 38 distinguished from a normal statement block by the presence of one or more yield statements (§15.14). 39

• The yield return statement produces the next value of the iteration. 40

• The yield break statement indicates that the iteration is complete. 41

An iterator block can be used as a method-body (§17.5), operator-body (§17.9), or accessor-body (§17.6.2) 42 as long as the return type of the corresponding function member is one of the enumerator interfaces 43 (§27.1.1) or one of the enumerable interfaces (§27.1.2). 44

Page 444: C# Language Specification

C# LANGUAGE SPECIFICATION

424

Iterator blocks are not a distinct element in the C# grammar. They are restricted in several ways and have a 1 major effect on the semantics of a function member declaration; however, grammatically, they are just 2 blocks. 3

When a function member is implemented using an iterator block, it is a compile-time error for the formal 4 parameter list of the function member to specify any ref or out parameters. 5

It is a compile-time error for a return statement to appear in an iterator block (but yield return 6 statements are permitted). 7

It is a compile-time error for an iterator block to contain an unsafe context (§25.1). An iterator block always 8 defines a safe context, even when its declaration is nested in an unsafe context. 9

27.1.1 Enumerator interfaces 10

The enumerator interfaces are the non-generic interface System.Collections.IEnumerator and all 11 instantiations of the generic interface System.Collections.Generic.IEnumerator<T>. For the sake 12 of brevity, these interfaces are referenced in this specification as IEnumerator and IEnumerator<T>, 13 respectively. 14

27.1.2 Enumerable interfaces 15

The enumerable interfaces are the non-generic interface System.Collections.IEnumerable and all 16 instantiations of the generic interface System.Collections.Generic.IEnumerable<T>. For the sake 17 of brevity, these interfaces are referenced in this specification as IEnumerable and IEnumerable<T>, 18 respectively. 19

27.1.3 Yield type 20

An iterator block produces a sequence of values, all of the same type. This type is called the yield type of the 21 iterator block. 22

• The yield type of an iterator block used to implement a function member that returns IEnumerator or 23 IEnumerable is object. 24

• The yield type of an iterator block used to implement a function member that returns IEnumerator<T> 25 or IEnumerable<T> is T. 26

27.1.4 This access 27

Within an iterator block of an instance member of a class, the expression this is classified as a value. The 28 type of the value is the class within which the usage occurs, and the value is a reference to the object for 29 which the member was invoked. 30

Within an iterator block of an instance member of a struct, the expression this is classified as a variable. 31 The type of the variable is the struct within which the usage occurs. The variable represents a copy of the 32 struct for which the member was invoked. The this variable in an iterator block of an instance member of a 33 struct behaves exactly the same as a value parameter of the struct type. [Note: This is different than in a non-34 iterator function member body. end note] 35

27.2 Enumerator objects 36

When a function member returning an enumerator interface type is implemented using an iterator block, 37 invoking the function member does not immediately execute the code in the iterator block. Instead, an 38 enumerator object is created and returned. This object encapsulates the code specified in the iterator block, 39 and execution of the code in the iterator block occurs when the enumerator object’s MoveNext method is 40 invoked. An enumerator object has the following characteristics: 41

• It implements IEnumerator and IEnumerator<T>, where T is the yield type of the iterator block. 42

• It implements System.IDisposable. 43

Page 445: C# Language Specification

Chapter 27 Iterators

425

• It is initialized with a copy of the argument values (if any) and instance value passed to the function 1 member. 2

• It has four potential states, before, running, suspended, and after, and is initially in the before state. 3

An enumerator object is typically an instance of a compiler-generated enumerator class that encapsulates the 4 code in the iterator block and implements the enumerator interfaces, but other methods of implementation 5 are possible. If an enumerator type is generated by the compiler, that class shall be nested, directly or 6 indirectly, in the type containing the function member, it shall have private accessibility, and it shall have a 7 name reserved for compiler use (§9.4.2). 8

An enumerator object can implement more interfaces than those specified above. 9

The following subclauses describe the exact behavior of the MoveNext, Current, and Dispose members 10 of the IEnumerable and IEnumerable<T> interface implementations provided by an enumerator object. 11

Enumerator objects do not support the IEnumerator.Reset method. Invoking this method causes a 12 System.NotSupportedException to be thrown. 13

27.2.1 The MoveNext method 14

The MoveNext method of an enumerator object encapsulates the code of an iterator block. Invoking the 15 MoveNext method executes code in the iterator block and sets the Current property of the enumerator 16 object as appropriate. The precise action performed by MoveNext depends on the state of the enumerator 17 object when MoveNext is invoked: 18

• If the state of the enumerator object is before, invoking MoveNext: 19

o Changes the state to running. 20

o Initializes the parameters (including this) of the iterator block to the argument values and instance 21 value saved when the enumerator object was intialized. 22

o Executes the iterator block from the beginning until execution is interrupted (as described below). 23

• If the state of the enumerator object is running, the result of invoking MoveNext is unspecified. 24

• If the state of the enumerator object is suspended, invoking MoveNext: 25

o Changes the state to running. 26

o Restores the values of all local variables and parameters (including this) to the values saved when 27 execution of the iterator block was last suspended. (Note that the contents of any objects referenced 28 by these variables might have changed since the previous call to MoveNext.) 29

o Resumes execution of the iterator block immediately following the yield return statement that 30 caused the suspension of execution, and continues until execution is interrupted (as described 31 below). 32

• If the state of the enumerator object is after, invoking MoveNext returns false. 33

When MoveNext executes the iterator block, execution can be interrupted in four ways: By a yield 34 return statement, by a yield break statement, by encountering the end of the iterator block, or by an 35 exception being thrown and propagated out of the iterator block. 36

• When a yield return statement is encountered (§15.14): 37

o The expression given in the statement is evaluated, implicitly converted to the yield type, and 38 assigned to the Current property of the enumerator object. 39

o Execution of the iterator body is suspended. The values of all local variables and parameters 40 (including this) are saved, as is the location of this yield return statement. If the yield 41 return statement is within one or more try blocks, the associated finally blocks are not 42 executed at this time. 43

Page 446: C# Language Specification

C# LANGUAGE SPECIFICATION

426

o The state of the enumerator object is changed to suspended. 1

o The MoveNext method returns true to its caller, indicating that the iteration successfully advanced 2 to the next value. 3

• When a yield break statement is encountered (§15.14): 4

o If the yield break statement is within one or more try blocks, the associated finally blocks are 5 executed. 6

o The state of the enumerator object is changed to after. 7

o The MoveNext method returns false to its caller, indicating that the iteration is complete. 8

• When the end of the iterator body is encountered: 9

o The state of the enumerator object is changed to after. 10

o The MoveNext method returns false to its caller, indicating that the iteration is complete. 11

• When an exception is thrown and propagated out of the iterator block: 12

o Appropriate finally blocks in the iterator body will have been executed by the exception 13 propagation. 14

o The state of the enumerator object is changed to after. 15

o The exception propagation continues to the caller of the MoveNext method. 16

27.2.2 The Current property 17

An enumerator object’s Current property is affected by yield return statements in the iterator block. 18

When an enumerator object is in the suspended state, the value of Current is the value set by the previous 19 call to MoveNext. When an enumerator object is in the before, running, or after states, the result of 20 accessing Current is unspecified. 21

For an iterator block with a yield type other than object, the result of accessing Current through the 22 enumerator object’s IEnumerable implementation corresponds to accessing Current through the 23 enumerator object’s IEnumerator<T> implementation and casting the result to object. 24

27.2.3 The Dispose method 25

The Dispose method is used to clean up the iteration by bringing the enumerator object to the after state. 26

• If the state of the enumerator object is before, invoking Dispose changes the state to after. 27

• If the state of the enumerator object is running, the result of invoking Dispose is unspecified. 28

• If the state of the enumerator object is suspended, invoking Dispose: 29

o Changes the state to running. 30

o Executes any finally blocks as if the last executed yield return statement were a yield break 31 statement. If this causes an exception to be thrown and propagated out of the iterator body, the state 32 of the enumerator object is set to after and the exception is propagated to the caller of the Dispose 33 method. 34

o Changes the state to after. 35

• If the state of the enumerator object is after, invoking Dispose has no affect. 36

27.3 Enumerable objects 37

When a function member returning an enumerable interface type is implemented using an iterator block, 38 invoking the function member does not immediately execute the code in the iterator block. Instead, an 39 enumerable object is created and returned. The enumerable object’s GetEnumerator method returns an 40

Page 447: C# Language Specification

Chapter 27 Iterators

427

enumerator object that encapsulates the code specified in the iterator block, and execution of the code in the 1 iterator block occurs when the enumerator object’s MoveNext method is invoked. An enumerable object has 2 the following characteristics: 3

• It implements IEnumerable and IEnumerable<T>, where T is the yield type of the iterator block. 4

• It is initialized with a copy of the argument values (if any) and instance value passed to the function 5 member. 6

An enumerable object is typically an instance of a compiler-generated enumerable class that encapsulates the 7 code in the iterator block and implements the enumerable interfaces, but other methods of implementation 8 are possible. If an enumerable class is generated by the compiler, that class shall be nested, directly or 9 indirectly, in the class containing the function member, it shall have private accessibility, and it shall have a 10 name reserved for compiler use (§9.4.2). 11

[Note: An enumerable object can implement more interfaces than those specified above. In particular, an 12 enumerable object can also implement IEnumerator and IEnumerator<T>, enabling it to serve as both 13 an enumerable and an enumerator. In that type of implementation, the first time an enumerable object’s 14 GetEnumerator method is invoked, the enumerable object itself is returned. Subsequent invocations of the 15 enumerable object’s GetEnumerator, if any, return a copy of the enumerable object. Thus, each returned 16 enumerator has its own state, and changes in one enumerator will not affect another. end note] 17

27.3.1 The GetEnumerator method 18

An enumerable object provides an implementation of the GetEnumerator methods of the IEnumerable 19 and IEnumerable<T> interfaces. The two GetEnumerator methods share a common implementation that 20 acquires and returns an available enumerator object. The enumerator object is initialized with the argument 21 values and instance value saved when the enumerable object was initialized, but otherwise the enumerator 22 object functions as described in §27.2. 23

27.4 Implementation example 24

[Note: This section describes a possible implementation of iterators in terms of standard C# constructs. The 25 implementation described here is by no means a mandated implementation or the only one possible. 26

The following Stack<T> class implements its GetEnumerator method using an iterator. The iterator 27 enumerates the elements of the stack in top to bottom order. 28

using System; 29 using System.Collections; 30 using System.Collections.Generic; 31

class Stack<T>: IEnumerable<T> 32 { 33 T[] items; 34 int count; 35

public void Push(T item) { 36 if (items == null) { 37 items = new T[4]; 38 } 39 else if (items.Length == count) { 40 T[] newItems = new T[count * 2]; 41 Array.Copy(items, 0, newItems, 0, count); 42 items = newItems; 43 } 44 items[count++] = item; 45 } 46

public T Pop() { 47 T result = items[--count]; 48 items[count] = default(T); 49 return result; 50 } 51

Page 448: C# Language Specification

C# LANGUAGE SPECIFICATION

428

public IEnumerator<T> GetEnumerator() { 1 for (int i = count - 1; i >= 0; --i) yield return items[i]; 2 } 3 } 4

The GetEnumerator method can be translated into an instantiation of a compiler-generated enumerator 5 class that encapsulates the code in the iterator block, as shown in the following. 6

class Stack<T>: IEnumerable<T> 7 { 8 … 9

public IEnumerator<T> GetEnumerator() { 10 return new __Enumerator1(this); 11 } 12

class __Enumerator1: IEnumerator<T>, IEnumerator 13 { 14 int __state; 15 T __current; 16 Stack<T> __this; 17 int i; 18

public __Enumerator1(Stack<T> __this) { 19 this.__this = __this; 20 } 21

public T Current { 22 get { return __current; } 23 } 24

object IEnumerator.Current { 25 get { return __current; } 26 } 27

public bool MoveNext() { 28 switch (__state) { 29 case 1: goto __state1; 30 case 2: goto __state2; 31 } 32 i = __this.count - 1; 33 __loop: 34 if (i < 0) goto __state2; 35 __current = __this.items[i]; 36 __state = 1; 37 return true; 38 __state1: 39 --i; 40 goto __loop; 41 __state2: 42 __state = 2; 43 return false; 44 } 45

public void Dispose() { 46 __state = 2; 47 } 48

void IEnumerator.Reset() { 49 throw new NotSupportedException(); 50 } 51 } 52 } 53

In the preceding translation, the code in the iterator block is turned into a state machine and placed in the 54 MoveNext method of the enumerator class. Furthermore, the local variable i is turned into a field in the 55 enumerator object so it can continue to exist across invocations of MoveNext. 56

The following example prints a simple multiplication table of the integers 1 through 10. The FromTo 57 method in the example returns an enumerable object and is implemented using an iterator. 58

using System; 59 using System.Collections.Generic; 60

Page 449: C# Language Specification

Chapter 27 Iterators

429

class Test 1 { 2 static IEnumerable<int> FromTo(int from, int to) { 3 while (from <= to) yield return from++; 4 } 5

static void Main() { 6 IEnumerable<int> e = FromTo(1, 10); 7 foreach (int x in e) { 8 foreach (int y in e) { 9 Console.Write("{0,3} ", x * y); 10 } 11 Console.WriteLine(); 12 } 13 } 14 } 15

The FromTo method can be translated into an instantiation of a compiler-generated enumerable class that 16 encapsulates the code in the iterator block, as shown in the following. 17

using System; 18 using System.Threading; 19 using System.Collections; 20 using System.Collections.Generic; 21

class Test 22 { 23 … 24

static IEnumerable<int> FromTo(int from, int to) { 25 return new __Enumerable1(from, to); 26 } 27

class __Enumerable1: 28 IEnumerable<int>, IEnumerable, 29 IEnumerator<int>, IEnumerator 30 { 31 int __state; 32 int __current; 33 int __from; 34 int from; 35 int to; 36 int i; 37

public __Enumerable1(int __from, int to) { 38 this.__from = __from; 39 this.to = to; 40 } 41

public IEnumerator<int> GetEnumerator() { 42 __Enumerable1 result = this; 43 if (Interlocked.CompareExchange(ref __state, 1, 0) != 0) { 44 result = new __Enumerable1(__from, to); 45 result.__state = 1; 46 } 47 result.from = result.__from; 48 return result; 49 } 50

IEnumerator IEnumerable.GetEnumerator() { 51 return (IEnumerator)GetEnumerator(); 52 } 53

public int Current { 54 get { return __current; } 55 } 56

object IEnumerator.Current { 57 get { return __current; } 58 } 59

Page 450: C# Language Specification

C# LANGUAGE SPECIFICATION

430

public bool MoveNext() { 1 switch (__state) { 2 case 1: 3 if (from > to) goto case 2; 4 __current = from++; 5 __state = 1; 6 return true; 7 case 2: 8 __state = 2; 9 return false; 10 default: 11 throw new InvalidOperationException(); 12 } 13 } 14

public void Dispose() { 15 __state = 2; 16 } 17

void IEnumerator.Reset() { 18 throw new NotSupportedException(); 19 } 20 } 21 } 22

The enumerable class implements both the enumerable interfaces and the enumerator interfaces, enabling it 23 to serve as both an enumerable and an enumerator. The first time the GetEnumerator method is invoked, 24 the enumerable object itself is returned. Subsequent invocations of the enumerable object’s 25 GetEnumerator, if any, return a copy of the enumerable object. Thus, each returned enumerator has its 26 own state and changes in one enumerator will not affect another. The Interlocked.CompareExchange 27 method is used to ensure thread-safe operation. 28

The from and to parameters are turned into fields in the enumerable class. Because from is modified in the 29 iterator block, an additional __from field is introduced to hold the initial value given to from in each 30 enumerator. 31

The MoveNext method throws an InvalidOperationException if it is called when __state is 0. This 32 protects against use of the enumerable object as an enumerator object without first calling GetEnumerator. 33

The following example shows a simple tree class. The Tree<T> class implements its GetEnumerator 34 method using an iterator. The iterator enumerates the elements of the tree in infix order. 35

using System; 36 using System.Collections.Generic; 37

class Tree<T>: IEnumerable<T> 38 { 39 T value; 40 Tree<T> left; 41 Tree<T> right; 42

public Tree(T value, Tree<T> left, Tree<T> right) { 43 this.value = value; 44 this.left = left; 45 this.right = right; 46 } 47

public IEnumerator<T> GetEnumerator() { 48 if (left != null) foreach (T x in left) yield x; 49 yield value; 50 if (right != null) foreach (T x in right) yield x; 51 } 52 } 53

Page 451: C# Language Specification

Chapter 27 Iterators

431

class Program 1 { 2 static Tree<T> MakeTree<T>(T[] items, int left, int right) { 3 if (left > right) return null; 4 int i = (left + right) / 2; 5 return new Tree<T>(items[i], 6 MakeTree(items, left, i - 1), 7 MakeTree(items, i + 1, right)); 8 } 9

static Tree<T> MakeTree<T>(params T[] items) { 10 return MakeTree(items, 0, items.Length - 1); 11 } 12

// The output of the program is: 13 // 1 2 3 4 5 6 7 8 9 14 // Mon Tue Wed Thu Fri Sat Sun 15 // 16

static void Main() { 17 Tree<int> ints = MakeTree(1, 2, 3, 4, 5, 6, 7, 8, 9); 18 foreach (int i in ints) Console.Write("{0} ", i); 19 Console.WriteLine(); 20

Tree<string> strings = MakeTree("Mon", "Tue", "Wed", "Thu", 21 "Fri", "Sat", "Sun"); 22 foreach (string s in strings) Console.Write("{0} ", s); 23 Console.WriteLine(); 24 } 25 } 26

The GetEnumerator method can be translated into an instantiation of a compiler-generated enumerator 27 class that encapsulates the code in the iterator block, as shown in the following. 28

class Tree<T>: IEnumerable<T> 29 { 30 … 31 public IEnumerator<T> GetEnumerator() { 32 return new __Enumerator1(this); 33 } 34

sealed class __Enumerator1 : IEnumerator<T>, IEnumerator 35 { 36 Node<T> __this; 37 IEnumerator<T> __left, __right; 38 int __state; 39 T __current; 40

public __Enumerator1(Node<T> __this) { this.__this = __this; } 41

public T Current { get { return __current; } } 42

public bool MoveNext() { 43 try { 44 switch (__state) { 45

case 0: 46 __state = -1; 47 if (__this.left == null) goto __yield_value; 48 __left = __this.left.GetEnumerator(); 49 goto case 1; 50

case 1: 51 __state = -2; 52 if (!__left.MoveNext()) goto __left_dispose; 53 __current = __left.Current; 54 __state = 1; 55 return true; 56

__left_dispose: 57 __state = -1; 58 __left.Dispose(); 59

Page 452: C# Language Specification

C# LANGUAGE SPECIFICATION

432

__yield_value: 1 __current = __this.value; 2 __state = 2; 3 return true; 4

case 2: 5 __state = -1; 6 if (__this.right == null) goto __end; 7 __right = __this.right.GetEnumerator(); 8 goto case 3; 9

case 3: 10 __state = -3; 11 if (!__right.MoveNext()) goto __right_dispose; 12 __current = __right.Current; 13 __state = 3; 14 return true; 15

__right_dispose: 16 __state = -1; 17 __right.Dispose(); 18

__end: 19 __state = 4; 20 break; 21

} 22 } finally { 23 if (__state < 0) Dispose(); 24 } 25 return false; 26 } 27

public void Dispose() { 28 try { 29 switch (__state) { 30

case 1: 31 case -2: 32 __left.Dispose(); 33 break; 34

case 3: 35 case -3: 36 __right.Dispose(); 37 break; 38

} 39 } finally { 40 __state = 4; 41 } 42 } 43

object IEnumerator.Current { get { return Current; } } 44

void IEnumerator.Reset() { 45 throw new NotSupportedException(); 46 } 47 } 48 } 49

The compiler generated temporaries used in the foreach statements are lifted into the __left and __right 50 fields of the enumerator object. The __state field of the enumerator object is carefully updated so that the 51 correct Dispose() method will be called correctly if an exception is thrown. Note that it is not possible to 52 write the translated code with simple foreach statements. end note] 53

Page 453: C# Language Specification

Annex A Grammar

433

Annex A. Grammar 1

This clause is informative. 2

This annex contains summaries of the lexical and syntactic grammars found in the main document, and of 3 the grammar extensions for unsafe code. Grammar productions appear here in the same order that they 4 appear in the main document. 5

A.1 Lexical grammar 6

input:: 7 input-sectionopt 8

input-section:: 9 input-section-part 10 input-section input-section-part 11

input-section-part:: 12 input-elementsopt new-line 13 pp-directive 14

input-elements:: 15 input-element 16 input-elements input-element 17

input-element:: 18 whitespace 19 comment 20 token 21

A.1.1 Line terminators 22

new-line:: 23 Carriage return character (U+000D) 24 Line feed character (U+000A) 25 Carriage return character (U+000D) followed by line feed character (U+000A) 26 Next line character (U+0085) 27 Line separator character (U+2028) 28 Paragraph separator character (U+2029) 29

A.1.2 White space 30

whitespace:: 31 whitespace-characters 32

whitespace-characters:: 33 whitespace-character 34 whitespace-characters whitespace-character 35

whitespace-character:: 36 Any character with Unicode class Zs 37 Horizontal tab character (U+0009) 38 Vertical tab character (U+000B) 39 Form feed character (U+000C) 40

Page 454: C# Language Specification

C# LANGUAGE SPECIFICATION

434

A.1.3 Comments 1

comment:: 2 single-line-comment 3 delimited-comment 4

single-line-comment:: 5 // input-charactersopt 6

input-characters:: 7 input-character 8 input-characters input-character 9

input-character:: 10 Any Unicode character except a new-line-character 11

new-line-character:: 12 Carriage return character (U+000D) 13 Line feed character (U+000A) 14 Next line character (U+0085) 15 Line separator character (U+2028) 16 Paragraph separator character (U+2029) 17

delimited-comment:: 18 /* delimited-comment-textopt asterisks / 19

delimited-comment-text:: 20 delimited-comment-section 21 delimited-comment-text delimited-comment-section 22

delimited-comment-section:: 23 not-asterisk 24 asterisks not-slash 25

asterisks:: 26 * 27 asterisks * 28

not-asterisk:: 29 Any Unicode character except * 30

not-slash:: 31 Any Unicode character except / 32

A.1.4 Tokens 33

token:: 34 identifier 35 keyword 36 integer-literal 37 real-literal 38 character-literal 39 string-literal 40 operator-or-punctuator 41

A.1.5 Unicode character escape sequences 42

unicode-character-escape-sequence:: 43 \u hex-digit hex-digit hex-digit hex-digit 44 \U hex-digit hex-digit hex-digit hex-digit hex-digit hex-digit hex-digit hex-digit 45

Page 455: C# Language Specification

Annex A Grammar

435

A.1.6 Identifiers 1

identifier:: 2 available-identifier 3 @ identifier-or-keyword 4

available-identifier:: 5 An identifier-or-keyword that is not a keyword 6

identifier-or-keyword:: 7 identifier-start-character identifier-part-charactersopt 8

identifier-start-character:: 9 letter-character 10 _ (the underscore character) 11

identifier-part-characters:: 12 identifier-part-character 13 identifier-part-characters identifier-part-character 14

identifier-part-character:: 15 letter-character 16 decimal-digit-character 17 connecting-character 18 combining-character 19 formatting-character 20

letter-character:: 21 A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl 22 A unicode-character-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or 23 Nl 24

combining-character:: 25 A Unicode character of classes Mn or Mc 26 A unicode-character-escape-sequence representing a character of classes Mn or Mc 27

decimal-digit-character:: 28 A Unicode character of the class Nd 29 A unicode-character-escape-sequence representing a character of the class Nd 30

connecting-character:: 31 A Unicode character of the class Pc 32 A unicode-character-escape-sequence representing a character of the class Pc 33

formatting-character:: 34 A Unicode character of the class Cf 35 A unicode-character-escape-sequence representing a character of the class Cf 36

Page 456: C# Language Specification

C# LANGUAGE SPECIFICATION

436

A.1.7 Keywords 1

keyword:: one of 2 abstract as base bool break 3 byte case catch char checked 4 class const continue decimal default 5 delegate do double else enum 6 event explicit extern false finally 7 fixed float for foreach goto 8 if implicit in int interface 9 internal is lock long namespace 10 new null object operator out 11 override params private protected public 12 readonly ref return sbyte sealed 13 short sizeof stackalloc static string 14 struct switch this throw true 15 try typeof uint ulong unchecked 16 unsafe ushort using virtual void 17 volatile while 18

A.1.8 Literals 19

literal:: 20 boolean-literal 21 integer-literal 22 real-literal 23 character-literal 24 string-literal 25 null-literal 26

boolean-literal:: 27 true 28 false 29

integer-literal:: 30 decimal-integer-literal 31 hexadecimal-integer-literal 32

decimal-integer-literal:: 33 decimal-digits integer-type-suffixopt 34

decimal-digits:: 35 decimal-digit 36 decimal-digits decimal-digit 37

decimal-digit:: one of 38 0 1 2 3 4 5 6 7 8 9 39

integer-type-suffix:: one of 40 U u L l UL Ul uL ul LU Lu lU lu 41

hexadecimal-integer-literal:: 42 0x hex-digits integer-type-suffixopt 43 0X hex-digits integer-type-suffixopt 44

hex-digits:: 45 hex-digit 46 hex-digits hex-digit 47

hex-digit:: one of 48 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f 49

Page 457: C# Language Specification

Annex A Grammar

437

real-literal:: 1 decimal-digits . decimal-digits exponent-partopt real-type-suffixopt 2 . decimal-digits exponent-partopt real-type-suffixopt 3 decimal-digits exponent-part real-type-suffixopt 4 decimal-digits real-type-suffix 5

exponent-part:: 6 e signopt decimal-digits 7 E signopt decimal-digits 8

sign:: one of 9 + - 10

real-type-suffix:: one of 11 F f D d M m 12

character-literal:: 13 ' character ' 14

character:: 15 single-character 16 simple-escape-sequence 17 hexadecimal-escape-sequence 18 unicode-character-escape-sequence 19

single-character:: 20 Any character except ' (U+0027), \ (U+005C), and new-line-character 21

simple-escape-sequence:: one of 22 \' \" \\ \0 \a \b \f \n \r \t \v 23

hexadecimal-escape-sequence:: 24 \x hex-digit hex-digitopt hex-digitopt hex-digitopt 25

string-literal:: 26 regular-string-literal 27 verbatim-string-literal 28

regular-string-literal:: 29 " regular-string-literal-charactersopt " 30

regular-string-literal-characters:: 31 regular-string-literal-character 32 regular-string-literal-characters regular-string-literal-character 33

regular-string-literal-character:: 34 single-regular-string-literal-character 35 simple-escape-sequence 36 hexadecimal-escape-sequence 37 unicode-character-escape-sequence 38

single-regular-string-literal-character:: 39 Any character except " (U+0022), \ (U+005C), and new-line-character 40

verbatim-string-literal:: 41 @" verbatim -string-literal-charactersopt " 42

verbatim-string-literal-characters:: 43 verbatim-string-literal-character 44 verbatim-string-literal-characters verbatim-string-literal-character 45

verbatim-string-literal-character:: 46 single-verbatim-string-literal-character 47 quote-escape-sequence 48

Page 458: C# Language Specification

C# LANGUAGE SPECIFICATION

438

single-verbatim-string-literal-character:: 1 any character except " 2

quote-escape-sequence:: 3 "" 4

null-literal:: 5 null 6

A.1.9 Operators and punctuators 7

operator-or-punctuator: one of 8 { } [ ] ( ) . , : ; 9 + - * / % & | ^ ! ~ 10 = < > ? ++ -- && || == -> 11 != <= >= += -= *= /= %= &= |= 12 ^= << <<= :: 13

right-shift: 14 > > 15

right-shift-assignment: 16 > >= 17

A.1.10 Pre-processing directives 18

pp-directive:: 19 pp-declaration 20 pp-conditional 21 pp-line 22 pp-diagnostic 23 pp-region 24 pp-pragma 25

pp-new-line:: 26 whitespaceopt single-line-commentopt new-line 27

conditional-symbol:: 28 Any identifier-or-keyword except true or false 29

pp-expression:: 30 whitespaceopt pp-or-expression whitespaceopt 31

pp-or-expression:: 32 pp-and-expression 33 pp-or-expression whitespaceopt || whitespaceopt pp-and-expression 34

pp-and-expression:: 35 pp-equality-expression 36 pp-and-expression whitespaceopt && whitespaceopt pp-equality-expression 37

pp-equality-expression:: 38 pp-unary-expression 39 pp-equality-expression whitespaceopt == whitespaceopt pp-unary-expression 40 pp-equality-expression whitespaceopt != whitespaceopt pp-unary-expression 41

pp-unary-expression:: 42 pp-primary-expression 43 ! whitespaceopt pp-unary-expression 44

Page 459: C# Language Specification

Annex A Grammar

439

pp-primary-expression:: 1 true 2 false 3 conditional-symbol 4 ( whitespaceopt pp-expression whitespaceopt ) 5

pp-declaration:: 6 whitespaceopt # whitespaceopt define whitespace conditional-symbol pp-new-line 7 whitespaceopt # whitespaceopt undef whitespace conditional-symbol pp-new-line 8

pp-conditional:: 9 pp-if-section pp-elif-sectionsopt pp-else-sectionopt pp-endif 10

pp-if-section:: 11 whitespaceopt # whitespaceopt if whitespace pp-expression pp-new-line conditional-12 sectionopt 13

pp-elif-sections:: 14 pp-elif-section 15 pp-elif-sections pp-elif-section 16

pp-elif-section:: 17 whitespaceopt # whitespaceopt elif whitespace pp-expression pp-new-line conditional-18 sectionopt 19

pp-else-section:: 20 whitespaceopt # whitespaceopt else pp-new-line conditional-sectionopt 21

pp-endif:: 22 whitespaceopt # whitespaceopt endif pp-new-line 23

conditional-section:: 24 input-section 25 skipped-section 26

skipped-section:: 27 skipped-section-part 28 skipped-section skipped-section-part 29

skipped-section-part:: 30 skipped-charactersopt new-line 31 pp-directive 32

skipped-characters:: 33 whitespaceopt not-number-sign input-charactersopt 34

not-number-sign:: 35 Any input-character except # 36

pp-line:: 37 whitespaceopt # whitespaceopt line whitespaceopt line-indicator pp-new-line 38

line-indicator:: 39 decimal-digits whitespace file-name 40 decimal-digits 41 identifier-or-keyword 42

file-name:: 43 " file-name-characters " 44

file-name-characters:: 45 file-name-character 46 file-name-characters file-name-character 47

Page 460: C# Language Specification

C# LANGUAGE SPECIFICATION

440

file-name-character:: 1 Any character except " (U+0022), and new-line-character 2

pp-diagnostic:: 3 whitespaceopt # whitespaceopt error whitespaceopt pp-message 4 whitespaceopt # whitespaceopt warning whitespaceopt pp-message 5

pp-message:: 6 input-charactersopt new-line 7

pp-region:: 8 pp-start-region conditional-sectionopt pp-end-region 9

pp-start-region:: 10 whitespaceopt # whitespaceopt region whitespaceopt pp-message 11

pp-end-region:: 12 whitespaceopt # whitespaceopt endregion whitespaceopt pp-message 13

pp-pragma: 14 whitespaceopt # whitespaceopt pragma pp-pragma-text 15

pp-pragma-text: 16 new-line 17 whitespace input-charactersopt new-line 18

A.2 Syntactic grammar 19

A.2.1 Basic concepts 20

compilation-unit: 21 extern-alias-directivesopt using-directivesopt global-attributesopt 22 namespace-member-declarationsopt 23

namespace-name: 24 namespace-or-type-name 25

type-name: 26 namespace-or-type-name 27

namespace-or-type-name: 28 identifier type-argument-listopt 29 namespace-or-type-name . identifier type-argument-listopt 30 qualified-alias-member 31

A.2.2 Types 32

type: 33 value-type 34 reference-type 35 type-parameter 36

value-type: 37 struct-type 38 enum-type 39

struct-type: 40 type-name 41 simple-type 42

simple-type: 43 numeric-type 44 bool 45

Page 461: C# Language Specification

Annex A Grammar

441

numeric-type: 1 integral-type 2 floating-point-type 3 decimal 4

integral-type: 5 sbyte 6 byte 7 short 8 ushort 9 int 10 uint 11 long 12 ulong 13 char 14

floating-point-type: 15 float 16 double 17

enum-type: 18 type-name 19

reference-type: 20 class-type 21 interface-type 22 array-type 23 delegate-type 24

class-type: 25 type-name 26 object 27 string 28

interface-type: 29 type-name 30

array-type: 31 non-array-type rank-specifiers 32

non-array-type: 33 type 34

rank-specifiers: 35 rank-specifier 36 rank-specifiers rank-specifier 37

rank-specifier: 38 [ dim-separatorsopt ] 39

dim-separators: 40 , 41 dim-separators , 42

delegate-type: 43 type-name 44

A.2.3 Variables 45

variable-reference: 46 expression 47

Page 462: C# Language Specification

C# LANGUAGE SPECIFICATION

442

A.2.4 Expressions 1

argument-list: 2 argument 3 argument-list , argument 4

argument: 5 expression 6 ref variable-reference 7 out variable-reference 8

primary-expression: 9 array-creation-expression 10 primary-no-array-creation-expression 11

primary-no-array-creation-expression: 12 literal 13 simple-name 14 parenthesized-expression 15 member-access 16 invocation-expression 17 element-access 18 this-access 19 base-access 20 post-increment-expression 21 post-decrement-expression 22 object-creation-expression 23 delegate-creation-expression 24 typeof-expression 25 checked-expression 26 unchecked-expression 27 default-value-expression 28 anonymous-method-expression 29

simple-name: 30 identifier type-argument-listopt 31

parenthesized-expression: 32 ( expression ) 33

member-access: 34 primary-expression . identifier type-argument-listopt 35 predefined-type . identifier type-argument-listopt 36 qualified-alias-member . identifier type-argument-listopt 37

predefined-type: one of 38 bool byte char decimal double float int long 39 object sbyte short string uint ulong ushort 40

invocation-expression: 41 primary-expression ( argument-listopt ) 42

element-access: 43 primary-no-array-creation-expression [ expression-list ] 44

expression-list: 45 expression 46 expression-list , expression 47

this-access: 48 this 49

Page 463: C# Language Specification

Annex A Grammar

443

base-access: 1 base . identifier type-argument-listopt 2 base [ expression-list ] 3

post-increment-expression: 4 primary-expression ++ 5

post-decrement-expression: 6 primary-expression -- 7

object-creation-expression: 8 new type ( argument-listopt ) 9

array-creation-expression: 10 new non-array-type [ expression-list ] rank-specifiersopt array-initializeropt 11 new array-type array-initializer 12

delegate-creation-expression: 13 new delegate-type ( expression ) 14

typeof-expression: 15 typeof ( type ) 16 typeof ( void ) 17

checked-expression: 18 checked ( expression ) 19

unchecked-expression: 20 unchecked ( expression ) 21

default-value-expression: 22 default ( primary-expression ) 23

anonymous-method-expression: 24 delegate anonymous-method-signatureopt block 25

anonymous-method-signature: 26 ( anonymous-method-parameter-listopt ) 27

anonymous-method-parameter-list: 28 anonymous-method-parameter 29 anonymous-method-parameter-list , anonymous-method-parameter 30

anonymous-method-parameter: 31 parameter-modifieropt type identifier 32

unary-expression: 33 primary-expression 34 + unary-expression 35 - unary-expression 36 ! unary-expression 37 ~ unary-expression 38 pre-increment-expression 39 pre-decrement-expression 40 cast-expression 41

pre-increment-expression: 42 ++ unary-expression 43

pre-decrement-expression: 44 -- unary-expression 45

cast-expression: 46 ( type ) unary-expression 47

Page 464: C# Language Specification

C# LANGUAGE SPECIFICATION

444

multiplicative-expression: 1 unary-expression 2 multiplicative-expression * unary-expression 3 multiplicative-expression / unary-expression 4 multiplicative-expression % unary-expression 5

additive-expression: 6 multiplicative-expression 7 additive-expression + multiplicative-expression 8 additive-expression – multiplicative-expression 9

shift-expression: 10 additive-expression 11 shift-expression << additive-expression 12 shift-expression right-shift additive-expression 13

relational-expression: 14 shift-expression 15 relational-expression < shift-expression 16 relational-expression > shift-expression 17 relational-expression <= shift-expression 18 relational-expression >= shift-expression 19 relational-expression is type 20 relational-expression as type 21

equality-expression: 22 relational-expression 23 equality-expression == relational-expression 24 equality-expression != relational-expression 25

and-expression: 26 equality-expression 27 and-expression & equality-expression 28

exclusive-or-expression: 29 and-expression 30 exclusive-or-expression ^ and-expression 31

inclusive-or-expression: 32 exclusive-or-expression 33 inclusive-or-expression | exclusive-or-expression 34

conditional-and-expression: 35 inclusive-or-expression 36 conditional-and-expression && inclusive-or-expression 37

conditional-or-expression: 38 conditional-and-expression 39 conditional-or-expression || conditional-and-expression 40

conditional-expression: 41 conditional-or-expression 42 conditional-or-expression ? expression : expression 43

assignment: 44 unary-expression assignment-operator expression 45

assignment-operator: one of 46 = += -= *= /= %= &= |= ^= <<= right-shift-assignment 47

Page 465: C# Language Specification

Annex A Grammar

445

expression: 1 conditional-expression 2 assignment 3

constant-expression: 4 expression 5

boolean-expression: 6 expression 7

A.2.5 Statements 8

statement: 9 labeled-statement 10 declaration-statement 11 embedded-statement 12

embedded-statement: 13 block 14 empty-statement 15 expression-statement 16 selection-statement 17 iteration-statement 18 jump-statement 19 try-statement 20 checked-statement 21 unchecked-statement 22 lock-statement 23 using-statement 24 yield-statement 25

block: 26 { statement-listopt } 27

statement-list: 28 statement 29 statement-list statement 30

empty-statement: 31 ; 32

labeled-statement: 33 identifier : statement 34

declaration-statement: 35 local-variable-declaration ; 36 local-constant-declaration ; 37

local-variable-declaration: 38 type local-variable-declarators 39

local-variable-declarators: 40 local-variable-declarator 41 local-variable-declarators , local-variable-declarator 42

local-variable-declarator: 43 identifier 44 identifier = local-variable-initializer 45

local-variable-initializer: 46 expression 47 array-initializer 48

Page 466: C# Language Specification

C# LANGUAGE SPECIFICATION

446

local-constant-declaration: 1 const type constant-declarators 2

constant-declarators: 3 constant-declarator 4 constant-declarators , constant-declarator 5

constant-declarator: 6 identifier = constant-expression 7

expression-statement: 8 statement-expression ; 9

statement-expression: 10 invocation-expression 11 object-creation-expression 12 assignment 13 post-increment-expression 14 post-decrement-expression 15 pre-increment-expression 16 pre-decrement-expression 17

selection-statement: 18 if-statement 19 switch-statement 20

if-statement: 21 if ( boolean-expression ) embedded-statement 22 if ( boolean-expression ) embedded-statement else embedded-statement 23

boolean-expression: 24 expression 25

switch-statement: 26 switch ( expression ) switch-block 27

switch-block: 28 { switch-sectionsopt } 29

switch-sections: 30 switch-section 31 switch-sections switch-section 32

switch-section: 33 switch-labels statement-list 34

switch-labels: 35 switch-label 36 switch-labels switch-label 37

switch-label: 38 case constant-expression : 39 default : 40

iteration-statement: 41 while-statement 42 do-statement 43 for-statement 44 foreach-statement 45

while-statement: 46 while ( boolean-expression ) embedded-statement 47

Page 467: C# Language Specification

Annex A Grammar

447

do-statement: 1 do embedded-statement while ( boolean-expression ) ; 2

for-statement: 3 for ( for-initializeropt ; for-conditionopt ; for-iteratoropt ) embedded-statement 4

for-initializer: 5 local-variable-declaration 6 statement-expression-list 7

for-condition: 8 boolean-expression 9

for-iterator: 10 statement-expression-list 11

statement-expression-list: 12 statement-expression 13 statement-expression-list , statement-expression 14

foreach-statement: 15 foreach ( type identifier in expression ) embedded-statement 16

jump-statement: 17 break-statement 18 continue-statement 19 goto-statement 20 return-statement 21 throw-statement 22

break-statement: 23 break ; 24

continue-statement: 25 continue ; 26

goto-statement: 27 goto identifier ; 28 goto case constant-expression ; 29 goto default ; 30

return-statement: 31 return expressionopt ; 32

throw-statement: 33 throw expressionopt ; 34

try-statement: 35 try block catch-clauses 36 try block finally-clause 37 try block catch-clauses finally-clause 38

catch-clauses: 39 specific-catch-clauses general-catch-clauseopt 40 specific-catch-clausesopt general-catch-clause 41

specific-catch-clauses: 42 specific-catch-clause 43 specific-catch-clauses specific-catch-clause 44

specific-catch-clause: 45 catch ( class-type identifieropt ) block 46

Page 468: C# Language Specification

C# LANGUAGE SPECIFICATION

448

general-catch-clause: 1 catch block 2

finally-clause: 3 finally block 4

checked-statement: 5 checked block 6

unchecked-statement: 7 unchecked block 8

lock-statement: 9 lock ( expression ) embedded-statement 10

using-statement: 11 using ( resource-acquisition ) embedded-statement 12

yield-statement: 13 yield return expression ; 14 yield break ; 15

resource-acquisition: 16 local-variable-declaration 17 expression 18

namespace-declaration: 19 namespace qualified-identifier namespace-body ;opt 20

qualified-identifier: 21 identifier 22 qualified-identifier . identifier 23

namespace-body: 24 { extern-alias-directivesopt using-directivesopt namespace-member-declarationsopt } 25

extern-alias-directives: 26 extern-alias-directive 27 extern-alias-directives extern-alias-directive 28

extern-alias-directive: 29 extern alias identifier ; 30

using-directives: 31 using-directive 32 using-directives using-directive 33

using-directive: 34 using-alias-directive 35 using-namespace-directive 36

using-alias-directive: 37 using identifier = namespace-or-type-name ; 38

using-namespace-directive: 39 using namespace-name ; 40

namespace-member-declarations: 41 namespace-member-declaration 42 namespace-member-declarations namespace-member-declaration 43

namespace-member-declaration: 44 namespace-declaration 45 type-declaration 46

Page 469: C# Language Specification

Annex A Grammar

449

type-declaration: 1 class-declaration 2 struct-declaration 3 interface-declaration 4 enum-declaration 5 delegate-declaration 6

A.2.6 Classes 7

class-declaration: 8 attributesopt class-modifiersopt partialopt class identifier type-parameter-listopt 9 class-baseopt type-parameter-constraints-clausesopt class-body ;opt 10

class-modifiers: 11 class-modifier 12 class-modifiers class-modifier 13

class-modifier: 14 new 15 public 16 protected 17 internal 18 private 19 abstract 20 sealed 21 static 22

class-base: 23 : class-type 24 : interface-type-list 25 : class-type , interface-type-list 26

interface-type-list: 27 interface-type 28 interface-type-list , interface-type 29

class-body: 30 { class-member-declarationsopt } 31

class-member-declarations: 32 class-member-declaration 33 class-member-declarations class-member-declaration 34

class-member-declaration: 35 constant-declaration 36 field-declaration 37 method-declaration 38 property-declaration 39 event-declaration 40 indexer-declaration 41 operator-declaration 42 constructor-declaration 43 destructor-declaration 44 static-constructor-declaration 45 type-declaration 46

constant-declaration: 47 attributesopt constant-modifiersopt const type constant-declarators ; 48

Page 470: C# Language Specification

C# LANGUAGE SPECIFICATION

450

constant-modifiers: 1 constant-modifier 2 constant-modifiers constant-modifier 3

constant-modifier: 4 new 5 public 6 protected 7 internal 8 private 9

constant-declarators: 10 constant-declarator 11 constant-declarators , constant-declarator 12

constant-declarator: 13 identifier = constant-expression 14

field-declaration: 15 attributesopt field-modifiersopt type variable-declarators ; 16

field-modifiers: 17 field-modifier 18 field-modifiers field-modifier 19

field-modifier: 20 new 21 public 22 protected 23 internal 24 private 25 static 26 readonly 27 volatile 28

variable-declarators: 29 variable-declarator 30 variable-declarators , variable-declarator 31

variable-declarator: 32 identifier 33 identifier = variable-initializer 34

variable-initializer: 35 expression 36 array-initializer 37

method-declaration: 38 method-header method-body 39

method-header: 40 attributesopt method-modifiersopt return-type member-name type-parameter-listopt 41 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt 42

method-modifiers: 43 method-modifier 44 method-modifiers method-modifier 45

Page 471: C# Language Specification

Annex A Grammar

451

method-modifier: 1 new 2 public 3 protected 4 internal 5 private 6 static 7 virtual 8 sealed 9 override 10 abstract 11 extern 12

return-type: 13 type 14 void 15

member-name: 16 identifier 17 interface-type . identifier 18

method-body: 19 block 20 ; 21

formal-parameter-list: 22 fixed-parameters 23 fixed-parameters , parameter-array 24 parameter-array 25

fixed-parameters: 26 fixed-parameter 27 fixed-parameters , fixed-parameter 28

fixed-parameter: 29 attributesopt parameter-modifieropt type identifier 30

parameter-modifier: 31 ref 32 out 33

parameter-array: 34 attributesopt params array-type identifier 35

property-declaration: 36 attributesopt property-modifiersopt type member-name { accessor-declarations } 37

property-modifiers: 38 property-modifier 39 property-modifiers property-modifier 40

Page 472: C# Language Specification

C# LANGUAGE SPECIFICATION

452

property-modifier: 1 new 2 public 3 protected 4 internal 5 private 6 static 7 virtual 8 sealed 9 override 10 abstract 11 extern 12

member-name: 13 identifier 14 interface-type . identifier 15

accessor-declarations: 16 get-accessor-declaration set-accessor-declarationopt 17 set-accessor-declaration get-accessor-declarationopt 18

get-accessor-declaration: 19 attributesopt accessor-modifieropt get accessor-body 20

set-accessor-declaration: 21 attributesopt accessor-modifieropt set accessor-body 22

accessor-modifier: 23 protected 24 internal 25 private 26 protected internal 27 internal protected 28

accessor-body: 29 block 30 ; 31

event-declaration: 32 attributesopt event-modifiersopt event type variable-declarators ; 33 attributesopt event-modifiersopt event type member-name { event-accessor-declarations 34 } 35

event-modifiers: 36 event-modifier 37 event-modifiers event-modifier 38

event-modifier: 39 new 40 public 41 protected 42 internal 43 private 44 static 45 virtual 46 sealed 47 override 48 abstract 49 extern 50

Page 473: C# Language Specification

Annex A Grammar

453

event-accessor-declarations: 1 add-accessor-declaration remove-accessor-declaration 2 remove-accessor-declaration add-accessor-declaration 3

add-accessor-declaration: 4 attributesopt add block 5

remove-accessor-declaration: 6 attributesopt remove block 7

indexer-declaration: 8 attributesopt indexer-modifiersopt indexer-declarator { accessor-declarations } 9

indexer-modifiers: 10 indexer-modifier 11 indexer-modifiers indexer-modifier 12

indexer-modifier: 13 new 14 public 15 protected 16 internal 17 private 18 virtual 19 sealed 20 override 21 abstract 22 extern 23

indexer-declarator: 24 type this [ formal-parameter-list ] 25 type interface-type . this [ formal-parameter-list ] 26

operator-declaration: 27 attributesopt operator-modifiers operator-declarator operator-body 28

operator-modifiers: 29 operator-modifier 30 operator-modifiers operator-modifier 31

operator-modifier: 32 public 33 static 34 extern 35

operator-declarator: 36 unary-operator-declarator 37 binary-operator-declarator 38 conversion-operator-declarator 39

unary-operator-declarator: 40 type operator overloadable-unary-operator ( type identifier ) 41

overloadable-unary-operator: one of 42 + - ! ~ ++ -- true false 43

binary-operator-declarator: 44 type operator overloadable-binary-operator ( type identifier , type identifier ) 45

overloadable-binary-operator: one of 46 + - * / % & | ^ << right-shift == != > < 47 >= <= 48

Page 474: C# Language Specification

C# LANGUAGE SPECIFICATION

454

conversion-operator-declarator: 1 implicit operator type ( type identifier ) 2 explicit operator type ( type identifier ) 3

operator-body: 4 block 5 ; 6

constructor-declaration: 7 attributesopt constructor-modifiersopt constructor-declarator constructor-body 8

constructor-modifiers: 9 constructor-modifier 10 constructor-modifiers constructor-modifier 11

constructor-modifier: 12 public 13 protected 14 internal 15 private 16 extern 17

constructor-declarator: 18 identifier ( formal-parameter-listopt ) constructor-initializeropt 19

constructor-initializer: 20 : base ( argument-listopt ) 21 : this ( argument-listopt ) 22

constructor-body: 23 block 24 ; 25

static-constructor-declaration: 26 attributesopt static-constructor-modifiers identifier ( ) static-constructor-body 27

static-constructor-modifiers: 28 externopt static 29 static externopt 30

static-constructor-body: 31 block 32 ; 33

destructor-declaration: 34 attributesopt externopt ~ identifier ( ) destructor-body 35

destructor-body: 36 block 37 ; 38

A.2.7 Structs 39

struct-declaration: 40 attributesopt struct-modifiersopt partialopt struct identifier type-parameter-listopt 41

struct-interfacesopt type-parameter-constraints-clausesopt struct-body ;opt 42

struct-modifiers: 43 struct-modifier 44 struct-modifiers struct-modifier 45

Page 475: C# Language Specification

Annex A Grammar

455

struct-modifier: 1 new 2 public 3 protected 4 internal 5 private 6

struct-interfaces: 7 : interface-type-list 8

struct-body: 9 { struct-member-declarationsopt } 10

struct-member-declarations: 11 struct-member-declaration 12 struct-member-declarations struct-member-declaration 13

struct-member-declaration: 14 constant-declaration 15 field-declaration 16 method-declaration 17 property-declaration 18 event-declaration 19 indexer-declaration 20 operator-declaration 21 constructor-declaration 22 static-constructor-declaration 23 type-declaration 24

A.2.8 Arrays 25

array-type: 26 non-array-type rank-specifiers 27

non-array-type: 28 type 29

rank-specifiers: 30 rank-specifier 31 rank-specifiers rank-specifier 32

rank-specifier: 33 [ dim-separatorsopt ] 34

dim-separators: 35 , 36 dim-separators , 37

array-initializer: 38 { variable-initializer-listopt } 39 { variable-initializer-list , } 40

variable-initializer-list: 41 variable-initializer 42 variable-initializer-list , variable-initializer 43

variable-initializer: 44 expression 45 array-initializer 46

Page 476: C# Language Specification

C# LANGUAGE SPECIFICATION

456

A.2.9 Interfaces 1

interface-declaration: 2 attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt 3 interface-baseopt type-parameter-constraints-clausesopt interface-body ;opt 4

interface-modifiers: 5 interface-modifier 6 interface-modifiers interface-modifier 7

interface-modifier: 8 new 9 public 10 protected 11 internal 12 private 13

interface-base: 14 : interface-type-list 15

interface-body: 16 { interface-member-declarationsopt } 17

interface-member-declarations: 18 interface-member-declaration 19 interface-member-declarations interface-member-declaration 20

interface-member-declaration: 21 interface-method-declaration 22 interface-property-declaration 23 interface-event-declaration 24 interface-indexer-declaration 25

interface-method-declaration: 26 attributesopt newopt return-type identifier type-parameter-listopt 27 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt ; 28

interface-property-declaration: 29 attributesopt newopt type identifier { interface-accessors } 30

interface-accessors: 31 attributesopt get ; 32 attributesopt set ; 33 attributesopt get ; attributesopt set ; 34 attributesopt set ; attributesopt get ; 35

interface-event-declaration: 36 attributesopt newopt event type identifier ; 37

interface-indexer-declaration: 38 attributesopt newopt type this [ formal-parameter-list ] { interface-accessors } 39

A.2.10 Enums 40

enum-declaration: 41 attributesopt enum-modifiersopt enum identifier enum-baseopt enum-body ;opt 42

enum-base: 43 : integral-type 44

enum-body: 45 { enum-member-declarationsopt } 46 { enum-member-declarations , } 47

Page 477: C# Language Specification

Annex A Grammar

457

enum-modifiers: 1 enum-modifier 2 enum-modifiers enum-modifier 3

enum-modifier: 4 new 5 public 6 protected 7 internal 8 private 9

enum-member-declarations: 10 enum-member-declaration 11 enum-member-declarations , enum-member-declaration 12

enum-member-declaration: 13 attributesopt identifier 14 attributesopt identifier = constant-expression 15

A.2.11 Delegates 16

delegate-declaration: 17 attributesopt delegate-modifiersopt delegate return-type identifier type-parameter-listopt 18 ( formal-parameter-listopt ) type-parameter-constraints-clausesopt ; 19

delegate-modifiers: 20 delegate-modifier 21 delegate-modifiers delegate-modifier 22

delegate-modifier: 23 new 24 public 25 protected 26 internal 27 private 28

A.2.12 Attributes 29

global-attributes: 30 global-attribute-sections 31

global-attribute-sections: 32 global-attribute-section 33 global-attribute-sections global-attribute-section 34

global-attribute-section: 35 [ global-attribute-target-specifier attribute-list ] 36 [ global-attribute-target-specifier attribute-list , ] 37

global-attribute-target-specifier: 38 global-attribute-target : 39

global-attribute-target: 40 identifier 41 keyword 42

attributes: 43 attribute-sections 44

attribute-sections: 45 attribute-section 46 attribute-sections attribute-section 47

Page 478: C# Language Specification

C# LANGUAGE SPECIFICATION

458

attribute-section: 1 [ attribute-target-specifieropt attribute-list ] 2 [ attribute-target-specifieropt attribute-list , ] 3

attribute-target-specifier: 4 attribute-target : 5

attribute-target: 6 identifier 7 keyword 8

attribute-list: 9 attribute 10 attribute-list , attribute 11

attribute: 12 attribute-name attribute-argumentsopt 13

attribute-name: 14 type-name 15

attribute-arguments: 16 ( positional-argument-listopt ) 17 ( positional-argument-list , named-argument-list ) 18 ( named-argument-list ) 19

positional-argument-list: 20 positional-argument 21 positional-argument-list , positional-argument 22

positional-argument: 23 attribute-argument-expression 24

named-argument-list: 25 named-argument 26 named-argument-list , named-argument 27

named-argument: 28 identifier = attribute-argument-expression 29

attribute-argument-expression: 30 expression 31

A.2.13 Generics 32

type-parameter-list: 33 < type-parameters > 34

type-parameters: 35 attributesopt type-parameter 36 type-parameters , attributesopt type-parameter 37

type-parameter: 38 identifier 39

type-argument-list: 40 < type-arguments > 41

type-arguments: 42 type-argument 43 type-arguments , type-argument 44

type-argument: 45 type 46

Page 479: C# Language Specification

Annex A Grammar

459

type-parameter-constraints-clauses: 1 type-parameter-constraints-clause 2 type-parameter-constraints-clauses type-parameter-constraints-clause 3

type-parameter-constraints-clause: 4 where type-parameter : type-parameter-constraints 5

type-parameter-constraints: 6 primary-constraint 7 secondary-constraints 8 constructor-constraint 9 primary-constraint , secondary-constraints 10 primary-constraint , constructor-constraint 11 secondary-constraints , constructor-constraint 12 primary-constraint , secondary-constraints , constructor-constraint 13

primary-constraint: 14 class-type 15 class 16 struct 17

secondary-constraints: 18 interface-type 19 type-parameter 20 secondary-constraints , interface-type 21 secondary-constraints , type-parameter 22

constructor-constraint: 23 new ( ) 24

A.3 Grammar extensions for unsafe code 25

embedded-statement: 26 ... 27 unsafe-statement 28

unsafe-statement: 29 unsafe block 30

type: 31 value-type 32 reference-type 33 pointer-type 34

pointer-type: 35 unmanaged-type * 36 void * 37

unmanaged-type: 38 type 39

primary-no-array-creation-expression: 40 ... 41 pointer-member-access 42 pointer-element-access 43 sizeof-expression 44

unary-expression: 45 ... 46 pointer-indirection-expression 47 addressof-expression 48

Page 480: C# Language Specification

C# LANGUAGE SPECIFICATION

460

pointer-indirection-expression: 1 * unary-expression 2

pointer-member-access: 3 primary-expression -> identifier type-argument-listopt 4

pointer-element-access: 5 primary-no-array-creation-expression [ expression ] 6

addressof-expression: 7 & unary-expression 8

sizeof-expression: 9 sizeof ( unmanaged-type ) 10

embedded-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

fixed-pointer-initializer: 21 & variable-reference 22 expression 23

variable-initializer: 24 expression 25 array-initializer 26 stackalloc-initializer 27

stackalloc-initializer: 28 stackalloc unmanaged-type [ expression ] 29

End of informative text. 30

Page 481: C# Language Specification

Annex B Portability issues

461

Annex B. Portability issues 1

This clause is informative. 2

This annex collects some information about portability that appears in this International Standard. 3

B.1 Undefined behavior 4

A program that does not contain any occurrences of the unsafe modifier cannot exhibit any undefined 5 behavior. 6

The behavior is undefined in the following circumstances: 7

1. When dereferencing the result of converting one pointer type to another and the resulting pointer is not 8 correctly aligned for the pointed-to type. (§25.4) 9

2. When the unary * operator is applied to a pointer containing an invalid value (§25.5.1). 10

3. When a pointer is subscripted to access an out-of-bounds element (§25.5.3). 11

4. Modifying objects of managed type through fixed pointers (§25.6) 12

5. The initial content of memory allocated by stackalloc (§25.7). 13

6. Attempting to allocate a negative number of items using stackalloc (§25.7). 14

B.2 Implementation-defined behavior 15

A conforming implementation is required to document its choice of behavior in each of the areas listed in 16 this subclause. The following are implementation-defined: 17

1. The behavior when an identifier not in Normalization Form C is encountered (§9.4.2). 18

2. The purpose of a line-indicator with an identifier-or-keyword whose value does not equal default 19 (§9.5.7). 20

3. The interpretation of the input-characters in the pp-pragma-text of a #pragma directive (§9.5.8). 21

4. The values of any application parameters passed to Main by the host environment prior to application 22 startup (§10.1). 23

5. Whether a System.ArithmeticException (or a subclass thereof) is thrown or the overflow goes 24 unreported with the resulting value being that of the left operand, when in an unchecked context and 25 the left operand of an integer division is the maximum negative int or long value and the right 26 operand is –1. (§14.7.2). 27

6. The mechanism by which linkage to an external function is achieved (§17.5.7). 28

7. The impact of thread termination when no matching catch clause is found for an exception and the code 29 that initially started that thread is reached. (§23.3) 30

8. The purpose of attribute target specifiers other than those identified by this standard (§24.2). 31

9. The mappings between pointers and integers (§25.4). 32

10. The effect of applying the unary * operator to a null pointer (§25.5.1). 33

11. The behavior when pointer arithmetic overflows the domain of the pointer type (§25.5.5). 34

12. The result of the sizeof operator for nonpre-defined value types (§25.5.8). 35

Page 482: C# Language Specification

C# LANGUAGE SPECIFICATION

462

13. The behavior of the fixed statement if the array expression is null or if the array has zero 1 elements (§25.6). 2

14. The behavior of the fixed statement if the string expression is null (§25.6). 3

15. The value returned when a stack allocation of size zero is made (§25.7). 4

B.3 Unspecified behavior 5

1. The time at which the destructor (if any) for an object is run, once that object has become eligible for 6 destruction (§10.9). 7

2. The value of the result when converting out-of-range values from float or double values to an 8 integral type in an unchecked context (§13.2.1). 9

3. The layout of arrays, except in an unsafe context (§14.5.10.2). 10

4. Whether there is any way to execute the block of an anonymous method other than through evaluation 11 and invocation of the anonymous-method-expression (§14.5.14.2). 12

5. The invocation list of a delegate produced from an anonymous-method-expression contains a single 13 entry. The exact target object and target method of the delegate are unspecified (§14.5.14.4). 14

6. The exact timing of static field initialization (§17.4.5.1). 15

7. The attributes of a type declared in multiple parts are determined by combining, in an unspecified 16 order, the attributes of each of its parts (§24.2). 17

8. The order in which members are packed into a struct (§25.5.8). 18

9. When an enumerator object is in the running state, the result of invoking MoveNext is unspecified 19 (§27.2.1). 20

10. When an enumerator object is in the before, running, or after states, the result of accessing Current is 21 unspecified (§27.2.2). 22

11. When an enumerator object is in the running state, the result of invoking Dispose is unspecified 23 (§27.2.3). 24

B.4 Other Issues 25

1. The exact results of floating-point expression evaluation can vary from one implementation to another, 26 because an implementation is permitted to evaluate such expressions using a greater range and/or 27 precision than is required. (§11.1.6) 28

2. The CLI reserves certain signatures for compatibility with other programming languages. (§17.2.7) 29

End of informative text. 30

Page 483: C# Language Specification

Annex C Naming guidelines

463

Annex C. Naming guidelines 1

This annex is informative. 2

One of the most important elements of predictability and discoverability is the use of a consistent naming 3 pattern. Many of the common user questions don’t even arise once these conventions are understood and 4 widely used. There are three elements to the naming guidelines: 5

1. Casing – use of the correct capitalization style 6

2. Mechanical – use nouns for classes, verbs for methods, etc. 7

3. Word choice – use consistent terms across class libraries. 8

The following subclause lays out rules for the first two elements, and some philosophy for the third. 9

C.1 Capitalization styles 10

The following subclause describes different ways of capitalizing identifiers. 11

C.1.1 Pascal casing 12

This convention capitalizes the first character of each word. For example: 13

Color BitConverter 14

C.1.2 Camel casing 15

This convention capitalizes the first character of each word except the first word. For example: 16

backgroundColor totalValueCount 17

C.1.3 All uppercase 18

Only use all uppercase letters for an identifier if it contains an abbreviation. For example: 19

System.IO 20 System.WinForms.UI 21

C.1.4 Capitalization summary 22

The following table summarizes the capitalization style for the different kinds of identifiers: 23

24

Page 484: C# Language Specification

C# LANGUAGE SPECIFICATION

464

Type Case Notes Class PascalCase

Class, attribute PascalCase Has a suffix of Attribute Class, exception PascalCase Has a suffix of Exception Constant PascalCase

Enum type PascalCase

Enum value PascalCase

Event PascalCase

Field, non-public instance camelCase

Field, public instance PascalCase Rarely used (use a property instead)

Interface PascalCase Has a prefix of I Local variable camelCase

Method PascalCase

Namespace PascalCase

Parameter camelCase

Property PascalCase 1

C.2 Word choice 2

• Do avoid using class names duplicated in heavily used namespaces. For example, don’t use the 3 following for a class name. 4

System Collections Forms UI 5

• Do not use abbreviations in identifiers. 6

• If you must use abbreviations, do use camelCase for any abbreviation containing more than two 7 characters, even if this is not the usual abbreviation. 8

C.3 Namespaces 9

The general rule for namespace naming is CompanyName.TechnologyName. 10

• Do avoid the possibility of two published namespaces having the same name, by prefixing namespace 11 names with a company name or other well-established brand. For example, Microsoft.Office for the 12 Office Automation classes provided by Microsoft. 13

• Do use PascalCase, and separate logical components with periods (as in 14 Microsoft.Office.PowerPoint). If your brand employs non-traditional casing, do follow the 15 casing defined by your brand, even if it deviates from normal namespace casing (for example, 16 NeXT.WebObjects, and ee.cummings). 17

• Do use plural namespace names where appropriate. For example, use System.Collections rather 18 than System.Collection. Exceptions to this rule are brand names and abbreviations. For example, 19 use System.IO not System.IOs. 20

• Do not have namespaces and classes with the same name. 21

C.4 Classes 22

• Do name classes with nouns or noun phrases. 23

Page 485: C# Language Specification

Annex C Naming guidelines

465

• Do use PascalCase. 1

• Do use sparingly, abbreviations in class names. 2

• Do not use any prefix (such as “C”, for example). Where possible, avoid starting with the letter “I”, 3 since that is the recommended prefix for interface names. If you must start with that letter, make sure the 4 second character is lowercase, as in IdentityStore. 5

• Do not use any underscores. 6

public class FileStream { … } 7 public class Button { … } 8 public class String { … } 9

C.5 Interfaces 10

• Do name interfaces with nouns or noun phrases, or adjectives describing behavior. For example, 11 IComponent (descriptive noun), ICustomAttributeProvider (noun phrase), and IPersistable 12 (adjective). 13

• Do use PascalCase. 14

• Do use sparingly, abbreviations in interface names. 15

• Do not use any underscores. 16

• Do prefix interface names with the letter “I”, to indicate that the type is an interface. 17

• Do use similar names when defining a class/interface pair where the class is a standard implementation 18 of the interface. The names should differ only by the “I” prefix in the interface name. This approach is 19 used for the interface IComponent and its standard implementation, Component. 20

public interface IComponent { … } 21 public class Component : IComponent { … } 22 public interface IServiceProvider{ … } 23 public interface IFormatable { … } 24

C.6 Enums 25

• Do use PascalCase for enums. 26

• Do use PascalCase for enum value names. 27

• Do use sparingly, abbreviations in enum names. 28

• Do not use a family-name prefix on enum. 29

• Do not use any “Enum” suffix on enum types. 30

• Do use a singular name for enums. 31

• Do use a plural name for bit fields. 32

• Do define enumerated values using an enum if they are used in a parameter or property. This gives 33 development tools a chance at knowing the possible values for a property or parameter. 34

public enum FileMode 35 { 36 Create, 37 CreateNew, 38 Open, 39 OpenOrCreate, 40 Truncate 41 } 42

• Do use the Flags custom attribute if the numeric values are meant to be bitwise ORed together. 43

Page 486: C# Language Specification

C# LANGUAGE SPECIFICATION

466

[Flags] 1 public enum Bindings 2 { 3 CreateInstance, 4 DefaultBinding, 5 ExcatBinding, 6 GetField, 7 GetProperty, 8 IgnoreCase, 9 InvokeMethod, 10 NonPublic, 11 OABinding, 12 SetField, 13 SetProperty, 14 Static 15 } 16

• Do use int as the underlying type of an enum. (An exception to this rule is if the enum represents flags 17 and there are more than 32 flags, or the enum might grow to that many flags in the future, or the type 18 needs to be different from int for backward compatibility.) 19

• Do use enums only if the value can be completely expressed as a set of bit flags. Do not use enums for 20 open sets (such as operating system version). 21

C.7 Static fields 22

• Do name static members with nouns, noun phrases, or abbreviations for nouns. 23

• Do name static members using PascalCase. 24

• Do not use Hungarian-type prefixes on static member names. 25

C.8 Parameters 26

• Do use descriptive names such that a parameter’s name and type clearly imply its meaning. 27

• Do name parameters using camelCase. 28

• Do prefer names based on a parameter’s meaning, to names based on the parameter’s type. It is likely 29 that development tools will provide the information about type in a convenient way, so the parameter 30 name can be put to better use describing semantics rather than type. 31

• Do not reserve parameters for future use. If more data is need in the next version, a new overload can be 32 added. 33

• Do not use Hungarian-type prefixes. 34

Type GetType(string typeName) 35 string Format(string format, object[] args) 36

C.9 Methods 37

• Do name methods with verbs or verb phrases. 38

• Do name methods with PascalCase. 39

RemoveAll() GetCharArray() Invoke() 40

C.10 Properties 41

• Do name properties using noun or noun phrases. 42

• Do name properties with PascalCase. 43

• Consider having a property with the same as a type. When declaring a property with the same name as a 44 type, also make the type of the property be that type. In other words, the following is okay 45

Page 487: C# Language Specification

Annex C Naming guidelines

467

public enum Color { … } 1

public class Control 2 { 3 public Color Color { get { … } set { … } } 4 } 5

but this is not 6

public enum Color { … } 7

public class Control 8 { 9 public int Color { get { … } set { … } } 10 } 11

In the latter case, it will not be possible to refer to the members of the Color enum because Color.Xxx 12 will be interpreted as being a member access that first gets the value of the Color property (of type 13 int) and then accesses a member of that value (which would have to be an instance member of 14 System.Int32). 15

C.11 Events 16

• Do name event handlers with the EventHandler suffix. 17

public delegate void MouseEventHandler(object sender, MouseEvent e); 18

• Do use two parameters named sender and e. The sender parameter represents the object that raised the 19 event, and this parameter is always of type object, even if it is possible to employ a more specific type. 20 The state associated with the event is encapsulated in an instance e of an event class. Use an appropriate 21 and specific event class for its type. 22

public delegate void MouseEventHandler(object sender, MouseEvent e); 23

• Do name event argument classes with the EventArgs suffix. 24

public class MouseEventArgs : EventArgs 25 { 26 int x; 27 int y; 28

public MouseEventArgs(int x, int y) { 29 this.x = x; 30 this.y = y; 31 } 32

public int X { get { return x; } } 33 public int Y { get { return y; } } 34 } 35

• Do name event names that have a concept of pre- and post-operation using the present and past tense (do 36 not use BeforeXxx/AfterXxx pattern). For example, a close event that could be canceled would have a 37 Closing and Closed event. 38

public event ControlEventHandler ControlAdded; 39

• Consider naming events with a verb. 40

C.12 Case sensitivity 41

• Don’t use names that require case sensitivity. Components might need to be usable from both case-42 sensitive and case-insensitive languages. Since case-insensitive languages cannot distinguish between 43 two names within the same context that differ only by case, components must avoid this situation. 44

Examples of what not to do: 45

• Don’t have two namespaces whose names differ only by case. 46

namespace ee.cummings; 47 namespace Ee.Cummings; 48

Page 488: C# Language Specification

C# LANGUAGE SPECIFICATION

468

• Don’t have a method with two parameters whose names differ only by case. 1

void F(string a, string A) 2

• Don’t have a namespace with two types whose names differ only by case. 3

System.WinForms.Point p; 4 System.WinForms.POINT pp; 5

• Don’t have a type with two properties whose names differ only by case. 6

int f { get; set; } 7 int F { get; set; } 8

• Don’t have a type with two methods whose names differ only by case. 9

void f(); 10 void F(); 11

C.13 Avoiding type name confusion 12

Different languages use different names to identify the fundamental managed types, so in a multi-language 13 environment, designers must take care to avoid language-specific terminology. This subclause describes a 14 set of rules that help avoid type name confusion. 15

• Do use semantically interesting names rather than type names. 16

• In the rare case that a parameter has no semantic meaning beyond its type, use a generic name. For 17 example, a class that supports writing a variety of data types into a stream might have: 18

void Write(double value); 19 void Write(float value); 20 void Write(long value); 21 void Write(int value); 22 void Write(short value); 23

rather than a language-specific alternative such as: 24

void Write(double doubleValue); 25 void Write(float floatValue); 26 void Write(long longValue); 27 void Write(int intValue); 28 void Write(short shortValue); 29

• In the extremely rare case that it is necessary to have a uniquely named method for each fundamental 30 data type, do use the following universal type names: SByte, Byte, Int16, UInt16, Int32, UInt32, 31 Int64, UInt64, Single, Double, Boolean, Char, String, and Object. For example, a class that 32 supports reading a variety of data types from a stream might have: 33

double ReadDouble(); 34 float ReadSingle(); 35 long ReadInt64(); 36 int ReadInt32(); 37 short ReadInt16(); 38

rather than a language-specific alternative such as: 39

double ReadDouble(); 40 float ReadFloat(); 41 long ReadLong(); 42 int ReadInt(); 43 short ReadShort(); 44

End of informative text. 45

Page 489: C# Language Specification

Annex D Standard Library

469

Annex D. Standard Library 1

A conforming C# implementation shall provide a set of types having specific semantics. These types and 2 their members are listed here, in alphabetical order. For a formal definition of these types and their 3 members, refer to ISO/IEC 23271:2002 Common Language Infrastructure (CLI), Partition IV; Base Class 4 Library (BCL), Extended Numerics Library, and Extended Array Library, which are included by reference in 5 this International Standard. 6

Type names beginning with System. are intended for use by the Standard Library: Such names not 7 currently in use might be defined in a future version of this International Standard. 8

9

// Namespace: System, Library: BCL 10 public class ApplicationException: Exception 11 { 12 public ApplicationException(); 13 public ApplicationException(string message); 14 public ApplicationException(string message, Exception innerException); 15 } 16 17 // Namespace: System, Library: BCL 18 public class ArgumentException: SystemException 19 { 20 public ArgumentException(); 21 public ArgumentException(string message); 22 public ArgumentException(string message, Exception innerException); 23 public ArgumentException(string message, string paramName, Exception 24 innerException); 25 public ArgumentException(string message, string paramName); 26 public virtual string ParamName { get; } 27 } 28 29 // Namespace: System, Library: BCL 30 public class ArgumentNullException: ArgumentException 31 { 32 public ArgumentNullException(); 33 public ArgumentNullException(string paramName); 34 public ArgumentNullException(string paramName, string message); 35 } 36 37 // Namespace: System, Library: BCL 38 public class ArgumentOutOfRangeException: ArgumentException 39 { 40 public ArgumentOutOfRangeException(); 41 public ArgumentOutOfRangeException(string paramName); 42 public ArgumentOutOfRangeException(string paramName, string message); 43 public ArgumentOutOfRangeException(string paramName, object actualValue, 44 string message); 45 public virtual object ActualValue { get; } 46 public override string Message { get; } 47 } 48 49 // Namespace: System, Library: BCL 50 public class ArithmeticException: SystemException 51 { 52 public ArithmeticException(); 53 public ArithmeticException(string message); 54 public ArithmeticException(string message, Exception innerException); 55 } 56 57 // Namespace: System, Library: BCL 58

Page 490: C# Language Specification

C# LANGUAGE SPECIFICATION

470

public abstract class Array: ICloneable, ICollection, IEnumerable, IList 1 { 2 protected Array(); 3 public static int BinarySearch(Array array, object value); 4 public static int BinarySearch(Array array, int index, int length, object 5 value); 6 public static int BinarySearch(Array array, object value, IComparer 7 comparer); 8 public static int BinarySearch(Array array, int index, int length, object 9 value, IComparer comparer); 10 public static void Clear(Array array, int index, int length); 11 public virtual object Clone(); 12 public static void Copy(Array sourceArray, Array destinationArray, int 13 length); 14 public static void Copy(Array sourceArray, int sourceIndex, Array 15 destinationArray, int destinationIndex, int length); 16 public virtual void CopyTo(Array array, int index); 17 public static Array CreateInstance(Type elementType, int length); 18 public static Array CreateInstance(Type elementType, int length1, int 19 length2); 20 public static Array CreateInstance(Type elementType, int length1, int 21 length2, int length3); 22 public static Array CreateInstance(Type elementType, int[] lengths); 23 public static Array CreateInstance(Type elementType, int[] lengths, int[] 24 lowerBounds); 25 public virtual IEnumerator GetEnumerator(); 26 public object GetValue(int[] indices); 27 public object GetValue(int index); 28 public object GetValue(int index1, int index2); 29 public object GetValue(int index1, int index2, int index3); 30 public static int IndexOf(Array array, object value); 31 public static int IndexOf(Array array, object value, int startIndex); 32 public static int IndexOf(Array array, object value, int startIndex, int 33 count); 34 public static int LastIndexOf(Array array, object value); 35 public static int LastIndexOf(Array array, object value, int startIndex); 36 public static int LastIndexOf(Array array, object value, int startIndex, 37 int count); 38 public static void Reverse(Array array); 39 public static void Reverse(Array array, int index, int length); 40 public void SetValue(object value, int index); 41 public void SetValue(object value, int index1, int index2); 42 public void SetValue(object value, int index1, int index2, int index3); 43 public void SetValue(object value, int[] indices); 44 public static void Sort(Array array); 45 public static void Sort(Array keys, Array items); 46 public static void Sort(Array array, int index, int length); 47 public static void Sort(Array keys, Array items, int index, int length); 48 public static void Sort(Array array, IComparer comparer); 49 public static void Sort(Array keys, Array items, IComparer comparer); 50 public static void Sort(Array array, int index, int length, IComparer 51 comparer); 52 public static void Sort(Array keys, Array items, int index, int length, 53 IComparer comparer); 54 int IList.Add(object value); 55 void IList.Clear(); 56 bool IList.Contains(object value); 57 int IList.IndexOf(object value); 58 void IList.Insert(int index, object value); 59 void IList.Remove(object value); 60 void IList.RemoveAt(int index); 61 bool IList.IsFixedSize { get; } 62 bool IList.IsReadOnly { get; } 63 bool ICollection.IsSynchronized { get; } 64 public int Length { get; } 65 public long LongLength {get;} 66 public int Rank { get; } 67 object ICollection.SyncRoot { get; } 68 int ICollection.Count { get; } 69

Page 491: C# Language Specification

Annex D Standard Library

471

public virtual object this[int index] { get; set; } 1 } 2 3 // Namespace: System.Collections, Library: BCL 4 public class ArrayList: ICloneable, ICollection, IEnumerable, IList 5 { 6 public ArrayList(); 7 public ArrayList(int capacity); 8 public ArrayList(ICollection c); 9 public static ArrayList Adapter(IList list); 10 public virtual int Add(object value); 11 public virtual void AddRange(ICollection c); 12 public virtual int BinarySearch(object value, IComparer comparer); 13 public virtual int BinarySearch(object value); 14 public virtual int BinarySearch(int index, int count, object value, 15 IComparer comparer); 16 public virtual void Clear(); 17 public virtual object Clone(); 18 public virtual bool Contains(object item); 19 public virtual void CopyTo(Array array, int arrayIndex); 20 public virtual void CopyTo(int index, Array array, int arrayIndex, int 21 count); 22 public virtual void CopyTo(Array array); 23 public static ArrayList FixedSize(ArrayList list); 24 public virtual IEnumerator GetEnumerator(); 25 public virtual IEnumerator GetEnumerator(int index, int count); 26 public virtual ArrayList GetRange(int index, int count); 27 public virtual int IndexOf(object value); 28 public virtual int IndexOf(object value, int startIndex, int count); 29 public virtual int IndexOf(object value, int startIndex); 30 public virtual void Insert(int index, object value); 31 public virtual void InsertRange(int index, ICollection c); 32 public virtual int LastIndexOf(object value, int startIndex, int count); 33 public virtual int LastIndexOf(object value, int startIndex); 34 public virtual int LastIndexOf(object value); 35 public static ArrayList ReadOnly(ArrayList list); 36 public virtual void Remove(object obj); 37 public virtual void RemoveAt(int index); 38 public virtual void RemoveRange(int index, int count); 39 public static ArrayList Repeat(object value, int count); 40 public virtual void Reverse(int index, int count); 41 public virtual void Reverse(); 42 public virtual void SetRange(int index, ICollection c); 43 public virtual void Sort(int index, int count, IComparer comparer); 44 public virtual void Sort(IComparer comparer); 45 public virtual void Sort(); 46 public static ArrayList Synchronized(ArrayList list); 47 public virtual Array ToArray(Type type); 48 public virtual object[] ToArray(); 49 public virtual void TrimToSize(); 50 public virtual int Capacity { get; set; } 51 int ICollection.Count { get; } 52 public virtual int Count { get; } 53 bool IList.IsFixedSize { get; } 54 public virtual bool IsFixedSize { get; } 55 bool IList.IsReadOnly { get; } 56 public virtual bool IsReadOnly { get; } 57 bool ICollection.IsSynchronized { get; } 58 public virtual bool IsSynchronized { get; } 59 public virtual object this[int index] { get; set; } 60 object ICollection.SyncRoot { get; } 61 public virtual object SyncRoot { get; } 62 } 63 64 // Namespace: System, Library: BCL 65 public class ArrayTypeMismatchException: SystemException 66 { 67 public ArrayTypeMismatchException(); 68 public ArrayTypeMismatchException(string message); 69

Page 492: C# Language Specification

C# LANGUAGE SPECIFICATION

472

public ArrayTypeMismatchException(string message, Exception innerException); 1 } 2 3 // Namespace: System.Text, Library: BCL 4 public class ASCIIEncoding: Encoding 5 { 6 public ASCIIEncoding(); 7 public override int GetByteCount(char[] chars, int index, int count); 8 public override int GetByteCount(string chars); 9 public override int GetBytes(string chars, int charIndex, int charCount, 10 byte[] bytes, int byteIndex); 11 public override int GetBytes(char[] chars, int charIndex, int charCount, 12 byte[] bytes, int byteIndex); 13 public override int GetCharCount(byte[] bytes, int index, int count); 14 public override int GetChars(byte[] bytes, int byteIndex, int byteCount, 15 char[] chars, int charIndex); 16 public override int GetMaxByteCount(int charCount); 17 public override int GetMaxCharCount(int byteCount); 18 public override string GetString(byte[] bytes, int byteIndex, int 19 byteCount); 20 public override string GetString(byte[] bytes); 21 } 22 23 // Namespace: System, Library: BCL 24 public delegate void AsyncCallback(IAsyncResult ar); 25 26 // Namespace: System, Library: BCL 27 public abstract class Attribute 28 { 29 protected Attribute(); 30 public override bool Equals(object obj); 31 public override int GetHashCode(); 32 } 33 34 // Namespace: System, Library: BCL 35 public enum AttributeTargets 36 { 37 All = Assembly | 0x2 | Class | Struct | Enum | Constructor | Method | 38 Property | Field | Event | Interface | Parameter | Delegate | 39 ReturnValue, 40 Assembly = 0x1, 41 Class = 0x4, 42 Constructor = 0x20, 43 Delegate = 0x1000, 44 Enum = 0x10, 45 Event = 0x200, 46 Field = 0x100, 47 Interface = 0x400, 48 Method = 0x40, 49 Parameter = 0x800, 50 Property = 0x80, 51 ReturnValue = 0x2000, 52 Struct = 0x8, 53 } 54 55 // Namespace: System, Library: BCL 56 public sealed class AttributeUsageAttribute: Attribute 57 { 58 public AttributeUsageAttribute(AttributeTargets validOn); 59 public bool AllowMultiple { get; set; } 60 public bool Inherited { get; set; } 61 public AttributeTargets ValidOn { get; } 62 } 63 64 // Namespace: System, Library: BCL 65 public struct Boolean: IComparable 66 { 67 public static readonly string FalseString; 68 public static readonly string TrueString; 69

Page 493: C# Language Specification

Annex D Standard Library

473

public int CompareTo(object obj); 1 public override bool Equals(object obj); 2 public override int GetHashCode(); 3 public static bool Parse(string value); 4 public string ToString(IFormatProvider provider); 5 public override string ToString(); 6 } 7 8 // Namespace: System, Library: BCL 9 public struct Byte: IComparable, IFormattable 10 { 11 public const byte MaxValue = 255; 12 public const byte MinValue = 0; 13 public int CompareTo(object value); 14 public override bool Equals(object obj); 15 public override int GetHashCode(); 16 public static byte Parse(string s); 17 public static byte Parse(string s, NumberStyles style); 18 public static byte Parse(string s, IFormatProvider provider); 19 public static byte Parse(string s, NumberStyles style, IFormatProvider 20 provider); 21 public string ToString(IFormatProvider provider); 22 public string ToString(string format, IFormatProvider provider); 23 public override string ToString(); 24 public string ToString(string format); 25 } 26 27 // Namespace: System, Library: BCL 28 public struct Char: IComparable 29 { 30 public const char MaxValue = (char)0xFFFF; 31 public const char MinValue = (char)0x0; 32 public int CompareTo(object value); 33 public override bool Equals(object obj); 34 public override int GetHashCode(); 35 public static double GetNumericValue(char c); 36 public static double GetNumericValue(string s, int index); 37 public static UnicodeCategory GetUnicodeCategory(char c); 38 public static UnicodeCategory GetUnicodeCategory(string s, int index); 39 public static bool IsControl(char c); 40 public static bool IsControl(string s, int index); 41 public static bool IsDigit(char c); 42 public static bool IsDigit(string s, int index); 43 public static bool IsLetter(char c); 44 public static bool IsLetter(string s, int index); 45 public static bool IsLetterOrDigit(char c); 46 public static bool IsLetterOrDigit(string s, int index); 47 public static bool IsLower(char c); 48 public static bool IsLower(string s, int index); 49 public static bool IsNumber(char c); 50 public static bool IsNumber(string s, int index); 51 public static bool IsPunctuation(char c); 52 public static bool IsPunctuation(string s, int index); 53 public static bool IsSeparator(char c); 54 public static bool IsSeparator(string s, int index); 55 public static bool IsSurrogate(char c); 56 public static bool IsSurrogate(string s, int index); 57 public static bool IsSymbol(char c); 58 public static bool IsSymbol(string s, int index); 59 public static bool IsUpper(char c); 60 public static bool IsUpper(string s, int index); 61 public static bool IsWhiteSpace(char c); 62 public static bool IsWhiteSpace(string s, int index); 63 public static char Parse(string s); 64 public static char ToLower(char c); 65 public string ToString(IFormatProvider provider); 66 public override string ToString(); 67 public static char ToUpper(char c); 68 } 69

Page 494: C# Language Specification

C# LANGUAGE SPECIFICATION

474

1 // Namespace: System, Library: BCL 2 public sealed class CharEnumerator: ICloneable, IEnumerator 3 { 4 public object Clone(); 5 public bool MoveNext(); 6 public void Reset(); 7 public char Current { get; } 8 object IEnumerator.Current { get; } 9 } 10 11 // Namespace: System, Library: BCL 12 public sealed class CLSCompliantAttribute: Attribute 13 { 14 public CLSCompliantAttribute(bool isCompliant); 15 public bool IsCompliant { get; } 16 } 17 18 // Namespace: System.Security, Library: BCL 19 public abstract class CodeAccessPermission: IPermission 20 { 21 protected CodeAccessPermission(); 22 public void Assert(); 23 public abstract IPermission Copy(); 24 public void Demand(); 25 public void Deny(); 26 public abstract void FromXml(SecurityElement elem); 27 public abstract IPermission Intersect(IPermission target); 28 public abstract bool IsSubsetOf(IPermission target); 29 public override string ToString(); 30 public abstract SecurityElement ToXml(); 31 public virtual IPermission Union(IPermission other); 32 } 33 34 // Namespace: System.Security.Permissions, Library: BCL 35 public abstract class CodeAccessSecurityAttribute: SecurityAttribute 36 { 37 protected CodeAccessSecurityAttribute(); 38 public CodeAccessSecurityAttribute(SecurityAction action); 39 } 40 41 // Namespace: System.Collections, Library: BCL 42 public sealed class Comparer: IComparer 43 { 44 public static readonly Comparer Default; 45 public int Compare(object a, object b); 46 } 47 48 // Namespace: System.Diagnostics, Library: BCL 49 public sealed class ConditionalAttribute: Attribute 50 { 51 public ConditionalAttribute(string conditionString); 52 public string ConditionString { get; } 53 } 54 55 // Namespace: System, Library: BCL 56 public sealed class Console 57 { 58 public static Stream OpenStandardError(); 59 public static Stream OpenStandardError(int bufferSize); 60 public static Stream OpenStandardInput(); 61 public static Stream OpenStandardInput(int bufferSize); 62 public static Stream OpenStandardOutput(); 63 public static Stream OpenStandardOutput(int bufferSize); 64 public static int Read(); 65 public static string ReadLine(); 66 public static void SetError(TextWriter newError); 67 public static void SetIn(TextReader newIn); 68 public static void SetOut(TextWriter newOut); 69

Page 495: C# Language Specification

Annex D Standard Library

475

public static void Write(string format, object arg0); 1 public static void Write(string format, object arg0, object arg1); 2 public static void Write(string format, object arg0, object arg1, object 3 arg2); 4 public static void Write(string format, params object[] arg); 5 public static void Write(bool value); 6 public static void Write(char value); 7 public static void Write(char[] buffer); 8 public static void Write(char[] buffer, int index, int count); 9 public static void Write(double value); 10 public static void Write(decimal value); 11 public static void Write(float value); 12 public static void Write(int value); 13 public static void Write(uint value); 14 public static void Write(long value); 15 public static void Write(ulong value); 16 public static void Write(object value); 17 public static void Write(string value); 18 public static void WriteLine(); 19 public static void WriteLine(bool value); 20 public static void WriteLine(char value); 21 public static void WriteLine(char[] buffer); 22 public static void WriteLine(char[] buffer, int index, int count); 23 public static void WriteLine(decimal value); 24 public static void WriteLine(double value); 25 public static void WriteLine(float value); 26 public static void WriteLine(int value); 27 public static void WriteLine(uint value); 28 public static void WriteLine(long value); 29 public static void WriteLine(ulong value); 30 public static void WriteLine(object value); 31 public static void WriteLine(string value); 32 public static void WriteLine(string format, object arg0); 33 public static void WriteLine(string format, object arg0, object arg1); 34 public static void WriteLine(string format, object arg0, object arg1, 35 object arg2); 36 public static void WriteLine(string format, params object[] arg); 37 public static TextWriter Error { get; } 38 public static TextReader In { get; } 39 public static TextWriter Out { get; } 40 } 41 42 // Namespace: System, Library: BCL 43 public sealed class Convert 44 { 45 public static bool ToBoolean(bool value); 46 public static bool ToBoolean(sbyte value); 47 public static bool ToBoolean(byte value); 48 public static bool ToBoolean(short value); 49 public static bool ToBoolean(ushort value); 50 public static bool ToBoolean(int value); 51 public static bool ToBoolean(uint value); 52 public static bool ToBoolean(long value); 53 public static bool ToBoolean(ulong value); 54 public static bool ToBoolean(string value); 55 public static bool ToBoolean(float value); 56 public static bool ToBoolean(double value); 57 public static bool ToBoolean(decimal value); 58 public static byte ToByte(bool value); 59 public static byte ToByte(byte value); 60 public static byte ToByte(char value); 61 public static byte ToByte(sbyte value); 62 public static byte ToByte(short value); 63 public static byte ToByte(ushort value); 64 public static byte ToByte(int value); 65 public static byte ToByte(uint value); 66 public static byte ToByte(long value); 67 public static byte ToByte(ulong value); 68 public static byte ToByte(float value); 69

Page 496: C# Language Specification

C# LANGUAGE SPECIFICATION

476

public static byte ToByte(double value); 1 public static byte ToByte(decimal value); 2 public static byte ToByte(string value); 3 public static byte ToByte(string value, IFormatProvider provider); 4 public static char ToChar(char value); 5 public static char ToChar(sbyte value); 6 public static char ToChar(byte value); 7 public static char ToChar(short value); 8 public static char ToChar(ushort value); 9 public static char ToChar(int value); 10 public static char ToChar(uint value); 11 public static char ToChar(long value); 12 public static char ToChar(ulong value); 13 public static char ToChar(string value); 14 public static DateTime ToDateTime(DateTime value); 15 public static DateTime ToDateTime(string value); 16 public static DateTime ToDateTime(string value, IFormatProvider provider); 17 public static decimal ToDecimal(sbyte value); 18 public static decimal ToDecimal(byte value); 19 public static decimal ToDecimal(short value); 20 public static decimal ToDecimal(ushort value); 21 public static decimal ToDecimal(int value); 22 public static decimal ToDecimal(uint value); 23 public static decimal ToDecimal(long value); 24 public static decimal ToDecimal(ulong value); 25 public static decimal ToDecimal(float value); 26 public static decimal ToDecimal(double value); 27 public static decimal ToDecimal(string value); 28 public static decimal ToDecimal(string value, IFormatProvider provider); 29 public static decimal ToDecimal(decimal value); 30 public static decimal ToDecimal(bool value); 31 public static double ToDouble(sbyte value); 32 public static double ToDouble(byte value); 33 public static double ToDouble(short value); 34 public static double ToDouble(ushort value); 35 public static double ToDouble(int value); 36 public static double ToDouble(uint value); 37 public static double ToDouble(long value); 38 public static double ToDouble(ulong value); 39 public static double ToDouble(float value); 40 public static double ToDouble(double value); 41 public static double ToDouble(decimal value); 42 public static double ToDouble(string value); 43 public static double ToDouble(string value, IFormatProvider provider); 44 public static double ToDouble(bool value); 45 public static short ToInt16(bool value); 46 public static short ToInt16(char value); 47 public static short ToInt16(sbyte value); 48 public static short ToInt16(byte value); 49 public static short ToInt16(ushort value); 50 public static short ToInt16(int value); 51 public static short ToInt16(uint value); 52 public static short ToInt16(short value); 53 public static short ToInt16(long value); 54 public static short ToInt16(ulong value); 55 public static short ToInt16(float value); 56 public static short ToInt16(double value); 57 public static short ToInt16(decimal value); 58 public static short ToInt16(string value); 59 public static short ToInt16(string value, IFormatProvider provider); 60 public static int ToInt32(bool value); 61 public static int ToInt32(char value); 62 public static int ToInt32(sbyte value); 63 public static int ToInt32(byte value); 64 public static int ToInt32(short value); 65 public static int ToInt32(ushort value); 66 public static int ToInt32(uint value); 67 public static int ToInt32(int value); 68 public static int ToInt32(long value); 69

Page 497: C# Language Specification

Annex D Standard Library

477

public static int ToInt32(ulong value); 1 public static int ToInt32(float value); 2 public static int ToInt32(double value); 3 public static int ToInt32(decimal value); 4 public static int ToInt32(string value); 5 public static int ToInt32(string value, IFormatProvider provider); 6 public static long ToInt64(bool value); 7 public static long ToInt64(char value); 8 public static long ToInt64(sbyte value); 9 public static long ToInt64(byte value); 10 public static long ToInt64(short value); 11 public static long ToInt64(ushort value); 12 public static long ToInt64(int value); 13 public static long ToInt64(uint value); 14 public static long ToInt64(ulong value); 15 public static long ToInt64(long value); 16 public static long ToInt64(float value); 17 public static long ToInt64(double value); 18 public static long ToInt64(decimal value); 19 public static long ToInt64(string value); 20 public static long ToInt64(string value, IFormatProvider provider); 21 public static sbyte ToSByte(bool value); 22 public static sbyte ToSByte(sbyte value); 23 public static sbyte ToSByte(char value); 24 public static sbyte ToSByte(byte value); 25 public static sbyte ToSByte(short value); 26 public static sbyte ToSByte(ushort value); 27 public static sbyte ToSByte(int value); 28 public static sbyte ToSByte(uint value); 29 public static sbyte ToSByte(long value); 30 public static sbyte ToSByte(ulong value); 31 public static sbyte ToSByte(float value); 32 public static sbyte ToSByte(double value); 33 public static sbyte ToSByte(decimal value); 34 public static sbyte ToSByte(string value); 35 public static sbyte ToSByte(string value, IFormatProvider provider); 36 public static float ToSingle(sbyte value); 37 public static float ToSingle(byte value); 38 public static float ToSingle(short value); 39 public static float ToSingle(ushort value); 40 public static float ToSingle(int value); 41 public static float ToSingle(uint value); 42 public static float ToSingle(long value); 43 public static float ToSingle(ulong value); 44 public static float ToSingle(float value); 45 public static float ToSingle(double value); 46 public static float ToSingle(decimal value); 47 public static float ToSingle(string value); 48 public static float ToSingle(string value, IFormatProvider provider); 49 public static float ToSingle(bool value); 50 public static string ToString(bool value); 51 public static string ToString(char value); 52 public static string ToString(sbyte value); 53 public static string ToString(sbyte value, IFormatProvider provider); 54 public static string ToString(byte value); 55 public static string ToString(byte value, IFormatProvider provider); 56 public static string ToString(short value); 57 public static string ToString(short value, IFormatProvider provider); 58 public static string ToString(ushort value); 59 public static string ToString(ushort value, IFormatProvider provider); 60 public static string ToString(int value); 61 public static string ToString(int value, IFormatProvider provider); 62 public static string ToString(uint value); 63 public static string ToString(uint value, IFormatProvider provider); 64 public static string ToString(long value); 65 public static string ToString(long value, IFormatProvider provider); 66 public static string ToString(ulong value); 67 public static string ToString(ulong value, IFormatProvider provider); 68 public static string ToString(float value); 69

Page 498: C# Language Specification

C# LANGUAGE SPECIFICATION

478

public static string ToString(float value, IFormatProvider provider); 1 public static string ToString(double value); 2 public static string ToString(double value, IFormatProvider provider); 3 public static string ToString(decimal value); 4 public static string ToString(decimal value, IFormatProvider provider); 5 public static string ToString(DateTime value); 6 public static string ToString(DateTime value, IFormatProvider provider); 7 public static string ToString(string value); 8 public static ushort ToUInt16(bool value); 9 public static ushort ToUInt16(char value); 10 public static ushort ToUInt16(sbyte value); 11 public static ushort ToUInt16(byte value); 12 public static ushort ToUInt16(short value); 13 public static ushort ToUInt16(int value); 14 public static ushort ToUInt16(ushort value); 15 public static ushort ToUInt16(uint value); 16 public static ushort ToUInt16(long value); 17 public static ushort ToUInt16(ulong value); 18 public static ushort ToUInt16(float value); 19 public static ushort ToUInt16(double value); 20 public static ushort ToUInt16(decimal value); 21 public static ushort ToUInt16(string value); 22 public static ushort ToUInt16(string value, IFormatProvider provider); 23 public static uint ToUInt32(bool value); 24 public static uint ToUInt32(char value); 25 public static uint ToUInt32(sbyte value); 26 public static uint ToUInt32(byte value); 27 public static uint ToUInt32(short value); 28 public static uint ToUInt32(ushort value); 29 public static uint ToUInt32(int value); 30 public static uint ToUInt32(uint value); 31 public static uint ToUInt32(long value); 32 public static uint ToUInt32(ulong value); 33 public static uint ToUInt32(float value); 34 public static uint ToUInt32(double value); 35 public static uint ToUInt32(decimal value); 36 public static uint ToUInt32(string value); 37 public static uint ToUInt32(string value, IFormatProvider provider); 38 public static ulong ToUInt64(bool value); 39 public static ulong ToUInt64(char value); 40 public static ulong ToUInt64(sbyte value); 41 public static ulong ToUInt64(byte value); 42 public static ulong ToUInt64(short value); 43 public static ulong ToUInt64(ushort value); 44 public static ulong ToUInt64(int value); 45 public static ulong ToUInt64(uint value); 46 public static ulong ToUInt64(long value); 47 public static ulong ToUInt64(ulong value); 48 public static ulong ToUInt64(float value); 49 public static ulong ToUInt64(double value); 50 public static ulong ToUInt64(decimal value); 51 public static ulong ToUInt64(string value); 52 public static ulong ToUInt64(string value, IFormatProvider provider); 53 } 54 55 // Namespace: System, Library: BCL 56 public struct DateTime: IComparable, IFormattable 57 { 58 public DateTime(long ticks); 59 public DateTime(int year, int month, int day); 60 public DateTime(int year, int month, int day, int hour, int minute, int 61 second); 62 public DateTime(int year, int month, int day, int hour, int minute, int 63 second, int millisecond); 64 public static readonly DateTime MaxValue; 65 public static readonly DateTime MinValue; 66 public DateTime Add(TimeSpan value); 67 public DateTime AddDays(double value); 68 public DateTime AddHours(double value); 69

Page 499: C# Language Specification

Annex D Standard Library

479

public DateTime AddMilliseconds(double value); 1 public DateTime AddMinutes(double value); 2 public DateTime AddMonths(int months); 3 public DateTime AddSeconds(double value); 4 public DateTime AddTicks(long value); 5 public DateTime AddYears(int value); 6 public static int Compare(DateTime t1, DateTime t2); 7 public int CompareTo(object value); 8 public static int DaysInMonth(int year, int month); 9 public override bool Equals(object value); 10 public static bool Equals(DateTime t1, DateTime t2); 11 public override int GetHashCode(); 12 public static bool IsLeapYear(int year); 13 public static DateTime operator +(DateTime d, TimeSpan t); 14 public static bool operator ==(DateTime d1, DateTime d2); 15 public static bool operator >(DateTime t1, DateTime t2); 16 public static bool operator >=(DateTime t1, DateTime t2); 17 public static bool operator !=(DateTime d1, DateTime d2); 18 public static bool operator <(DateTime t1, DateTime t2); 19 public static bool operator <=(DateTime t1, DateTime t2); 20 public static DateTime operator -(DateTime d, TimeSpan t); 21 public static TimeSpan operator -(DateTime d1, DateTime d2); 22 public static DateTime Parse(string s); 23 public static DateTime Parse(string s, IFormatProvider provider); 24 public static DateTime Parse(string s, IFormatProvider provider, 25 DateTimeStyles styles); 26 public static DateTime ParseExact(string s, string format, IFormatProvider 27 provider); 28 public static DateTime ParseExact(string s, string format, IFormatProvider 29 provider, DateTimeStyles style); 30 public static DateTime ParseExact(string s, string[] formats, 31 IFormatProvider provider, DateTimeStyles style); 32 public TimeSpan Subtract(DateTime value); 33 public DateTime Subtract(TimeSpan value); 34 public DateTime ToLocalTime(); 35 public string ToLongDateString(); 36 public string ToLongTimeString(); 37 public string ToShortDateString(); 38 public string ToShortTimeString(); 39 public string ToString(IFormatProvider provider); 40 public string ToString(string format, IFormatProvider provider); 41 public override string ToString(); 42 public string ToString(string format); 43 public DateTime ToUniversalTime(); 44 public DateTime Date { get; } 45 public int Day { get; } 46 public int DayOfYear { get; } 47 public int Hour { get; } 48 public int Millisecond { get; } 49 public int Minute { get; } 50 public int Month { get; } 51 public static DateTime Now { get; } 52 public int Second { get; } 53 public long Ticks { get; } 54 public TimeSpan TimeOfDay { get; } 55 public static DateTime Today { get; } 56 public static DateTime UtcNow { get; } 57 public int Year { get; } 58 } 59 60 // Namespace: System.Globalization, Library: BCL 61 public sealed class DateTimeFormatInfo: ICloneable, IFormatProvider 62 { 63 public DateTimeFormatInfo(); 64 public object Clone(); 65 public string GetAbbreviatedMonthName(int month); 66 public int GetEra(string eraName); 67 public string GetEraName(int era); 68 public object GetFormat(Type formatType); 69

Page 500: C# Language Specification

C# LANGUAGE SPECIFICATION

480

public string GetMonthName(int month); 1 public static DateTimeFormatInfo ReadOnly(DateTimeFormatInfo dtfi); 2 public string[] AbbreviatedDayNames { get; set; } 3 public string[] AbbreviatedMonthNames { get; set; } 4 public string AMDesignator { get; set; } 5 public static DateTimeFormatInfo CurrentInfo { get; } 6 public string DateSeparator { get; set; } 7 public string[] DayNames { get; set; } 8 public string FullDateTimePattern { get; set; } 9 public static DateTimeFormatInfo InvariantInfo { get; } 10 public bool IsReadOnly { get; } 11 public string LongDatePattern { get; set; } 12 public string LongTimePattern { get; set; } 13 public string MonthDayPattern { get; set; } 14 public string[] MonthNames { get; set; } 15 public string PMDesignator { get; set; } 16 public string ShortDatePattern { get; set; } 17 public string ShortTimePattern { get; set; } 18 public string TimeSeparator { get; set; } 19 public string YearMonthPattern { get; set; } 20 } 21 22 // Namespace: System.Globalization, Library: BCL 23 public enum DateTimeStyles 24 { 25 AdjustToUniversal = 0x10, 26 AllowInnerWhite = 0x4, 27 AllowLeadingWhite = 0x1, 28 AllowTrailingWhite = 0x2, 29 AllowWhiteSpaces = AllowLeadingWhite | AllowTrailingWhite | AllowInnerWhite, 30 NoCurrentDateDefault = 0x8, 31 None = 0x0, 32 } 33 34 // Namespace: System, Library: ExtendedNumerics 35 public struct Decimal: IComparable, IFormattable 36 { 37 public Decimal(int value); 38 public Decimal(uint value); 39 public Decimal(long value); 40 public Decimal(ulong value); 41 public Decimal(float value); 42 public Decimal(double value); 43 public Decimal(int[] bits); 44 public static readonly decimal MaxValue; 45 public static readonly decimal MinusOne; 46 public static readonly decimal MinValue; 47 public static readonly decimal One; 48 public static readonly decimal Zero; 49 public static decimal Add(decimal d1, decimal d2); 50 public static int Compare(decimal d1, decimal d2); 51 public int CompareTo(object value); 52 public static decimal Divide(decimal d1, decimal d2); 53 public override bool Equals(object value); 54 public static bool Equals(decimal d1, decimal d2); 55 public static decimal Floor(decimal d); 56 public static int[] GetBits(decimal d); 57 public override int GetHashCode(); 58 public static decimal Multiply(decimal d1, decimal d2); 59 public static decimal Negate(decimal d); 60 public static Decimal operator +(Decimal d1, Decimal d2); 61 public static Decimal operator --(Decimal d); 62 public static Decimal operator /(Decimal d1, Decimal d2); 63 public static bool operator ==(Decimal d1, Decimal d2); 64 public static explicit operator Decimal(float value); 65 public static explicit operator Decimal(double value); 66 public static explicit operator byte(Decimal value); 67 public static explicit operator sbyte(Decimal value); 68 public static explicit operator char(Decimal value); 69

Page 501: C# Language Specification

Annex D Standard Library

481

public static explicit operator short(Decimal value); 1 public static explicit operator ushort(Decimal value); 2 public static explicit operator int(Decimal value); 3 public static explicit operator uint(Decimal value); 4 public static explicit operator long(Decimal value); 5 public static explicit operator ulong(Decimal value); 6 public static explicit operator float(Decimal value); 7 public static explicit operator double(Decimal value); 8 public static bool operator >(Decimal d1, Decimal d2); 9 public static bool operator >=(Decimal d1, Decimal d2); 10 public static implicit operator Decimal(byte value); 11 public static implicit operator Decimal(sbyte value); 12 public static implicit operator Decimal(short value); 13 public static implicit operator Decimal(ushort value); 14 public static implicit operator Decimal(char value); 15 public static implicit operator Decimal(int value); 16 public static implicit operator Decimal(uint value); 17 public static implicit operator Decimal(long value); 18 public static implicit operator Decimal(ulong value); 19 public static Decimal operator ++(Decimal d); 20 public static bool operator !=(Decimal d1, Decimal d2); 21 public static bool operator <(Decimal d1, Decimal d2); 22 public static bool operator <=(Decimal d1, Decimal d2); 23 public static Decimal operator %(Decimal d1, Decimal d2); 24 public static Decimal operator *(Decimal d1, Decimal d2); 25 public static Decimal operator -(Decimal d1, Decimal d2); 26 public static Decimal operator -(Decimal d); 27 public static Decimal operator +(Decimal d); 28 public static decimal Parse(string s); 29 public static decimal Parse(string s, NumberStyles style); 30 public static decimal Parse(string s, IFormatProvider provider); 31 public static decimal Parse(string s, NumberStyles style, IFormatProvider 32 provider); 33 public static decimal Remainder(decimal d1, decimal d2); 34 public static decimal Round(decimal d, int decimals); 35 public static decimal Subtract(decimal d1, decimal d2); 36 public string ToString(IFormatProvider provider); 37 public string ToString(string format, IFormatProvider provider); 38 public override string ToString(); 39 public string ToString(string format); 40 public static decimal Truncate(decimal d); 41 } 42 43 // Namespace: System.Text, Library: BCL 44 public abstract class Decoder 45 { 46 protected Decoder(); 47 public abstract int GetCharCount(byte[] bytes, int index, int count); 48 public abstract int GetChars(byte[] bytes, int byteIndex, int byteCount, 49 char[] chars, int charIndex); 50 } 51 52 // Namespace: System, Library: BCL 53 public abstract class Delegate: ICloneable 54 { 55 public virtual object Clone(); 56 public static Delegate Combine(Delegate a, Delegate b); 57 public static Delegate Combine(Delegate[] delegates); 58 public override bool Equals(object obj); 59 public override int GetHashCode(); 60 public virtual Delegate[] GetInvocationList(); 61 public static bool operator ==(Delegate d1, Delegate d2); 62 public static bool operator !=(Delegate d1, Delegate d2); 63 public static Delegate Remove(Delegate source, Delegate value); 64 public static Delegate RemoveAll(Delegate source, Delegate value); 65 public object Target { get; } 66 } 67 68 // Namespace: System.Collections, Library: BCL 69

Page 502: C# Language Specification

C# LANGUAGE SPECIFICATION

482

public struct DictionaryEntry 1 { 2 public DictionaryEntry(object key, object value); 3 public object Key { get; set; } 4 public object Value { get; set; } 5 } 6 7 // Namespace: System.IO, Library: BCL 8 public sealed class Directory 9 { 10 public static void Delete(string path); 11 public static void Delete(string path, bool recursive); 12 public static bool Exists(string path); 13 public static DateTime GetCreationTime(string path); 14 public static string GetCurrentDirectory(); 15 public static string[] GetDirectories(string path); 16 public static string[] GetDirectories(string path, string searchPattern); 17 public static string GetDirectoryRoot(string path); 18 public static string[] GetFiles(string path); 19 public static string[] GetFiles(string path, string searchPattern); 20 public static string[] GetFileSystemEntries(string path); 21 public static string[] GetFileSystemEntries(string path, string 22 searchPattern); 23 public static DateTime GetLastAccessTime(string path); 24 public static DateTime GetLastWriteTime(string path); 25 public static void Move(string sourceDirName, string destDirName); 26 public static void SetCreationTime(string path, DateTime creationTime); 27 public static void SetCurrentDirectory(string path); 28 public static void SetLastAccessTime(string path, DateTime lastAccessTime); 29 public static void SetLastWriteTime(string path, DateTime lastWriteTime); 30 } 31 32 // Namespace: System.IO, Library: BCL 33 public class DirectoryNotFoundException: IOException 34 { 35 public DirectoryNotFoundException(); 36 public DirectoryNotFoundException(string message); 37 public DirectoryNotFoundException(string message, Exception innerException); 38 } 39 40 // Namespace: System, Library: BCL 41 public class DivideByZeroException: ArithmeticException 42 { 43 public DivideByZeroException(); 44 public DivideByZeroException(string message); 45 public DivideByZeroException(string message, Exception innerException); 46 } 47 48 // Namespace: System, Library: ExtendedNumerics 49 public struct Double: IComparable, IFormattable 50 { 51 public const double Epsilon = 4.9406564584124654e-324; 52 public const double MaxValue = 1.7976931348623157e+308; 53 public const double MinValue = -1.7976931348623157e+308; 54 public const double NaN = (double)0.0 / (double)0.0; 55 public const double NegativeInfinity = (double)-1.0 / (double)(0.0); 56 public const double PositiveInfinity = (double)1.0 / (double)(0.0); 57 public int CompareTo(object value); 58 public override bool Equals(object obj); 59 public override int GetHashCode(); 60 public static bool IsInfinity(double d); 61 public static bool IsNaN(double d); 62 public static bool IsNegativeInfinity(double d); 63 public static bool IsPositiveInfinity(double d); 64 public static double Parse(string s); 65 public static double Parse(string s, NumberStyles style); 66 public static double Parse(string s, IFormatProvider provider); 67 public static double Parse(string s, NumberStyles style, IFormatProvider 68 provider); 69

Page 503: C# Language Specification

Annex D Standard Library

483

public string ToString(IFormatProvider provider); 1 public string ToString(string format, IFormatProvider provider); 2 public override string ToString(); 3 public string ToString(string format); 4 } 5 6 // Namespace: System, Library: BCL 7 public class DuplicateWaitObjectException: ArgumentException 8 { 9 public DuplicateWaitObjectException(); 10 public DuplicateWaitObjectException(string parameterName); 11 public DuplicateWaitObjectException(string parameterName, string message); 12 } 13 14 // Namespace: System.Text, Library: BCL 15 public abstract class Encoder 16 { 17 protected Encoder(); 18 public abstract int GetByteCount(char[] chars, int index, int count, bool 19 flush); 20 public abstract int GetBytes(char[] chars, int charIndex, int charCount, 21 byte[] bytes, int byteIndex, bool flush); 22 } 23 24 // Namespace: System.Text, Library: BCL 25 public abstract class Encoding 26 { 27 protected Encoding(); 28 public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, 29 byte[] bytes); 30 public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, 31 byte[] bytes, int index, int count); 32 public override bool Equals(object value); 33 public abstract int GetByteCount(char[] chars, int index, int count); 34 public virtual int GetByteCount(string s); 35 public virtual int GetByteCount(char[] chars); 36 public virtual int GetBytes(string s, int charIndex, int charCount, byte[] 37 bytes, int byteIndex); 38 public virtual byte[] GetBytes(string s); 39 public abstract int GetBytes(char[] chars, int charIndex, int charCount, 40 byte[] bytes, int byteIndex); 41 public virtual byte[] GetBytes(char[] chars, int index, int count); 42 public virtual byte[] GetBytes(char[] chars); 43 public abstract int GetCharCount(byte[] bytes, int index, int count); 44 public virtual int GetCharCount(byte[] bytes); 45 public abstract int GetChars(byte[] bytes, int byteIndex, int byteCount, 46 char[] chars, int charIndex); 47 public virtual char[] GetChars(byte[] bytes, int index, int count); 48 public virtual char[] GetChars(byte[] bytes); 49 public virtual Decoder GetDecoder(); 50 public virtual Encoder GetEncoder(); 51 public override int GetHashCode(); 52 public abstract int GetMaxByteCount(int charCount); 53 public abstract int GetMaxCharCount(int byteCount); 54 public virtual byte[] GetPreamble(); 55 public virtual string GetString(byte[] bytes, int index, int count); 56 public virtual string GetString(byte[] bytes); 57 public static Encoding ASCII { get; } 58 public static Encoding BigEndianUnicode { get; } 59 public static Encoding Default { get; } 60 public static Encoding Unicode { get; } 61 public static Encoding UTF8 { get; } 62 } 63 64 // Namespace: System.IO, Library: BCL 65 public class EndOfStreamException: IOException 66 { 67 public EndOfStreamException(); 68 public EndOfStreamException(string message); 69

Page 504: C# Language Specification

C# LANGUAGE SPECIFICATION

484

public EndOfStreamException(string message, Exception innerException); 1 } 2 3 // Namespace: System, Library: BCL 4 public abstract class Enum: ValueType, IComparable, IFormattable 5 { 6 public int CompareTo(object target); 7 public override bool Equals(object obj); 8 public static string Format(Type enumType, object value, string format); 9 public override int GetHashCode(); 10 public static string GetName(Type enumType, object value); 11 public static string[] GetNames(Type enumType); 12 public static Type GetUnderlyingType(Type enumType); 13 public static Array GetValues(Type enumType); 14 public static bool IsDefined(Type enumType, object value); 15 public static object Parse(Type enumType, string value); 16 public static object Parse(Type enumType, string value, bool ignoreCase); 17 public static object ToObject(Type enumType, object value); 18 public static object ToObject(Type enumType, sbyte value); 19 public static object ToObject(Type enumType, short value); 20 public static object ToObject(Type enumType, int value); 21 public static object ToObject(Type enumType, byte value); 22 public static object ToObject(Type enumType, ushort value); 23 public static object ToObject(Type enumType, uint value); 24 public static object ToObject(Type enumType, long value); 25 public static object ToObject(Type enumType, ulong value); 26 public string ToString(IFormatProvider provider); 27 public string ToString(string format, IFormatProvider provider); 28 public override string ToString(); 29 public string ToString(string format); 30 } 31 32 // Namespace: System, Library: BCL 33 public sealed class Environment 34 { 35 public static void Exit(int exitCode); 36 public static string[] GetCommandLineArgs(); 37 public static string GetEnvironmentVariable(string variable); 38 public static IDictionary GetEnvironmentVariables(); 39 public static string CommandLine { get; } 40 public static int ExitCode { get; set; } 41 public bool HasShutdownStarted { get; } 42 public static string NewLine { get; } 43 public static string StackTrace { get; } 44 public static int TickCount { get; } 45 public static Version Version { get; } 46 } 47 48 // Namespace: System.Security.Permissions, Library: BCL 49 public sealed class EnvironmentPermission: CodeAccessPermission 50 { 51 public EnvironmentPermission(PermissionState state); 52 public EnvironmentPermission(EnvironmentPermissionAccess flag, string 53 pathList); 54 public override IPermission Copy(); 55 public override void FromXml(SecurityElement esd); 56 public override IPermission Intersect(IPermission target); 57 public override bool IsSubsetOf(IPermission target); 58 public override SecurityElement ToXml(); 59 public override IPermission Union(IPermission other); 60 } 61 62 // Namespace: System.Security.Permissions, Library: BCL 63 public enum EnvironmentPermissionAccess 64 { 65 AllAccess = Read | Write, 66 NoAccess = 0x0, 67 Read = 0x1, 68 Write = 0x2, 69

Page 505: C# Language Specification

Annex D Standard Library

485

} 1 2 // Namespace: System.Security.Permissions, Library: BCL 3 public sealed class EnvironmentPermissionAttribute: CodeAccessSecurityAttribute 4 { 5 public EnvironmentPermissionAttribute(SecurityAction action); 6 public override IPermission CreatePermission(); 7 public string All { set; } 8 public string Read { get; set; } 9 public string Write { get; set; } 10 } 11 12 // Namespace: System, Library: BCL 13 public class EventArgs 14 { 15 public EventArgs(); 16 public static readonly EventArgs Empty; 17 } 18 19 // Namespace: System, Library: BCL 20 public delegate void EventHandler(object sender, EventArgs e); 21 22 // Namespace: System, Library: BCL 23 public class Exception 24 { 25 public Exception(); 26 public Exception(string message); 27 public Exception(string message, Exception innerException); 28 public virtual Exception GetBaseException(); 29 public override string ToString(); 30 public Exception InnerException { get; } 31 public virtual string Message { get; } 32 public virtual string StackTrace { get; } 33 } 34 35 // Namespace: System, Library: BCL 36 public sealed class ExecutionEngineException: SystemException 37 { 38 public ExecutionEngineException(); 39 public ExecutionEngineException(string message); 40 public ExecutionEngineException(string message, Exception innerException); 41 } 42 43 // Namespace: System.IO, Library: BCL 44 public sealed class File 45 { 46 public static StreamWriter AppendText(string path); 47 public static void Copy(string sourceFileName, string destFileName); 48 public static void Copy(string sourceFileName, string destFileName, bool 49 overwrite); 50 public static FileStream Create(string path); 51 public static FileStream Create(string path, int bufferSize); 52 public static StreamWriter CreateText(string path); 53 public static void Delete(string path); 54 public static bool Exists(string path); 55 public static DateTime GetCreationTime(string path); 56 public static DateTime GetLastAccessTime(string path); 57 public static DateTime GetLastWriteTime(string path); 58 public static void Move(string sourceFileName, string destFileName); 59 public static FileStream Open(string path, FileMode mode); 60 public static FileStream Open(string path, FileMode mode, FileAccess 61 access); 62 public static FileStream Open(string path, FileMode mode, FileAccess 63 access, FileShare share); 64 public static FileStream OpenRead(string path); 65 public static StreamReader OpenText(string path); 66 public static FileStream OpenWrite(string path); 67 public static void SetCreationTime(string path, DateTime creationTime); 68 public static void SetLastAccessTime(string path, DateTime lastAccessTime); 69

Page 506: C# Language Specification

C# LANGUAGE SPECIFICATION

486

public static void SetLastWriteTime(string path, DateTime lastWriteTime); 1 } 2 3 // Namespace: System.IO, Library: BCL 4 public enum FileAccess 5 { 6 Read = 0x1, 7 ReadWrite = Read | Write, 8 Write = 0x2, 9 } 10 11 // Namespace: System.Security.Permissions, Library: BCL 12 public sealed class FileIOPermission: CodeAccessPermission 13 { 14 public FileIOPermission(PermissionState state); 15 public FileIOPermission(FileIOPermissionAccess access, string path); 16 public override IPermission Copy(); 17 public override void FromXml(SecurityElement esd); 18 public override IPermission Intersect(IPermission target); 19 public override bool IsSubsetOf(IPermission target); 20 public override SecurityElement ToXml(); 21 public override IPermission Union(IPermission other); 22 } 23 24 // Namespace: System.Security.Permissions, Library: BCL 25 public enum FileIOPermissionAccess 26 { 27 AllAccess = Read | Write | Append | PathDiscovery, 28 Append = 0x4, 29 NoAccess = 0x0, 30 PathDiscovery = 0x8, 31 Read = 0x1, 32 Write = 0x2, 33 } 34 35 // Namespace: System.Security.Permissions, Library: BCL 36 public sealed class FileIOPermissionAttribute: CodeAccessSecurityAttribute 37 { 38 public FileIOPermissionAttribute(SecurityAction action); 39 public override IPermission CreatePermission(); 40 public string All { set; } 41 public string Append { get; set; } 42 public string PathDiscovery { get; set; } 43 public string Read { get; set; } 44 public string Write { get; set; } 45 } 46 47 // Namespace: System.IO, Library: BCL 48 public class FileLoadException: IOException 49 { 50 public FileLoadException(); 51 public FileLoadException(string message); 52 public FileLoadException(string message, Exception inner); 53 public FileLoadException(string message, string fileName); 54 public FileLoadException(string message, string fileName, Exception inner); 55 public override string ToString(); 56 public string FileName { get; } 57 public override string Message { get; } 58 } 59 60 // Namespace: System.IO, Library: BCL 61 public enum FileMode 62 { 63 Append = 6, 64 Create = 2, 65 CreateNew = 1, 66 Open = 3, 67 OpenOrCreate = 4, 68 Truncate = 5, 69

Page 507: C# Language Specification

Annex D Standard Library

487

} 1 2 // Namespace: System.IO, Library: BCL 3 public class FileNotFoundException: IOException 4 { 5 public FileNotFoundException(); 6 public FileNotFoundException(string message); 7 public FileNotFoundException(string message, Exception innerException); 8 public FileNotFoundException(string message, string fileName); 9 public FileNotFoundException(string message, string fileName, Exception 10 innerException); 11 public override string ToString(); 12 public string FileName { get; } 13 public override string Message { get; } 14 } 15 16 // Namespace: System.IO, Library: BCL 17 public enum FileShare 18 { 19 None = 0x0, 20 Read = 0x1, 21 ReadWrite = Read | Write, 22 Write = 0x2, 23 } 24 25 // Namespace: System.IO, Library: BCL 26 public class FileStream: Stream 27 { 28 public FileStream(string path, FileMode mode); 29 public FileStream(string path, FileMode mode, FileAccess access); 30 public FileStream(string path, FileMode mode, FileAccess access, FileShare 31 share); 32 public FileStream(string path, FileMode mode, FileAccess access, FileShare 33 share, int bufferSize); 34 public FileStream(string path, FileMode mode, FileAccess access, FileShare 35 share, int bufferSize, bool useAsync); 36 public override IAsyncResult BeginRead(byte[] array, int offset, int 37 numBytes, AsyncCallback userCallback, object stateObject); 38 public override IAsyncResult BeginWrite(byte[] array, int offset, int 39 numBytes, AsyncCallback userCallback, object stateObject); 40 public override void Close(); 41 protected virtual void Dispose(bool disposing); 42 public override int EndRead(IAsyncResult asyncResult); 43 public override void EndWrite(IAsyncResult asyncResult); 44 ~FileStream(); 45 public override void Flush(); 46 public override int Read(byte[] array, int offset, int count); 47 public override int ReadByte(); 48 public override long Seek(long offset, SeekOrigin origin); 49 public override void SetLength(long value); 50 public override void Write(byte[] array, int offset, int count); 51 public override void WriteByte(byte value); 52 public override bool CanRead { get; } 53 public override bool CanSeek { get; } 54 public override bool CanWrite { get; } 55 public virtual bool IsAsync { get; } 56 public override long Length { get; } 57 public override long Position { get; set; } 58 } 59 60 // Namespace: System, Library: BCL 61 public class FlagsAttribute: Attribute 62 { 63 public FlagsAttribute(); 64 } 65 66 // Namespace: System, Library: BCL 67 public class FormatException: SystemException 68 { 69

Page 508: C# Language Specification

C# LANGUAGE SPECIFICATION

488

public FormatException(); 1 public FormatException(string message); 2 public FormatException(string message, Exception innerException); 3 } 4 5 // Namespace: System, Library: BCL 6 public sealed class GC 7 { 8 public static void KeepAlive(object obj); 9 public static void ReRegisterForFinalize(object obj); 10 public static void SuppressFinalize(object obj); 11 public static void WaitForPendingFinalizers(); 12 } 13 14 // Namespace: System.Collections, Library: BCL 15 public class Hashtable: ICloneable, ICollection, IDictionary, IEnumerable 16 { 17 public Hashtable(); 18 public Hashtable(int capacity); 19 public Hashtable(IHashCodeProvider hcp, IComparer comparer); 20 public Hashtable(int capacity, IHashCodeProvider hcp, IComparer comparer); 21 public Hashtable(IDictionary d); 22 public Hashtable(IDictionary d, IHashCodeProvider hcp, IComparer comparer); 23 public virtual void Add(object key, object value); 24 public virtual void Clear(); 25 public virtual object Clone(); 26 public virtual bool Contains(object key); 27 public virtual bool ContainsKey(object key); 28 public virtual bool ContainsValue(object value); 29 public virtual void CopyTo(Array array, int arrayIndex); 30 public virtual IDictionaryEnumerator GetEnumerator(); 31 protected virtual int GetHash(object key); 32 protected virtual bool KeyEquals(object item, object key); 33 public virtual void Remove(object key); 34 public static Hashtable Synchronized(Hashtable table); 35 IEnumerator IEnumerable.GetEnumerator(); 36 int ICollection.Count { get; } 37 public virtual int Count { get; } 38 bool IDictionary.IsFixedSize { get; } 39 public virtual bool IsFixedSize { get; } 40 bool IDictionary.IsReadOnly { get; } 41 public virtual bool IsReadOnly { get; } 42 bool ICollection.IsSynchronized { get; } 43 public virtual bool IsSynchronized { get; } 44 public virtual object this[object key] { get; set; } 45 ICollection IDictionary.Keys { get; } 46 public virtual ICollection Keys { get; } 47 object ICollection.SyncRoot { get; } 48 public virtual object SyncRoot { get; } 49 ICollection IDictionary.Values { get; } 50 public virtual ICollection Values { get; } 51 } 52 53 // Namespace: System, Library: BCL 54 public interface IAsyncResult 55 { 56 object AsyncState { get; } 57 WaitHandle AsyncWaitHandle { get; } 58 bool CompletedSynchronously { get; } 59 bool IsCompleted { get; } 60 } 61 62 // Namespace: System, Library: BCL 63 public interface ICloneable 64 { 65 object Clone(); 66 } 67 68 // Namespace: System.Collections, Library: BCL 69

Page 509: C# Language Specification

Annex D Standard Library

489

public interface ICollection: IEnumerable 1 { 2 void CopyTo(Array array, int index); 3 int Count { get; } 4 bool IsSynchronized { get; } 5 object SyncRoot { get; } 6 } 7 8 // Namespace: System, Library: BCL 9 public interface IComparable 10 { 11 int CompareTo(object obj); 12 } 13 14 // Namespace: System.Collections, Library: BCL 15 public interface IComparer 16 { 17 int Compare(object x, object y); 18 } 19 20 // Namespace: System.Collections, Library: BCL 21 public interface IDictionary: ICollection, IEnumerable 22 { 23 void Add(object key, object value); 24 void Clear(); 25 bool Contains(object key); 26 IDictionaryEnumerator GetEnumerator(); 27 void Remove(object key); 28 bool IsFixedSize { get; } 29 bool IsReadOnly { get; } 30 object this[object key] { get; set; } 31 ICollection Keys { get; } 32 ICollection Values { get; } 33 } 34 35 // Namespace: System.Collections, Library: BCL 36 public interface IDictionaryEnumerator: IEnumerator 37 { 38 DictionaryEntry Entry { get; } 39 object Key { get; } 40 object Value { get; } 41 } 42 43 // Namespace: System, Library: BCL 44 public interface IDisposable 45 { 46 void Dispose(); 47 } 48 49 // Namespace: System.Collections, Library: BCL 50 public interface IEnumerable 51 { 52 IEnumerator GetEnumerator(); 53 } 54 55 // Namespace: System.Collections, Library: BCL 56 public interface IEnumerator 57 { 58 bool MoveNext(); 59 void Reset(); 60 object Current { get; } 61 } 62 63 // Namespace: System, Library: BCL 64 public interface IFormatProvider 65 { 66 object GetFormat(Type formatType); 67 } 68 69

Page 510: C# Language Specification

C# LANGUAGE SPECIFICATION

490

// Namespace: System, Library: BCL 1 public interface IFormattable 2 { 3 string ToString(string format, IFormatProvider formatProvider); 4 } 5 6 // Namespace: System.Collections, Library: BCL 7 public interface IHashCodeProvider 8 { 9 int GetHashCode(object obj); 10 } 11 12 // Namespace: System.Collections, Library: BCL 13 public interface IList: ICollection, IEnumerable 14 { 15 int Add(object value); 16 void Clear(); 17 bool Contains(object value); 18 int IndexOf(object value); 19 void Insert(int index, object value); 20 void Remove(object value); 21 void RemoveAt(int index); 22 bool IsFixedSize { get; } 23 bool IsReadOnly { get; } 24 object this[int index] { get; set; } 25 } 26 27 // Namespace: System, Library: BCL 28 public sealed class IndexOutOfRangeException: SystemException 29 { 30 public IndexOutOfRangeException(); 31 public IndexOutOfRangeException(string message); 32 public IndexOutOfRangeException(string message, Exception innerException); 33 } 34 35 // Namespace: System, Library: BCL 36 public struct Int16: IComparable, IFormattable 37 { 38 public const short MaxValue = 32767; 39 public const short MinValue = -32768; 40 public int CompareTo(object value); 41 public override bool Equals(object obj); 42 public override int GetHashCode(); 43 public static short Parse(string s); 44 public static short Parse(string s, NumberStyles style); 45 public static short Parse(string s, IFormatProvider provider); 46 public static short Parse(string s, NumberStyles style, IFormatProvider 47 provider); 48 public string ToString(IFormatProvider provider); 49 public string ToString(string format, IFormatProvider provider); 50 public override string ToString(); 51 public string ToString(string format); 52 } 53 54 // Namespace: System, Library: BCL 55 public struct Int32: IComparable, IFormattable 56 { 57 public const int MaxValue = 2147483647; 58 public const int MinValue = -2147483648; 59 public int CompareTo(object value); 60 public override bool Equals(object obj); 61 public override int GetHashCode(); 62 public static int Parse(string s); 63 public static int Parse(string s, NumberStyles style); 64 public static int Parse(string s, IFormatProvider provider); 65 public static int Parse(string s, NumberStyles style, IFormatProvider 66 provider); 67 public string ToString(IFormatProvider provider); 68 public string ToString(string format, IFormatProvider provider); 69

Page 511: C# Language Specification

Annex D Standard Library

491

public override string ToString(); 1 public string ToString(string format); 2 } 3 4 // Namespace: System, Library: BCL 5 public struct Int64: IComparable, IFormattable 6 { 7 public const long MaxValue = 9223372036854775807; 8 public const long MinValue = -9223372036854775808; 9 public int CompareTo(object value); 10 public override bool Equals(object obj); 11 public override int GetHashCode(); 12 public static long Parse(string s); 13 public static long Parse(string s, NumberStyles style); 14 public static long Parse(string s, IFormatProvider provider); 15 public static long Parse(string s, NumberStyles style, IFormatProvider 16 provider); 17 public string ToString(IFormatProvider provider); 18 public string ToString(string format, IFormatProvider provider); 19 public override string ToString(); 20 public string ToString(string format); 21 } 22 23 // Namespace: System.Threading, Library: BCL 24 public sealed class Interlocked 25 { 26 public static int CompareExchange(ref int location1, int value, int 27 comparand); 28 public static float CompareExchange(ref float location1, float value, float 29 comparand); 30 public static object CompareExchange(ref object location1, object value, 31 object comparand); 32 public static int Decrement(ref int location); 33 public static long Decrement(ref long location); 34 public static int Exchange(ref int location1, int value); 35 public static float Exchange(ref float location1, float value); 36 public static object Exchange(ref object location1, object value); 37 public static int Increment(ref int location); 38 public static long Increment(ref long location); 39 } 40 41 // Namespace: System, Library: BCL 42 public class InvalidCastException: SystemException 43 { 44 public InvalidCastException(); 45 public InvalidCastException(string message); 46 public InvalidCastException(string message, Exception innerException); 47 } 48 49 // Namespace: System, Library: BCL 50 public class InvalidOperationException: SystemException 51 { 52 public InvalidOperationException(); 53 public InvalidOperationException(string message); 54 public InvalidOperationException(string message, Exception innerException); 55 } 56 57 // Namespace: System, Library: BCL 58 public sealed class InvalidProgramException: SystemException 59 { 60 public InvalidProgramException(); 61 public InvalidProgramException(string message); 62 public InvalidProgramException(string message, Exception inner); 63 } 64 65 // Namespace: System.IO, Library: BCL 66 public class IOException: SystemException 67 { 68 public IOException(); 69

Page 512: C# Language Specification

C# LANGUAGE SPECIFICATION

492

public IOException(string message); 1 public IOException(string message, Exception innerException); 2 } 3 4 // Namespace: System.Security, Library: BCL 5 public interface IPermission 6 { 7 IPermission Copy(); 8 void Demand(); 9 IPermission Intersect(IPermission target); 10 bool IsSubsetOf(IPermission target); 11 IPermission Union(IPermission target); 12 } 13 14 // Namespace: System, Library: BCL 15 public abstract class MarshalByRefObject 16 { 17 } 18 19 // Namespace: System, Library: ExtendedNumerics 20 public sealed class Math 21 { 22 public const double E = 2.71828182845905; 23 public const double PI = 3.14159265358979; 24 public static sbyte Abs(sbyte value); 25 public static short Abs(short value); 26 public static int Abs(int value); 27 public static long Abs(long value); 28 public static float Abs(float value); 29 public static double Abs(double value); 30 public static decimal Abs(decimal value); 31 public static double Acos(double d); 32 public static double Asin(double d); 33 public static double Atan(double d); 34 public static double Atan2(double y, double x); 35 public static long BigMul(int a, int b); 36 public static double Ceiling(double a); 37 public static double Cos(double d); 38 public static double Cosh(double value); 39 public static int DivRem(int a, int b, out int result); 40 public static long DivRem(long a, long b, out long result); 41 public static double Exp(double d); 42 public static double Floor(double d); 43 public static double IEEERemainder(double x, double y); 44 public static double Log(double d); 45 public static double Log(double a, double newBase); 46 public static double Log10(double d); 47 public static sbyte Max(sbyte val1, sbyte val2); 48 public static byte Max(byte val1, byte val2); 49 public static short Max(short val1, short val2); 50 public static ushort Max(ushort val1, ushort val2); 51 public static int Max(int val1, int val2); 52 public static uint Max(uint val1, uint val2); 53 public static long Max(long val1, long val2); 54 public static ulong Max(ulong val1, ulong val2); 55 public static float Max(float val1, float val2); 56 public static double Max(double val1, double val2); 57 public static decimal Max(decimal val1, decimal val2); 58 public static sbyte Min(sbyte val1, sbyte val2); 59 public static byte Min(byte val1, byte val2); 60 public static short Min(short val1, short val2); 61 public static ushort Min(ushort val1, ushort val2); 62 public static int Min(int val1, int val2); 63 public static uint Min(uint val1, uint val2); 64 public static long Min(long val1, long val2); 65 public static ulong Min(ulong val1, ulong val2); 66 public static float Min(float val1, float val2); 67 public static double Min(double val1, double val2); 68 public static decimal Min(decimal val1, decimal val2); 69

Page 513: C# Language Specification

Annex D Standard Library

493

public static double Pow(double x, double y); 1 public static double Round(double a); 2 public static double Round(double value, int digits); 3 public static decimal Round(decimal d); 4 public static int Sign(sbyte value); 5 public static int Sign(short value); 6 public static int Sign(int value); 7 public static int Sign(long value); 8 public static int Sign(float value); 9 public static int Sign(double value); 10 public static int Sign(decimal value); 11 public static double Sin(double a); 12 public static double Sinh(double value); 13 public static double Sqrt(double d); 14 public static double Tan(double a); 15 public static double Tanh(double value); 16 } 17 18 // Namespace: System.IO, Library: BCL 19 public class MemoryStream: Stream 20 { 21 public MemoryStream(); 22 public MemoryStream(int capacity); 23 public MemoryStream(byte[] buffer); 24 public MemoryStream(byte[] buffer, bool writable); 25 public MemoryStream(byte[] buffer, int index, int count); 26 public MemoryStream(byte[] buffer, int index, int count, bool writable); 27 public MemoryStream(byte[] buffer, int index, int count, bool writable, 28 bool publiclyVisible); 29 public override void Close(); 30 public override void Flush(); 31 public virtual byte[] GetBuffer(); 32 public override int Read(byte[] buffer, int offset, int count); 33 public override int ReadByte(); 34 public override long Seek(long offset, SeekOrigin loc); 35 public override void SetLength(long value); 36 public virtual byte[] ToArray(); 37 public override void Write(byte[] buffer, int offset, int count); 38 public override void WriteByte(byte value); 39 public virtual void WriteTo(Stream stream); 40 public override bool CanRead { get; } 41 public override bool CanSeek { get; } 42 public override bool CanWrite { get; } 43 public virtual int Capacity { get; set; } 44 public override long Length { get; } 45 public override long Position { get; set; } 46 } 47 48 // Namespace: System.Threading, Library: BCL 49 public sealed class Monitor 50 { 51 public static void Enter(object obj); 52 public static void Exit(object obj); 53 public static void Pulse(object obj); 54 public static void PulseAll(object obj); 55 public static bool TryEnter(object obj); 56 public static bool TryEnter(object obj, int millisecondsTimeout); 57 public static bool TryEnter(object obj, TimeSpan timeout); 58 public static bool Wait(object obj, int millisecondsTimeout); 59 public static bool Wait(object obj, TimeSpan timeout); 60 public static bool Wait(object obj); 61 } 62 63 // Namespace: System, Library: ExtendedNumerics 64 public class NotFiniteNumberException: ArithmeticException 65 { 66 public NotFiniteNumberException(); 67 public NotFiniteNumberException(double offendingNumber); 68 public NotFiniteNumberException(string message); 69

Page 514: C# Language Specification

C# LANGUAGE SPECIFICATION

494

public NotFiniteNumberException(string message, double offendingNumber); 1 public NotFiniteNumberException(string message, double offendingNumber, 2 Exception innerException); 3 public double OffendingNumber { get; } 4 } 5 6 // Namespace: System, Library: BCL 7 public class NotSupportedException: SystemException 8 { 9 public NotSupportedException(); 10 public NotSupportedException(string message); 11 public NotSupportedException(string message, Exception innerException); 12 } 13 14 // Namespace: System, Library: BCL 15 public class NullReferenceException: SystemException 16 { 17 public NullReferenceException(); 18 public NullReferenceException(string message); 19 public NullReferenceException(string message, Exception innerException); 20 } 21 22 // Namespace: System.Globalization, Library: BCL 23 public sealed class NumberFormatInfo: ICloneable, IFormatProvider 24 { 25 public NumberFormatInfo(); 26 public object Clone(); 27 public object GetFormat(Type formatType); 28 public static NumberFormatInfo ReadOnly(NumberFormatInfo nfi); 29 public int CurrencyDecimalDigits { get; set; } 30 public string CurrencyDecimalSeparator { get; set; } 31 public string CurrencyGroupSeparator { get; set; } 32 public int[] CurrencyGroupSizes { get; set; } 33 public int CurrencyNegativePattern { get; set; } 34 public int CurrencyPositivePattern { get; set; } 35 public string CurrencySymbol { get; set; } 36 public static NumberFormatInfo CurrentInfo { get; } 37 public static NumberFormatInfo InvariantInfo { get; } 38 public bool IsReadOnly { get; } 39 public string NaNSymbol { get; set; } 40 public string NegativeInfinitySymbol { get; set; } 41 public string NegativeSign { get; set; } 42 public int NumberDecimalDigits { get; set; } 43 public string NumberDecimalSeparator { get; set; } 44 public string NumberGroupSeparator { get; set; } 45 public int[] NumberGroupSizes { get; set; } 46 public int NumberNegativePattern { get; set; } 47 public int PercentDecimalDigits { get; set; } 48 public string PercentDecimalSeparator { get; set; } 49 public string PercentGroupSeparator { get; set; } 50 public int[] PercentGroupSizes { get; set; } 51 public int PercentNegativePattern { get; set; } 52 public int PercentPositivePattern { get; set; } 53 public string PercentSymbol { get; set; } 54 public string PerMilleSymbol { get; set; } 55 public string PositiveInfinitySymbol { get; set; } 56 public string PositiveSign { get; set; } 57 } 58 59 // Namespace: System.Globalization, Library: BCL 60 public enum NumberStyles 61 { 62 AllowCurrencySymbol = 0x100, 63 AllowDecimalPoint = 0x20, 64 AllowExponent = 0x80, 65 AllowHexSpecifier = 0x200, 66 AllowLeadingSign = 0x4, 67 AllowLeadingWhite = 0x1, 68 AllowParentheses = 0x10, 69

Page 515: C# Language Specification

Annex D Standard Library

495

AllowThousands = 0x40, 1 AllowTrailingSign = 0x8, 2 AllowTrailingWhite = 0x2, 3 Any = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | 4 AllowTrailingSign | AllowParentheses | AllowDecimalPoint | 5 AllowThousands | AllowExponent | AllowCurrencySymbol, 6 Currency = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | 7 AllowTrailingSign | AllowParentheses | AllowDecimalPoint | 8 AllowThousands | AllowCurrencySymbol, 9 Float = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | 10 AllowDecimalPoint | AllowExponent, 11 HexNumber = AllowLeadingWhite | AllowTrailingWhite | AllowHexSpecifier, 12 Integer = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign, 13 None = 0x0, 14 Number = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | 15 AllowTrailingSign | AllowDecimalPoint | AllowThousands, 16 } 17 18 // Namespace: System, Library: BCL 19 public class Object 20 { 21 public Object(); 22 public virtual bool Equals(object obj); 23 public static bool Equals(object objA, object objB); 24 ~Object(); 25 public virtual int GetHashCode(); 26 public Type GetType(); 27 protected object MemberwiseClone(); 28 public static bool ReferenceEquals(object objA, object objB); 29 public virtual string ToString(); 30 } 31 32 // Namespace: System, Library: BCL 33 public class ObjectDisposedException: InvalidOperationException 34 { 35 public ObjectDisposedException(string objectName); 36 public ObjectDisposedException(string objectName, string message); 37 public override string Message { get; } 38 public string ObjectName { get; } 39 } 40 41 // Namespace: System, Library: BCL 42 public sealed class ObsoleteAttribute: Attribute 43 { 44 public ObsoleteAttribute(); 45 public ObsoleteAttribute(string message); 46 public ObsoleteAttribute(string message, bool error); 47 public bool IsError { get; } 48 public string Message { get; } 49 } 50 51 // Namespace: System, Library: BCL 52 public class OutOfMemoryException: SystemException 53 { 54 public OutOfMemoryException(); 55 public OutOfMemoryException(string message); 56 public OutOfMemoryException(string message, Exception innerException); 57 } 58 59 // Namespace: System, Library: BCL 60 public class OverflowException: ArithmeticException 61 { 62 public OverflowException(); 63 public OverflowException(string message); 64 public OverflowException(string message, Exception innerException); 65 } 66 67 // Namespace: System.IO, Library: BCL 68 public sealed class Path 69

Page 516: C# Language Specification

C# LANGUAGE SPECIFICATION

496

{ 1 public static readonly char AltDirectorySeparatorChar; 2 public static readonly char DirectorySeparatorChar; 3 public static readonly char PathSeparator; 4 public static string ChangeExtension(string path, string extension); 5 public static string Combine(string path1, string path2); 6 public static string GetDirectoryName(string path); 7 public static string GetExtension(string path); 8 public static string GetFileName(string path); 9 public static string GetFileNameWithoutExtension(string path); 10 public static string GetFullPath(string path); 11 public static string GetPathRoot(string path); 12 public static string GetTempFileName(); 13 public static string GetTempPath(); 14 public static bool HasExtension(string path); 15 public static bool IsPathRooted(string path); 16 } 17 18 // Namespace: System.IO, Library: BCL 19 public class PathTooLongException: IOException 20 { 21 public PathTooLongException(); 22 public PathTooLongException(string message); 23 public PathTooLongException(string message, Exception innerException); 24 } 25 26 // Namespace: System.Security, Library: BCL 27 public class PermissionSet: ICollection, IEnumerable 28 { 29 public PermissionSet(PermissionState state); 30 public PermissionSet(PermissionSet permSet); 31 public virtual IPermission AddPermission(IPermission perm); 32 public virtual void Assert(); 33 public virtual PermissionSet Copy(); 34 public virtual void CopyTo(Array array, int index); 35 public virtual void Demand(); 36 public virtual void Deny(); 37 public virtual void FromXml(SecurityElement et); 38 public virtual IEnumerator GetEnumerator(); 39 public virtual bool IsSubsetOf(PermissionSet target); 40 public virtual void PermitOnly(); 41 public override string ToString(); 42 public virtual SecurityElement ToXml(); 43 public virtual PermissionSet Union(PermissionSet other); 44 int ICollection.Count { get; } 45 bool ICollection.IsSynchronized { get; } 46 object ICollection.SyncRoot { get; } 47 } 48 49 // Namespace: System.Security.Permissions, Library: BCL 50 public enum PermissionState 51 { 52 None = 0, 53 Unrestricted = 1, 54 } 55 56 // Namespace: System, Library: BCL 57 public class Random 58 { 59 public Random(); 60 public Random(int Seed); 61 public virtual int Next(int maxValue); 62 public virtual int Next(int minValue, int maxValue); 63 public virtual int Next(); 64 public virtual void NextBytes(byte[] buffer); 65 public virtual double NextDouble(); 66 } 67 68 // Namespace: System, Library: BCL 69

Page 517: C# Language Specification

Annex D Standard Library

497

public class RankException: SystemException 1 { 2 public RankException(); 3 public RankException(string message); 4 public RankException(string message, Exception innerException); 5 } 6 7 // Namespace: System, Library: BCL 8 public struct SByte: IComparable, IFormattable 9 { 10 public const sbyte MaxValue = 127; 11 public const sbyte MinValue = -128; 12 public int CompareTo(object obj); 13 public override bool Equals(object obj); 14 public override int GetHashCode(); 15 public static sbyte Parse(string s); 16 public static sbyte Parse(string s, NumberStyles style); 17 public static sbyte Parse(string s, IFormatProvider provider); 18 public static sbyte Parse(string s, NumberStyles style, IFormatProvider 19 provider); 20 public string ToString(IFormatProvider provider); 21 public string ToString(string format, IFormatProvider provider); 22 public override string ToString(); 23 public string ToString(string format); 24 } 25 26 // Namespace: System.Security.Permissions, Library: BCL 27 public enum SecurityAction 28 { 29 Assert = 3, 30 Demand = 2, 31 Deny = 4, 32 InheritanceDemand = 7, 33 LinkDemand = 6, 34 PermitOnly = 5, 35 RequestMinimum = 8, 36 RequestOptional = 9, 37 RequestRefuse = 10, 38 } 39 40 // Namespace: System.Security.Permissions, Library: BCL 41 public abstract class SecurityAttribute: Attribute 42 { 43 protected SecurityAttribute(); 44 public SecurityAttribute(SecurityAction action); 45 public abstract IPermission CreatePermission(); 46 public bool Unrestricted { get; set; } 47 } 48 49 // Namespace: System.Security, Library: BCL 50 public sealed class SecurityElement 51 { 52 public override string ToString(); 53 } 54 55 // Namespace: System.Security, Library: BCL 56 public class SecurityException: SystemException 57 { 58 public SecurityException(); 59 public SecurityException(string message); 60 public SecurityException(string message, Exception inner); 61 } 62 63 // Namespace: System.Security.Permissions, Library: BCL 64 public sealed class SecurityPermission: CodeAccessPermission 65 { 66 public SecurityPermission(PermissionState state); 67 public SecurityPermission(SecurityPermissionFlag flag); 68 public override IPermission Copy(); 69

Page 518: C# Language Specification

C# LANGUAGE SPECIFICATION

498

public override void FromXml(SecurityElement esd); 1 public override IPermission Intersect(IPermission target); 2 public override bool IsSubsetOf(IPermission target); 3 public override SecurityElement ToXml(); 4 public override IPermission Union(IPermission target); 5 } 6 7 // Namespace: System.Security.Permissions, Library: BCL 8 public sealed class SecurityPermissionAttribute: CodeAccessSecurityAttribute 9 { 10 public SecurityPermissionAttribute(SecurityAction action); 11 public override IPermission CreatePermission(); 12 public SecurityPermissionFlag Flags { get; set; } 13 } 14 15 // Namespace: System.Security.Permissions, Library: BCL 16 public enum SecurityPermissionFlag 17 { 18 Assertion = 0x1, 19 ControlThread = 0x10, 20 Execution = 0x8, 21 NoFlags = 0x0, 22 SkipVerification = 0x4, 23 UnmanagedCode = 0x2, 24 } 25 26 // Namespace: System.IO, Library: BCL 27 public enum SeekOrigin 28 { 29 Begin = 0, 30 Current = 1, 31 End = 2, 32 } 33 34 // Namespace: System, Library: ExtendedNumerics 35 public struct Single: IComparable, IFormattable 36 { 37 public const float Epsilon = (float)1.401298E-45; 38 public const float MaxValue = (float)3.402823E+38; 39 public const float MinValue = (float)-3.402823E+38; 40 public const float NaN = (float)0.0 / (float)0.0; 41 public const float NegativeInfinity = (float)-1.0 / (float)0.0; 42 public const float PositiveInfinity = (float)1.0 / (float)0.0; 43 public int CompareTo(object value); 44 public override bool Equals(object obj); 45 public override int GetHashCode(); 46 public static bool IsInfinity(float f); 47 public static bool IsNaN(float f); 48 public static bool IsNegativeInfinity(float f); 49 public static bool IsPositiveInfinity(float f); 50 public static float Parse(string s); 51 public static float Parse(string s, NumberStyles style); 52 public static float Parse(string s, IFormatProvider provider); 53 public static float Parse(string s, NumberStyles style, IFormatProvider 54 provider); 55 public string ToString(IFormatProvider provider); 56 public string ToString(string format, IFormatProvider provider); 57 public override string ToString(); 58 public string ToString(string format); 59 } 60 61 // Namespace: System, Library: BCL 62 public sealed class StackOverflowException: SystemException 63 { 64 public StackOverflowException(); 65 public StackOverflowException(string message); 66 public StackOverflowException(string message, Exception innerException); 67 } 68 69

Page 519: C# Language Specification

Annex D Standard Library

499

// Namespace: System.IO, Library: BCL 1 public abstract class Stream: MarshalByRefObject, IDisposable 2 { 3 protected Stream(); 4 public static readonly Stream Null; 5 public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, 6 AsyncCallback callback, object state); 7 public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int 8 count, AsyncCallback callback, object state); 9 public virtual void Close(); 10 protected virtual WaitHandle CreateWaitHandle(); 11 public virtual int EndRead(IAsyncResult asyncResult); 12 public virtual void EndWrite(IAsyncResult asyncResult); 13 public abstract void Flush(); 14 public abstract int Read(byte[] buffer, int offset, int count); 15 public virtual int ReadByte(); 16 public abstract long Seek(long offset, SeekOrigin origin); 17 public abstract void SetLength(long value); 18 void IDisposable.Dispose(); 19 public abstract void Write(byte[] buffer, int offset, int count); 20 public virtual void WriteByte(byte value); 21 public abstract bool CanRead { get; } 22 public abstract bool CanSeek { get; } 23 public abstract bool CanWrite { get; } 24 public abstract long Length { get; } 25 public abstract long Position { get; set; } 26 } 27 28 // Namespace: System.IO, Library: BCL 29 public class StreamReader: TextReader 30 { 31 public StreamReader(Stream stream); 32 public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks); 33 public StreamReader(Stream stream, Encoding encoding); 34 public StreamReader(Stream stream, Encoding encoding, bool 35 detectEncodingFromByteOrderMarks); 36 public StreamReader(Stream stream, Encoding encoding, bool 37 detectEncodingFromByteOrderMarks, int bufferSize); 38 public StreamReader(string path); 39 public StreamReader(string path, bool detectEncodingFromByteOrderMarks); 40 public StreamReader(string path, Encoding encoding); 41 public StreamReader(string path, Encoding encoding, bool 42 detectEncodingFromByteOrderMarks); 43 public StreamReader(string path, Encoding encoding, bool 44 detectEncodingFromByteOrderMarks, int bufferSize); 45 public override void Close(); 46 public void DiscardBufferedData(); 47 protected override void Dispose(bool disposing); 48 public override int Peek(); 49 public override int Read(char[] buffer, int index, int count); 50 public override int Read(); 51 public override string ReadLine(); 52 public override string ReadToEnd(); 53 public virtual Stream BaseStream { get; } 54 public virtual Encoding CurrentEncoding { get; } 55 } 56 57 // Namespace: System.IO, Library: BCL 58 public class StreamWriter: TextWriter 59 { 60 public StreamWriter(Stream stream); 61 public StreamWriter(Stream stream, Encoding encoding); 62 public StreamWriter(Stream stream, Encoding encoding, int bufferSize); 63 public StreamWriter(string path); 64 public StreamWriter(string path, bool append); 65 public StreamWriter(string path, bool append, Encoding encoding); 66 public StreamWriter(string path, bool append, Encoding encoding, int 67 bufferSize); 68 public override void Close(); 69

Page 520: C# Language Specification

C# LANGUAGE SPECIFICATION

500

protected override void Dispose(bool disposing); 1 ~StreamWriter(); 2 public override void Flush(); 3 public override void Write(string value); 4 public override void Write(char[] buffer, int index, int count); 5 public override void Write(char[] buffer); 6 public override void Write(char value); 7 public virtual bool AutoFlush { get; set; } 8 public virtual Stream BaseStream { get; } 9 public override Encoding Encoding { get; } 10 } 11 12 // Namespace: System, Library: BCL 13 public sealed class String: ICloneable, IComparable, IEnumerable 14 { 15 unsafe public String(char* value); 16 unsafe public String(char* value, int startIndex, int length); 17 public String(char[] value, int startIndex, int length); 18 public String(char[] value); 19 public String(char c, int count); 20 public static readonly string Empty; 21 public object Clone(); 22 public static int Compare(string strA, string strB); 23 public static int Compare(string strA, string strB, bool ignoreCase); 24 public static int Compare(string strA, int indexA, string strB, int indexB, 25 int length); 26 public static int Compare(string strA, int indexA, string strB, int indexB, 27 int length, bool ignoreCase); 28 public static int CompareOrdinal(string strA, string strB); 29 public static int CompareOrdinal(string strA, int indexA, string strB, int 30 indexB, int length); 31 public int CompareTo(object value); 32 public static string Concat(object arg0, object arg1); 33 public static string Concat(object arg0, object arg1, object arg2); 34 public static string Concat(params object[] args); 35 public static string Concat(string str0, string str1); 36 public static string Concat(string str0, string str1, string str2); 37 public static string Concat(params string[] values); 38 public static string Copy(string str); 39 public void CopyTo(int sourceIndex, char[] destination, int 40 destinationIndex, int count); 41 public bool EndsWith(string value); 42 public override bool Equals(object obj); 43 public static bool Equals(string a, string b); 44 public static string Format(string format, object arg0); 45 public static string Format(string format, object arg0, object arg1); 46 public static string Format(string format, object arg0, object arg1, object 47 arg2); 48 public static string Format(string format, params object[] args); 49 public static string Format(IFormatProvider provider, string format, params 50 object[] args); 51 public CharEnumerator GetEnumerator(); 52 public override int GetHashCode(); 53 public int IndexOf(char value); 54 public int IndexOf(char value, int startIndex); 55 public int IndexOf(char value, int startIndex, int count); 56 public int IndexOf(string value); 57 public int IndexOf(string value, int startIndex); 58 public int IndexOf(string value, int startIndex, int count); 59 public int IndexOfAny(char[] anyOf); 60 public int IndexOfAny(char[] anyOf, int startIndex); 61 public int IndexOfAny(char[] anyOf, int startIndex, int count); 62 public string Insert(int startIndex, string value); 63 public static string Intern(string str); 64 public static string IsInterned(string str); 65 public static string Join(string separator, string[] value); 66 public static string Join(string separator, string[] value, int startIndex, 67 int count); 68 public int LastIndexOf(char value); 69

Page 521: C# Language Specification

Annex D Standard Library

501

public int LastIndexOf(char value, int startIndex); 1 public int LastIndexOf(char value, int startIndex, int count); 2 public int LastIndexOf(string value); 3 public int LastIndexOf(string value, int startIndex); 4 public int LastIndexOf(string value, int startIndex, int count); 5 public int LastIndexOfAny(char[] anyOf); 6 public int LastIndexOfAny(char[] anyOf, int startIndex); 7 public int LastIndexOfAny(char[] anyOf, int startIndex, int count); 8 public static bool operator ==(String a, String b); 9 public static bool operator !=(String a, String b); 10 public string PadLeft(int totalWidth); 11 public string PadLeft(int totalWidth, char paddingChar); 12 public string PadRight(int totalWidth); 13 public string PadRight(int totalWidth, char paddingChar); 14 public string Remove(int startIndex, int count); 15 public string Replace(char oldChar, char newChar); 16 public string Replace(string oldValue, string newValue); 17 public string[] Split(params char[] separator); 18 public string[] Split(char[] separator, int count); 19 public bool StartsWith(string value); 20 public string Substring(int startIndex); 21 public string Substring(int startIndex, int length); 22 IEnumerator IEnumerable.GetEnumerator(); 23 public char[] ToCharArray(); 24 public char[] ToCharArray(int startIndex, int length); 25 public string ToLower(); 26 public string ToString(IFormatProvider provider); 27 public override string ToString(); 28 public string ToUpper(); 29 public string Trim(params char[] trimChars); 30 public string Trim(); 31 public string TrimEnd(params char[] trimChars); 32 public string TrimStart(params char[] trimChars); 33 public char this[int index] { get; } 34 public int Length { get; } 35 } 36 37 // Namespace: System.Text, Library: BCL 38 public sealed class StringBuilder 39 { 40 public StringBuilder(); 41 public StringBuilder(int capacity); 42 public StringBuilder(string value); 43 public StringBuilder Append(char value, int repeatCount); 44 public StringBuilder Append(char[] value, int startIndex, int charCount); 45 public StringBuilder Append(string value); 46 public StringBuilder Append(string value, int startIndex, int count); 47 public StringBuilder Append(bool value); 48 public StringBuilder Append(sbyte value); 49 public StringBuilder Append(byte value); 50 public StringBuilder Append(char value); 51 public StringBuilder Append(short value); 52 public StringBuilder Append(int value); 53 public StringBuilder Append(long value); 54 public StringBuilder Append(float value); 55 public StringBuilder Append(double value); 56 public StringBuilder Append(decimal value); 57 public StringBuilder Append(ushort value); 58 public StringBuilder Append(uint value); 59 public StringBuilder Append(ulong value); 60 public StringBuilder Append(object value); 61 public StringBuilder Append(char[] value); 62 public StringBuilder AppendFormat(string format, object arg0); 63 public StringBuilder AppendFormat(string format, object arg0, object arg1); 64 public StringBuilder AppendFormat(string format, object arg0, object arg1, 65 object arg2); 66 public StringBuilder AppendFormat(string format, params object[] args); 67 public StringBuilder AppendFormat(IFormatProvider provider, string format, 68 params object[] args); 69

Page 522: C# Language Specification

C# LANGUAGE SPECIFICATION

502

public int EnsureCapacity(int capacity); 1 public bool Equals(StringBuilder sb); 2 public StringBuilder Insert(int index, string value, int count); 3 public StringBuilder Insert(int index, string value); 4 public StringBuilder Insert(int index, bool value); 5 public StringBuilder Insert(int index, sbyte value); 6 public StringBuilder Insert(int index, byte value); 7 public StringBuilder Insert(int index, short value); 8 public StringBuilder Insert(int index, char value); 9 public StringBuilder Insert(int index, char[] value); 10 public StringBuilder Insert(int index, char[] value, int startIndex, int 11 charCount); 12 public StringBuilder Insert(int index, int value); 13 public StringBuilder Insert(int index, long value); 14 public StringBuilder Insert(int index, float value); 15 public StringBuilder Insert(int index, double value); 16 public StringBuilder Insert(int index, decimal value); 17 public StringBuilder Insert(int index, ushort value); 18 public StringBuilder Insert(int index, uint value); 19 public StringBuilder Insert(int index, ulong value); 20 public StringBuilder Insert(int index, object value); 21 public StringBuilder Remove(int startIndex, int length); 22 public StringBuilder Replace(string oldValue, string newValue); 23 public StringBuilder Replace(string oldValue, string newValue, int 24 startIndex, int count); 25 public StringBuilder Replace(char oldChar, char newChar); 26 public StringBuilder Replace(char oldChar, char newChar, int startIndex, 27 int count); 28 public override string ToString(); 29 public string ToString(int startIndex, int length); 30 public int Capacity { get; set; } 31 public char this[int index] { get; set; } 32 public int Length { get; set; } 33 } 34 35 // Namespace: System.IO, Library: BCL 36 public class StringReader: TextReader 37 { 38 public StringReader(string s); 39 public override void Close(); 40 protected override void Dispose(bool disposing); 41 public override int Peek(); 42 public override int Read(char[] buffer, int index, int count); 43 public override int Read(); 44 public override string ReadLine(); 45 public override string ReadToEnd(); 46 } 47 48 // Namespace: System.IO, Library: BCL 49 public class StringWriter: TextWriter 50 { 51 public StringWriter(); 52 public StringWriter(IFormatProvider formatProvider); 53 public StringWriter(StringBuilder sb); 54 public StringWriter(StringBuilder sb, IFormatProvider formatProvider); 55 public override void Close(); 56 protected override void Dispose(bool disposing); 57 public virtual StringBuilder GetStringBuilder(); 58 public override string ToString(); 59 public override void Write(string value); 60 public override void Write(char[] buffer, int index, int count); 61 public override void Write(char value); 62 public override Encoding Encoding { get; } 63 } 64 65 // Namespace: System.Threading, Library: BCL 66 public class SynchronizationLockException: SystemException 67 { 68 public SynchronizationLockException(); 69

Page 523: C# Language Specification

Annex D Standard Library

503

public SynchronizationLockException(string message); 1 public SynchronizationLockException(string message, Exception 2 innerException); 3 } 4 5 // Namespace: System, Library: BCL 6 public class SystemException: Exception 7 { 8 public SystemException(); 9 public SystemException(string message); 10 public SystemException(string message, Exception innerException); 11 } 12 13 // Namespace: System.IO, Library: BCL 14 public abstract class TextReader: MarshalByRefObject, IDisposable 15 { 16 protected TextReader(); 17 public static readonly TextReader Null; 18 public virtual void Close(); 19 protected virtual void Dispose(bool disposing); 20 public virtual int Peek(); 21 public virtual int Read(char[] buffer, int index, int count); 22 public virtual int Read(); 23 public virtual int ReadBlock(char[] buffer, int index, int count); 24 public virtual string ReadLine(); 25 public virtual string ReadToEnd(); 26 public static TextReader Synchronized(TextReader reader); 27 void IDisposable.Dispose(); 28 } 29 30 // Namespace: System.IO, Library: BCL 31 public abstract class TextWriter: MarshalByRefObject, IDisposable 32 { 33 protected TextWriter(); 34 protected TextWriter(IFormatProvider formatProvider); 35 public static readonly TextWriter Null; 36 public virtual void Close(); 37 protected virtual void Dispose(bool disposing); 38 public virtual void Flush(); 39 public static TextWriter Synchronized(TextWriter writer); 40 void IDisposable.Dispose(); 41 public virtual void Write(string format, params object[] arg); 42 public virtual void Write(string format, object arg0, object arg1, object 43 arg2); 44 public virtual void Write(string format, object arg0, object arg1); 45 public virtual void Write(string format, object arg0); 46 public virtual void Write(object value); 47 public virtual void Write(string value); 48 public virtual void Write(decimal value); 49 public virtual void Write(double value); 50 public virtual void Write(float value); 51 public virtual void Write(ulong value); 52 public virtual void Write(long value); 53 public virtual void Write(uint value); 54 public virtual void Write(int value); 55 public virtual void Write(bool value); 56 public virtual void Write(char[] buffer, int index, int count); 57 public virtual void Write(char[] buffer); 58 public virtual void Write(char value); 59 public virtual void WriteLine(string format, params object[] arg); 60 public virtual void WriteLine(string format, object arg0, object arg1, 61 object arg2); 62 public virtual void WriteLine(string format, object arg0, object arg1); 63 public virtual void WriteLine(string format, object arg0); 64 public virtual void WriteLine(object value); 65 public virtual void WriteLine(string value); 66 public virtual void WriteLine(decimal value); 67 public virtual void WriteLine(double value); 68 public virtual void WriteLine(float value); 69

Page 524: C# Language Specification

C# LANGUAGE SPECIFICATION

504

public virtual void WriteLine(ulong value); 1 public virtual void WriteLine(long value); 2 public virtual void WriteLine(uint value); 3 public virtual void WriteLine(int value); 4 public virtual void WriteLine(bool value); 5 public virtual void WriteLine(char[] buffer, int index, int count); 6 public virtual void WriteLine(char[] buffer); 7 public virtual void WriteLine(char value); 8 public virtual void WriteLine(); 9 public abstract Encoding Encoding { get; } 10 public virtual IFormatProvider FormatProvider { get; } 11 public virtual string NewLine { get; set; } 12 } 13 14 // Namespace: System, Library: BCL 15 public sealed class Thread 16 { 17 public Thread(ThreadStart start); 18 public void Abort(object stateInfo); 19 public void Abort(); 20 ~Thread(); 21 public void Join(); 22 public bool Join(int millisecondsTimeout); 23 public bool Join(TimeSpan timeout); 24 public static void MemoryBarrier (); 25 public static void ResetAbort(); 26 public static void Sleep(int millisecondsTimeout); 27 public static void Sleep(TimeSpan timeout); 28 public void Start(); 29 public static byte VolatileRead (ref byte address); 30 public static short VolatileRead (ref short address); 31 public static int VolatileRead (ref int address); 32 public static long VolatileRead (ref long address); 33 public static sbyte VolatileRead (ref sbyte address); 34 public static ushort VolatileRead (ref ushort address); 35 public static uint VolatileRead (ref uint address); 36 public static ulong VolatileRead (ref ulong address); 37 public static float VolatileRead (ref float address); 38 public static double VolatileRead (ref double address); 39 public static object VolatileRead (ref object address); 40 public static void VolatileWrite (ref byte address, byte value); 41 public static void VolatileWrite (ref short address, short value); 42 public static void VolatileWrite (ref int address, int value); 43 public static void VolatileWrite (ref long address, long value); 44 public static void VolatileWrite (ref sbyte address, sbyte value); 45 public static void VolatileWrite (ref ushort address, ushort value); 46 public static void VolatileWrite (ref uint address, uint value); 47 public static void VolatileWrite (ref ulong address, ulong value); 48 public static void VolatileWrite (ref float address, float value); 49 public static void VolatileWrite (ref double address, double value); 50 public static void VolatileWrite (ref object address, object value); 51 public static Thread CurrentThread { get; } 52 public bool IsAlive { get; } 53 public bool IsBackground { get; set; } 54 public string Name { get; set; } 55 public ThreadPriority Priority { get; set; } 56 public ThreadState ThreadState { get; } 57 } 58 59 // Namespace: System.Threading, Library: BCL 60 public sealed class ThreadAbortException: SystemException 61 { 62 public object ExceptionState { get; } 63 } 64 65 // Namespace: System.Threading, Library: BCL 66 public enum ThreadPriority 67 { 68 AboveNormal = 3, 69

Page 525: C# Language Specification

Annex D Standard Library

505

BelowNormal = 1, 1 Highest = 4, 2 Lowest = 0, 3 Normal = 2, 4 } 5 6 // Namespace: System.Threading, Library: BCL 7 public delegate void ThreadStart(); 8 9 // Namespace: System.Threading, Library: BCL 10 public enum ThreadState 11 { 12 Aborted = 0x100, 13 AbortRequested = 0x80, 14 Background = 0x4, 15 Running = 0x0, 16 Stopped = 0x10, 17 Unstarted = 0x8, 18 WaitSleepJoin = 0x20, 19 } 20 21 // Namespace: System.Threading, Library: BCL 22 public class ThreadStateException: SystemException 23 { 24 public ThreadStateException(); 25 public ThreadStateException(string message); 26 public ThreadStateException(string message, Exception innerException); 27 } 28 29 // Namespace: System.Threading, Library: BCL 30 public sealed class Timeout 31 { 32 public const int Infinite = -1; 33 } 34 35 // Namespace: System.Threading, Library: BCL 36 public sealed class Timer: MarshalByRefObject, IDisposable 37 { 38 public Timer(TimerCallback callback, object state, int dueTime, int period); 39 public Timer(TimerCallback callback, object state, TimeSpan dueTime, 40 TimeSpan period); 41 public bool Change(int dueTime, int period); 42 public bool Change(TimeSpan dueTime, TimeSpan period); 43 public void Dispose(); 44 public bool Dispose(WaitHandle notifyObject); 45 ~Timer(); 46 } 47 48 // Namespace: System.Threading, Library: BCL 49 public delegate void TimerCallback(object state); 50 51 // Namespace: System, Library: BCL 52 public struct TimeSpan: IComparable 53 { 54 public TimeSpan(long ticks); 55 public TimeSpan(int hours, int minutes, int seconds); 56 public TimeSpan(int days, int hours, int minutes, int seconds); 57 public TimeSpan(int days, int hours, int minutes, int seconds, int 58 milliseconds); 59 public static readonly TimeSpan MaxValue; 60 public static readonly TimeSpan MinValue; 61 public const long TicksPerDay = 864000000000; 62 public const long TicksPerHour = 36000000000; 63 public const long TicksPerMillisecond = 10000; 64 public const long TicksPerMinute = 600000000; 65 public const long TicksPerSecond = 10000000; 66 public static readonly TimeSpan Zero; 67 public TimeSpan Add(TimeSpan ts); 68 public static int Compare(TimeSpan t1, TimeSpan t2); 69

Page 526: C# Language Specification

C# LANGUAGE SPECIFICATION

506

public int CompareTo(object value); 1 public TimeSpan Duration(); 2 public override bool Equals(object value); 3 public static bool Equals(TimeSpan t1, TimeSpan t2); 4 public static TimeSpan FromDays(double value); 5 public static TimeSpan FromHours(double value); 6 public static TimeSpan FromMilliseconds(double value); 7 public static TimeSpan FromMinutes(double value); 8 public static TimeSpan FromSeconds(double value); 9 public static TimeSpan FromTicks(long value); 10 public override int GetHashCode(); 11 public TimeSpan Negate(); 12 public static TimeSpan operator +(TimeSpan t1, TimeSpan t2); 13 public static bool operator ==(TimeSpan t1, TimeSpan t2); 14 public static bool operator >(TimeSpan t1, TimeSpan t2); 15 public static bool operator >=(TimeSpan t1, TimeSpan t2); 16 public static bool operator !=(TimeSpan t1, TimeSpan t2); 17 public static bool operator <(TimeSpan t1, TimeSpan t2); 18 public static bool operator <=(TimeSpan t1, TimeSpan t2); 19 public static TimeSpan operator -(TimeSpan t1, TimeSpan t2); 20 public static TimeSpan operator -(TimeSpan t); 21 public static TimeSpan operator +(TimeSpan t); 22 public static TimeSpan Parse(string s); 23 public TimeSpan Subtract(TimeSpan ts); 24 public override string ToString(); 25 public int Days { get; } 26 public int Hours { get; } 27 public int Milliseconds { get; } 28 public int Minutes { get; } 29 public int Seconds { get; } 30 public long Ticks { get; } 31 public double TotalDays { get; } 32 public double TotalHours { get; } 33 public double TotalMilliseconds { get; } 34 public double TotalMinutes { get; } 35 public double TotalSeconds { get; } 36 } 37 38 // Namespace: System, Library: BCL 39 public abstract class Type: Object 40 { 41 public virtual int GetArrayRank(); 42 public abstract Type GetElementType(); 43 public override int GetHashCode(); 44 public virtual bool IsAssignableFrom(Type c); 45 public virtual bool IsInstanceOfType(object o); 46 public virtual bool IsSubclassOf(Type c); 47 public override string ToString(); 48 public abstract Type BaseType { get; } 49 public abstract string FullName { get; } 50 public bool IsArray { get; } 51 public bool IsClass { get; } 52 public bool IsEnum { get; } 53 public bool IsInterface { get; } 54 public bool IsPointer { get; } 55 public bool IsValueType { get; } 56 } 57 58 // Namespace: System, Library: BCL 59 public sealed class TypeInitializationException: SystemException 60 { 61 public string TypeName { get; } 62 } 63 64 // Namespace: System, Library: BCL 65 public struct UInt16: IComparable, IFormattable 66 { 67 public const ushort MaxValue = 65535; 68 public const ushort MinValue = 0; 69

Page 527: C# Language Specification

Annex D Standard Library

507

public int CompareTo(object value); 1 public override bool Equals(object obj); 2 public override int GetHashCode(); 3 public static ushort Parse(string s); 4 public static ushort Parse(string s, NumberStyles style); 5 public static ushort Parse(string s, IFormatProvider provider); 6 public static ushort Parse(string s, NumberStyles style, IFormatProvider 7 provider); 8 public string ToString(IFormatProvider provider); 9 public string ToString(string format, IFormatProvider provider); 10 public override string ToString(); 11 public string ToString(string format); 12 } 13 14 // Namespace: System, Library: BCL 15 public struct UInt32: IComparable, IFormattable 16 { 17 public const uint MaxValue = 4294967295; 18 public const uint MinValue = 0; 19 public int CompareTo(object value); 20 public override bool Equals(object obj); 21 public override int GetHashCode(); 22 public static uint Parse(string s); 23 public static uint Parse(string s, NumberStyles style); 24 public static uint Parse(string s, IFormatProvider provider); 25 public static uint Parse(string s, NumberStyles style, IFormatProvider 26 provider); 27 public string ToString(IFormatProvider provider); 28 public string ToString(string format, IFormatProvider provider); 29 public override string ToString(); 30 public string ToString(string format); 31 } 32 33 // Namespace: System, Library: BCL 34 public struct UInt64: IComparable, IFormattable 35 { 36 public const ulong MaxValue = 18446744073709551615; 37 public const ulong MinValue = 0; 38 public int CompareTo(object value); 39 public override bool Equals(object obj); 40 public override int GetHashCode(); 41 public static ulong Parse(string s); 42 public static ulong Parse(string s, NumberStyles style); 43 public static ulong Parse(string s, IFormatProvider provider); 44 public static ulong Parse(string s, NumberStyles style, IFormatProvider 45 provider); 46 public string ToString(IFormatProvider provider); 47 public string ToString(string format, IFormatProvider provider); 48 public override string ToString(); 49 public string ToString(string format); 50 } 51 52 // Namespace: System, Library: BCL 53 public class UnauthorizedAccessException: SystemException 54 { 55 public UnauthorizedAccessException(); 56 public UnauthorizedAccessException(string message); 57 public UnauthorizedAccessException(string message, Exception inner); 58 } 59 60 // Namespace: System.Globalization, Library: BCL 61 public enum UnicodeCategory 62 { 63 ClosePunctuation = 21, 64 ConnectorPunctuation = 18, 65 Control = 14, 66 CurrencySymbol = 26, 67 DashPunctuation = 19, 68 DecimalDigitNumber = 8, 69

Page 528: C# Language Specification

C# LANGUAGE SPECIFICATION

508

EnclosingMark = 7, 1 FinalQuotePunctuation = 23, 2 Format = 15, 3 InitialQuotePunctuation = 22, 4 LetterNumber = 9, 5 LineSeparator = 12, 6 LowercaseLetter = 1, 7 MathSymbol = 25, 8 ModifierLetter = 3, 9 ModifierSymbol = 27, 10 NonSpacingMark = 5, 11 OpenPunctuation = 20, 12 OtherLetter = 4, 13 OtherNotAssigned = 29, 14 OtherNumber = 10, 15 OtherPunctuation = 24, 16 OtherSymbol = 28, 17 ParagraphSeparator = 13, 18 PrivateUse = 17, 19 SpaceSeparator = 11, 20 SpacingCombiningMark = 6, 21 Surrogate = 16, 22 TitlecaseLetter = 2, 23 UppercaseLetter = 0, 24 } 25 26 // Namespace: System.Text, Library: BCL 27 public class UnicodeEncoding: Encoding 28 { 29 public UnicodeEncoding(); 30 public UnicodeEncoding(bool bigEndian, bool byteOrderMark); 31 public override bool Equals(object value); 32 public override int GetByteCount(char[] chars, int index, int count); 33 public override int GetByteCount(string s); 34 public override int GetBytes(string s, int charIndex, int charCount, byte[] 35 bytes, int byteIndex); 36 public override byte[] GetBytes(string s); 37 public override int GetBytes(char[] chars, int charIndex, int charCount, 38 byte[] bytes, int byteIndex); 39 public override int GetCharCount(byte[] bytes, int index, int count); 40 public override int GetChars(byte[] bytes, int byteIndex, int byteCount, 41 char[] chars, int charIndex); 42 public override Decoder GetDecoder(); 43 public override int GetHashCode(); 44 public override int GetMaxByteCount(int charCount); 45 public override int GetMaxCharCount(int byteCount); 46 public override byte[] GetPreamble(); 47 } 48 49 // Namespace: System.Text, Library: BCL 50 public class UTF8Encoding: Encoding 51 { 52 public UTF8Encoding(); 53 public UTF8Encoding(bool encoderShouldEmitUTF8Identifier); 54 public UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool 55 throwOnInvalidBytes); 56 public override bool Equals(object value); 57 public override int GetByteCount(char[] chars, int index, int count); 58 public override int GetByteCount(string chars); 59 public override int GetBytes(string s, int charIndex, int charCount, byte[] 60 bytes, int byteIndex); 61 public override byte[] GetBytes(string s); 62 public override int GetBytes(char[] chars, int charIndex, int charCount, 63 byte[] bytes, int byteIndex); 64 public override int GetCharCount(byte[] bytes, int index, int count); 65 public override int GetChars(byte[] bytes, int byteIndex, int byteCount, 66 char[] chars, int charIndex); 67 public override Decoder GetDecoder(); 68 public override Encoder GetEncoder(); 69

Page 529: C# Language Specification

Annex D Standard Library

509

public override int GetHashCode(); 1 public override int GetMaxByteCount(int charCount); 2 public override int GetMaxCharCount(int byteCount); 3 public override byte[] GetPreamble(); 4 } 5 6 // Namespace: System, Library: BCL 7 public abstract class ValueType 8 { 9 protected ValueType(); 10 public override bool Equals(object obj); 11 public override int GetHashCode(); 12 public override string ToString(); 13 } 14 15 // Namespace: System.Security, Library: BCL 16 public class VerificationException: SystemException 17 { 18 public VerificationException(); 19 public VerificationException(string message); 20 public VerificationException(string message, Exception innerException); 21 } 22 23 // Namespace: System, Library: BCL 24 public sealed class Version: ICloneable, IComparable 25 { 26 public Version(int major, int minor, int build, int revision); 27 public Version(int major, int minor, int build); 28 public Version(int major, int minor); 29 public Version(string version); 30 public Version(); 31 public object Clone(); 32 public int CompareTo(object version); 33 public override bool Equals(object obj); 34 public override int GetHashCode(); 35 public static bool operator ==(Version v1, Version v2); 36 public static bool operator >(Version v1, Version v2); 37 public static bool operator >=(Version v1, Version v2); 38 public static bool operator !=(Version v1, Version v2); 39 public static bool operator <(Version v1, Version v2); 40 public static bool operator <=(Version v1, Version v2); 41 public int Build { get; } 42 public int Major { get; } 43 public int Minor { get; } 44 public int Revision { get; } 45 } 46 47 // Namespace: System.Threading, Library: BCL 48 public abstract class WaitHandle: MarshalByRefObject, IDisposable 49 { 50 public WaitHandle(); 51 public virtual void Close(); 52 protected virtual void Dispose(bool explicitDisposing); 53 ~WaitHandle(); 54 void IDisposable.Dispose(); 55 public static bool WaitAll(WaitHandle[] waitHandles); 56 public static int WaitAny(WaitHandle[] waitHandles); 57 public virtual bool WaitOne(); 58 } 59

Page 530: C# Language Specification
Page 531: C# Language Specification

Annex E Documentation Comments

511

Annex E. Documentation Comments 1

This annex is informative. 2

C# provides a mechanism for programmers to document their code using a special comment syntax that 3 contains XML text. Comments using such syntax are called documentation comments. The XML 4 generation tool is called the documentation generator. (This generator could be, but need not be, the 5 C# compiler itself.) The output produced by the documentation generator is called the documentation file. A 6 documentation file is used as input to a documentation viewer; a tool intended to produce some sort of 7 visual display of type information and its associated documentation. 8

A conforming C# compiler is not required to check the syntax of documentation comments; such comments 9 are simply ordinary comments. A conforming compiler is permitted to do such checking, however. 10

This specification suggests a set of standard tags to be used in documentation comments. For 11 C# implementations targeting the CLI, it also provides information about the documentation generator and 12 the format of the documentation file. No information is provided about the documentation viewer. 13

E.1 Introduction 14

Comments having a special form can be used to direct a tool to produce XML from those comments and the 15 source code elements, which they precede. Such comments are single-line comments of the form /// … or 16 delimited comments of the form /** … */. They must immediately precede a user-defined type (such as a 17 class, delegate, or interface) or a member (such as a field, event, property, or method) that they annotate. 18

Syntax: 19

single-line-doc-comment:: 20 /// input-charactersopt 21

delimited-doc-comment:: 22 /** delimited-comment-charactersopt */ 23

In a single-line-doc-comment, if there is a whitespace character following the /// characters on each of the 24 single-line-doc-comments adjacent to the current single-line-doc-comment, then that whitespace character is 25 not included in the XML output. 26

In a delimited-doc-comment, if the first non-whitespace character on the second line is an asterisk and the 27 same pattern of optional whitespace characters and an asterisk character is repeated at the beginning of each 28 of the lines within the delimited-doc-comment, then the characters of the repeated pattern are not included in 29 the XML output. The pattern can include whitespace characters after, as well as before, the asterisk 30 character. 31

Example: 32

/** 33 * <remarks> 34 * Class <c>Point</c> models a point in a two-dimensional plane. 35 * </remarks> 36 */ 37 public class Point 38 { 39 /// <remarks>Method <c>Draw</c> renders the point.</remarks> 40 void Draw() { … } 41 } 42

Page 532: C# Language Specification

C# LANGUAGE SPECIFICATION

512

The text within documentation comments must be well formed according to the rules of XML 1 (http://www.w3.org/TR/REC-xml). If the XML is ill formed, a warning is generated and the documentation 2 file will contain a comment saying that an error was encountered. 3

Although developers are free to create their own set of tags, a recommended set is defined in §E.2. Some of 4 the recommended tags have special meanings: 5

• The <param> tag is used to describe parameters. If such a tag is used, the documentation generator must 6 verify that the specified parameter exists and that all parameters are described in documentation 7 comments. If such verification fails, the documentation generator issues a warning. 8

• The cref attribute can be attached to any tag to provide a reference to a code element. The 9 documentation generator must verify that this code element exists. If the verification fails, the 10 documentation generator issues a warning. When looking for a name described in a cref attribute, the 11 documentation generator must respect namespace visibility according to using statements appearing 12 within the source code. 13

• The <summary> tag is intended to be used by a documentation viewer to display additional information 14 about a type or member. 15

Note carefully that the documentation file does not provide full information about the type and members (for 16 example, it does not contain any type information). To get such information about a type or member, the 17 documentation file must be used in conjunction with reflection on the actual type or member. 18

E.2 Recommended tags 19

The documentation generator must accept and process any tag that is valid according to the rules of XML. 20 The following tags provide commonly used functionality in user documentation. (Of course, other tags are 21 possible.) 22

23

Tag Reference Purpose <c> §E.2.1 Set text in a code-like font <code> §E.2.2 Set one or more lines of source code or program output <example> §E.2.3 Indicate an example <exception> §E.2.4 Identifies the exceptions a method can throw <list> §E.2.5 Create a list or table <para> §E.2.6 Permit structure to be added to text <param> §E.2.7 Describe a parameter for a method or constructor <paramref> §E.2.8 Identify that a word is a parameter name <permission> §E.2.9 Document the security accessibility of a member <remarks> §E.2.10 Describe a type <returns> §E.2.11 Describe the return value of a method <see> §E.2.12 Specify a link <seealso> §E.2.13 Generate a See Also entry <summary> §E.2.14 Describe a member of a type <value> §E.2.15 Describe a property

24

Page 533: C# Language Specification

Annex E Documentation Comments

513

E.2.1 <c> 1

This tag provides a mechanism to indicate that a fragment of text within a description should be set in a 2 special font such as that used for a block of code. For lines of actual code, use <code> (§E.2.2). 3

Syntax: 4

<c>text to be set like code</c> 5

Example: 6

/// <remarks> 7 /// Class <c>Point</c> models a point in a two-dimensional plane. 8 /// </remarks> 9 public class Point 10 { 11 … 12 } 13

E.2.2 <code> 14

This tag is used to set one or more lines of source code or program output in some special font. For small 15 code fragments in narrative, use <c> (§E.2.1). 16

Syntax: 17

<code>source code or program output</code> 18

Example: 19

/// <summary> 20 /// Changes the Point's location by the given x- and y-offsets. 21 /// </summary> 22 /// <example> 23 /// The following code: 24 /// <code> 25 /// Point p = new Point(3,5); 26 /// p.Translate(-1,3); 27 /// </code> 28 /// results in <c>p</c>'s having the value (2,8). 29 /// </example> 30 public void Translate(int xor, int yor) { 31 X += xor; 32 Y += yor; 33 } 34

E.2.3 <example> 35

This tag allows example code within a comment, to specify how a method or other library member might be 36 used. Ordinarily, this would also involve use of the tag <code> (§E.2.2) as well. 37

Syntax: 38

<example>description</example> 39

Example: 40

See <code> (§E.2.2) for an example. 41

E.2.4 <exception> 42

This tag provides a way to document the exceptions a method can throw. 43

Syntax: 44

<exception cref="member">description</exception> 45

where 46

cref="member" 47

Page 534: C# Language Specification

C# LANGUAGE SPECIFICATION

514

The name of a member. The documentation generator checks that the given member exists and 1 translates member to the canonical element name in the documentation file. 2

description 3

A description of the circumstances in which the exception is thrown. 4

Example: 5

public class DataBaseOperations 6 { 7 /// <exception cref="MasterFileFormatCorruptException"></exception> 8 /// <exception cref="MasterFileLockedOpenException"></exception> 9 public static void ReadRecord(int flag) { 10 if (flag == 1) 11 throw new MasterFileFormatCorruptException(); 12 else if (flag == 2) 13 throw new MasterFileLockedOpenException(); 14 // … 15 } 16 } 17

E.2.5 <list> 18

This tag is used to create a list or table of items. It can contain a <listheader> block to define the heading 19 row of either a table or definition list. (When defining a table, only an entry for term in the heading need be 20 supplied.) 21

Each item in the list is specified with an <item> block. When creating a definition list, both term and 22 description must be specified. However, for a table, bulleted list, or numbered list, only description 23 need be specified. 24

Syntax: 25

<list type="style"> 26 <listheader> 27 <term>term</term> 28 <description>description</description> 29 </listheader> 30 <item> 31 <term>term</term> 32 <description>description</description> 33 </item> 34 … 35 <item> 36 <term>term</term> 37 <description>description</description> 38 </item> 39 </list> 40

where 41

style 42

The style of the list. Must be bullet, number, or table. 43

term 44

The term to define, whose definition is in description. 45

description 46

Either an item in a bullet or numbered list, or the definition of a term. 47

Example: 48

Page 535: C# Language Specification

Annex E Documentation Comments

515

public class MyClass 1 { 2 /// <remarks> 3 /// Here is an example of a bulleted list: 4 /// <list type="bullet"> 5 /// <item> 6 /// <description>First item.</description> 7 /// </item> 8 /// <item> 9 /// <description>Second item.</description> 10 /// </item> 11 /// </list> 12 /// </remarks> 13 public static void Main () { 14 … 15 } 16 } 17

E.2.6 <para> 18

This tag is for use inside other tags, such as <remarks> (§E.2.10) or <returns> (§E.2.11), and permits 19 structure to be added to text. 20

Syntax: 21

<para>content</para> 22

where 23

content 24

The text of the paragraph. 25

Example: 26

/// <summary> 27 /// <para> 28 /// This is the entry point of the Point class testing program. 29 /// </para> 30 /// <para> 31 /// This program tests each method and operator, and is intended 32 /// to be run after any non-trvial maintenance has been performed 33 /// on the Point class. 34 /// </para> 35 /// </summary> 36 public static void Main() { 37 … 38 } 39

E.2.7 <param> 40

This tag is used to describe a parameter for a method, constructor, or indexer. 41

Syntax: 42

<param name="name">description</param> 43

where 44

name 45

The name of the parameter. 46

description 47

A description of the parameter. 48

Example: 49

Page 536: C# Language Specification

C# LANGUAGE SPECIFICATION

516

/// <summary> 1 /// This method changes the Point's location to the given coordinates. 2 /// </summary> 3 /// <param name="xor">The new x-coordinate.</param> 4 /// <param name="yor">The new y-coordinate.</param> 5 public void Move(int xor, int yor) { 6 X = xor; 7 Y = yor; 8 } 9

E.2.8 <paramref> 10

This tag is used to indicate that a word is a parameter. The documentation file can be processed to format 11 this parameter in some distinct way. 12

Syntax: 13

<paramref name="name"/> 14

where 15

name 16

The name of the parameter. 17

Example: 18

/// <summary> 19 /// This constructor initializes the new Point to 20 /// (<paramref name="xor"/>,<paramref name="yor"/>). 21 /// </summary> 22 /// <param name="xor">The new Point's x-coordinate.</param> 23 /// <param name="yor">The new Point's y-coordinate.</param> 24 public Point(int xor, int yor) { 25 X = xor; 26 Y = yor; 27 } 28

E.2.9 <permission> 29

This tag allows the security accessibility of a member to be documented. 30

Syntax: 31

<permission cref="member">description</permission> 32

where 33

member 34

The name of a member. The documentation generator checks that the given code element exists and 35 translates member to the canonical element name in the documentation file. 36

description 37

A description of the access to the member. 38

Example: 39

/// <permission cref="System.Security.PermissionSet"> 40 /// Everyone can access this method. 41 /// </permission> 42 public static void Test() { 43 … 44 } 45

E.2.10 <remarks> 46

This tag is used to specify overview information about a type. Use <summary> (§E.2.14) to describe the 47 members of a type. 48

Syntax: 49

Page 537: C# Language Specification

Annex E Documentation Comments

517

<remarks>text</remarks> 1

where 2

text 3

The text of the remarks. 4

Example: 5

/// <remarks> 6 /// Class <c>Point</c> models a point in a two-dimensional plane. 7 /// </remarks> 8 public class Point 9 { 10 … 11 } 12

E.2.11 <returns> 13

This tag is used to describe the return value of a method. 14

Syntax: 15

<returns>description</returns> 16

where 17

description 18

A description of the return value. 19

Example: 20

/// <summary> 21 /// Report a Point's location as a string. 22 /// </summary> 23 /// <returns> 24 /// A string representing a Point's location, in the form (x,y), 25 /// without any leading, training, or embedded whitespace. 26 /// </returns> 27 public override string ToString() { 28 return "(" + X + "," + Y + ")"; 29 } 30

E.2.12 <see> 31

This tag allows a link to be specified within text. Use <seealso> (§E.2.13) to indicate text that is to appear 32 in a See Also subclause. 33

Syntax: 34

<see cref="member"/> 35

where 36

member 37

The name of a member. The documentation generator checks that the given code element exists and 38 passes member to the element name in the documentation file. 39

Example: 40

/// <summary> 41 /// This method changes the Point's location to the given coordinates. 42 /// Use the <see cref="Translate"/> method to apply a relative change. 43 /// </summary> 44 public void Move(int xor, int yor) { 45 X = xor; 46 Y = yor; 47 } 48

Page 538: C# Language Specification

C# LANGUAGE SPECIFICATION

518

/// <summary> 1 /// This method changes the Point's location by the given offsets. 2 /// Use the <see cref="Move"/> method to directly set the coordinates. 3 /// </summary> 4 public void Translate(int xor, int yor) { 5 X += xor; 6 Y += yor; 7 } 8

E.2.13 <seealso> 9

This tag allows an entry to be generated for the See Also subclause. Use <see> (§E.2.12) to specify a link 10 from within text. 11

Syntax: 12

<seealso cref="member"/> 13

where 14

member 15

The name of a member. The documentation generator checks that the given code element exists and 16 passes member to the element name in the documentation file. 17

Example: 18

/// <summary> 19 /// This method determines whether two Points have the same location. 20 /// </summary> 21 /// <seealso cref="operator =="/> 22 /// <seealso cref="operator !="/> 23 public override bool Equals(object o) { 24 … 25 } 26

E.2.14 <summary> 27

This tag can be used to describe a member for a type. Use <remarks> (§E.2.10) to describe the type itself. 28

Syntax: 29

<summary>description</summary> 30

where 31

description 32

A summary of the member. 33

Example: 34

/// <summary> 35 /// This constructor initializes the new Point to (0,0). 36 /// </summary> 37 public Point(): this(0,0) { 38 } 39

E.2.15 <value> 40

This tag allows a property to be described. 41

Syntax: 42

<value>property description</value> 43

where 44

property description 45

A description for the property. 46

Example: 47

Page 539: C# Language Specification

Annex E Documentation Comments

519

/// <value> 1 /// The point's x-coordinate. 2 /// </value> 3 public int X 4 { 5 get { return x; } 6 set { x = value; } 7 } 8

E.3 Processing the documentation file 9

The following information is intended for C# implementations targeting the CLI. 10

The documentation generator generates an ID string for each element in the source code that is tagged with a 11 documentation comment. This ID string uniquely identifies a source element. A documentation viewer can 12 use an ID string to identify the corresponding metadata/reflection item to which the documentation applies. 13

The documentation file is not a hierarchical representation of the source code; rather, it is a flat list with a 14 generated ID string for each element. 15

E.3.1 ID string format 16

The documentation generator observes the following rules when it generates the ID strings: 17

• No white space is placed in the string. 18

• The first part of the string identifies the kind of member being documented, via a single character 19 followed by a colon. The following kinds of members are defined: 20

21

Character Description

E Event

F Field

M Method (including constructors, destructors, and operators)

N Namespace

P Property (including indexers)

T Type (such as class, delegate, enum, interface, and struct)

! Error string; the rest of the string provides information about the error. For example, the documentation generator generates error information for links that cannot be resolved.

22

• The second part of the string is the fully qualified name of the element, starting at the root of the 23 namespace. The name of the element, its enclosing type(s), and namespace are separated by periods. If 24 the name of the item itself has periods, they are replaced by the NUMBER SIGN # (U+0023). (It is 25 assumed that no element has this character in its name.) 26

• For methods and properties with arguments, the argument list follows, enclosed in parentheses. For 27 those without arguments, the parentheses are omitted. The arguments are separated by commas. The 28 encoding of each argument is the same as a CLI signature, as follows: Arguments are represented by 29 their fully qualified name. For example, int becomes System.Int32, string becomes 30 System.String, object becomes System.Object, and so on. Arguments having the out or ref 31 modifier have an @ following their type name. Arguments passed by value or via params have no 32 special notation. Arguments that are arrays are represented as [ lowerbound : size , … , lowerbound : 33 size ] where the number of commas is the rank less one, and the lower bounds and size of each 34 dimension, if known, are represented in decimal. If a lower bound or size is not specified, it is omitted. 35 If the lower bound and size for a particular dimension are omitted, the “:” is omitted as well. Jagged 36 arrays are represented by one “[]” per level. Arguments that have pointer types other than void are 37

Page 540: C# Language Specification

C# LANGUAGE SPECIFICATION

520

represented using a * following the type name. A void pointer is represented using a type name of 1 System.Void. 2

E.3.2 ID string examples 3

The following examples each show a fragment of C# code, along with the ID string produced from each 4 source element capable of having a documentation comment: 5

• Types are represented using their fully qualified name. 6

enum Color { Red, Blue, Green } 7

namespace Acme 8 { 9 interface IProcess { … } 10

struct ValueType { … } 11

class Widget: IProcess 12 { 13 public class NestedClass { … } 14

public interface IMenuItem { … } 15

public delegate void Del(int i); 16

public enum Direction { North, South, East, West } 17 } 18 } 19

"T:Color" 20 "T:Acme.IProcess" 21 "T:Acme.ValueType" 22 "T:Acme.Widget" 23 "T:Acme.Widget.NestedClass" 24 "T:Acme.Widget.IMenuItem" 25 "T:Acme.Widget.Del" 26 "T:Acme.Widget.Direction" 27

• Fields are represented by their fully qualified name. 28

namespace Acme 29 { 30 struct ValueType 31 { 32 private int total; 33 } 34

class Widget: IProcess 35 { 36 public class NestedClass 37 { 38 private int value; 39 } 40

private string message; 41 private static Color defaultColor; 42 private const double PI = 3.14159; 43 protected readonly double monthlyAverage; 44 private long[] array1; 45 private Widget[,] array2; 46 private unsafe int *pCount; 47 private unsafe float **ppValues; 48 } 49 } 50

Page 541: C# Language Specification

Annex E Documentation Comments

521

"F:Acme.ValueType.total" 1 "F:Acme.Widget.NestedClass.value" 2 "F:Acme.Widget.message" 3 "F:Acme.Widget.defaultColor" 4 "F:Acme.Widget.PI" 5 "F:Acme.Widget.monthlyAverage" 6 "F:Acme.Widget.array1" 7 "F:Acme.Widget.array2" 8 "F:Acme.Widget.pCount" 9 "F:Acme.Widget.ppValues" 10

• Constructors. 11

namespace Acme 12 { 13 class Widget: IProcess 14 { 15 static Widget() { … } 16

public Widget() { … } 17

public Widget(string s) { … } 18 } 19 } 20

"M:Acme.Widget.#cctor" 21 "M:Acme.Widget.#ctor" 22 "M:Acme.Widget.#ctor(System.String)" 23

• Destructors. 24

namespace Acme 25 { 26 class Widget: IProcess 27 { 28 ~Widget() { … } 29 } 30 } 31

"M:Acme.Widget.Finalize" 32

• Methods. 33

namespace Acme 34 { 35 struct ValueType 36 { 37 public void M(int i) { … } 38 } 39

class Widget: IProcess 40 { 41 public class NestedClass 42 { 43 public void M(int i) { … } 44 } 45

public static void M0() { … } 46 public void M1(char c, out float f, ref ValueType v) { … } 47 public void M2(short[] x1, int[,] x2, long[][] x3) { … } 48 public void M3(long[][] x3, Widget[][,,] x4) { … } 49 public unsafe void M4(char *pc, Color **pf) { … } 50 public unsafe void M5(void *pv, double *[][,] pd) { … } 51 public void M6(int i, params object[] args) { … } 52 } 53 } 54

Page 542: C# Language Specification

C# LANGUAGE SPECIFICATION

522

"M:Acme.ValueType.M(System.Int32)" 1 "M:Acme.Widget.NestedClass.M(System.Int32)" 2 "M:Acme.Widget.M0" 3 "M:Acme.Widget.M1(System.Char,System.Single@,Acme.ValueType@)" 4 "M:Acme.Widget.M2(System.Int16[],System.Int32[0:,0:],System.Int64[][])" 5 "M:Acme.Widget.M3(System.Int64[][],Acme.Widget[0:,0:,0:][])" 6 "M:Acme.Widget.M4(System.Char*,Color**)" 7 "M:Acme.Widget.M5(System.Void*,System.Double*[0:,0:][])" 8 "M:Acme.Widget.M6(System.Int32,System.Object[])" 9

• Properties and indexers. 10

namespace Acme 11 { 12 class Widget: IProcess 13 { 14 public int Width {get { … } set { … }} 15 public int this[int i] {get { … } set { … }} 16 public int this[string s, int i] {get { … } set { … }} 17 } 18 } 19

"P:Acme.Widget.Width" 20 "P:Acme.Widget.Item(System.Int32)" 21 "P:Acme.Widget.Item(System.String,System.Int32)" 22

• Events 23

namespace Acme 24 { 25 class Widget: IProcess 26 { 27 public event Del AnEvent; 28 } 29 } 30

"E:Acme.Widget.AnEvent" 31

• Unary operators. 32

namespace Acme 33 { 34 class Widget: IProcess 35 { 36 public static Widget operator+(Widget x) { … } 37 } 38 } 39

"M:Acme.Widget.op_UnaryPlus(Acme.Widget)" 40

The complete set of unary operator function names used is as follows: op_UnaryPlus, 41 op_UnaryNegation, op_LogicalNot, op_OnesComplement, op_Increment, 42 op_Decrement, op_True, and op_False. 43

• Binary operators. 44

namespace Acme 45 { 46 class Widget: IProcess 47 { 48 public static Widget operator+(Widget x1, Widget x2) { return x1; } 49 } 50 } 51

"M:Acme.Widget.op_Addition(Acme.Widget,Acme.Widget)" 52

The complete set of binary operator function names used is as follows: op_Addition, 53 op_Subtraction, op_Multiply, op_Division, op_Modulus, op_BitwiseAnd, 54 op_BitwiseOr, op_ExclusiveOr, op_LeftShift, op_RightShift, op_Equality, 55 op_Inequality, op_LessThan, op_LessThanOrEqual, op_GreaterThan, and 56 op_GreaterThanOrEqual. 57

Page 543: C# Language Specification

Annex E Documentation Comments

523

• Conversion operators have a trailing “~” followed by the return type. 1

namespace Acme 2 { 3 class Widget: IProcess 4 { 5 public static explicit operator int(Widget x) { … } 6 public static implicit operator long(Widget x) { … } 7 } 8 } 9

"M:Acme.Widget.op_Explicit(Acme.Widget)~System.Int32" 10 "M:Acme.Widget.op_Implicit(Acme.Widget)~System.Int64" 11

E.4 An example 12

E.4.1 C# source code 13

The following example shows the source code of a Point class: 14

namespace Graphics 15 { 16

/// <remarks> 17 /// Class <c>Point</c> models a point in a two-dimensional plane. 18 /// </remarks> 19 public class Point 20 { 21 /// <summary> 22 /// Instance variable <c>x</c> represents the Point's x-coordinate. 23 /// </summary> 24 private int x; 25

/// <summary> 26 /// Instance variable <c>y</c> represents the Point's y-coordinate. 27 /// </summary> 28 private int y; 29

/// <value> 30 /// The Point's x-coordinate. 31 /// </value> 32 public int X 33 { 34 get { return x; } 35 set { x = value; } 36 } 37

/// <value> 38 /// The Point's y-coordinate. 39 /// </value> 40 public int Y 41 { 42 get { return y; } 43 set { y = value; } 44 } 45

/// <summary> 46 /// This constructor initializes the new Point to (0,0). 47 /// </summary> 48 public Point(): this(0,0) {} 49

/// <summary> 50 /// This constructor initializes the new Point to 51 /// (<paramref name="xor"/>,<paramref name="yor"/>). 52 /// </summary> 53 /// <param name="xor">The new Point's x-coordinate.</param> 54 /// <param name="yor">The new Point's y-coordinate.</param> 55 public Point(int xor, int yor) { 56 x = xor; 57 y = yor; 58 } 59

Page 544: C# Language Specification

C# LANGUAGE SPECIFICATION

524

/// <summary> 1 /// This method changes the point's location to the given 2 /// coordinates. 3 /// </summary> 4 /// <param name="xor">The new x-coordinate.</param> 5 /// <param name="yor">The new y-coordinate.</param> 6 /// <seealso cref="Translate"/> 7 public void Move(int xor, int yor) { 8 x = xor; 9 y = yor; 10 } 11

/// <summary> 12 /// This method changes the point's location by the given 13 /// x- and y-offsets. 14 /// </summary> 15 /// <example> 16 /// The following code: 17 /// <code> 18 /// Point p = new Point(3,5); 19 /// p.Translate(-1,3); 20 /// </code> 21 /// results in <c>p</c>'s having the value (2,8). 22 /// </example> 23 /// <param name="xor">The relative x-offset.</param> 24 /// <param name="yor">The relative y-offset.</param> 25 /// <seealso cref="Move"/> 26 public void Translate(int xor, int yor) { 27 x += xor; 28 y += yor; 29 } 30

/// <summary> 31 /// This method determines whether two Points have the same 32 /// location. 33 /// </summary> 34 /// <param name="o"> 35 /// The object to be compared to the current object. 36 /// </param> 37 /// <returns> 38 /// True if the Points have the same location; otherwise, false. 39 /// </returns> 40 /// <seealso cref="operator =="/> 41 /// <seealso cref="operator !="/> 42 public override bool Equals(object o) { 43 Point p = o as Point; 44 if (p == null) return false; 45 return x == p.x && y == p.y; 46 } 47

/// <summary> 48 /// Computes the hash code for a Point. 49 /// </summary> 50 /// <returns> 51 /// A hash code computed from the x and y coordinates. 52 /// </returns> 53 public override int GetHashCode() { 54 return x ^ y; 55 } 56

/// <summary> 57 /// Report a point's location as a string. 58 /// </summary> 59 /// <returns> 60 /// A string representing a point's location, in the form (x,y), 61 /// without any leading, training, or embedded whitespace. 62 /// </returns> 63 public override string ToString() { 64 return "(" + x + "," + y + ")"; 65 } 66

Page 545: C# Language Specification

Annex E Documentation Comments

525

/// <summary> 1 /// This operator determines whether two Points have the same 2 /// location. 3 /// </summary> 4 /// <param name="p1">The first Point to be compared.</param> 5 /// <param name="p2">The second Point to be compared.</param> 6 /// <returns> 7 /// True if the Points have the same location; otherwise, false. 8 /// </returns> 9 /// <seealso cref="Equals"/> 10 /// <seealso cref="operator !="/> 11 public static bool operator ==(Point p1, Point p2) { 12 if ((object)p1 == null || (object)p2 == null) return false; 13 return p1.x == p2.x && p1.y == p2.y; 14 } 15

/// <summary> 16 /// This operator determines whether two Points have the same 17 /// location. 18 /// </summary> 19 /// <param name="p1">The first Point to be compared.</param> 20 /// <param name="p2">The second Point to be compared.</param> 21 /// <returns> 22 /// True if the Points do not have the same location; 23 /// otherwise, false. 24 /// </returns> 25 /// <seealso cref="Equals"/> 26 /// <seealso cref="operator =="/> 27 public static bool operator !=(Point p1, Point p2) { 28 return !(p1 == p2); 29 } 30

/// <summary> 31 /// <para> 32 /// This is the entry point of the Point class testing program. 33 /// </para> 34 /// <para> 35 /// This program tests each method and operator, and is intended 36 /// to be run after any non-trvial maintenance has been performed 37 /// on the Point class. 38 /// </para> 39 /// </summary> 40 public static void Main() { 41 // class test code goes here 42 } 43 } 44 } 45

E.4.2 Resulting XML 46

Here is the output produced by one documentation generator when given the source code for class Point, 47 shown above: 48

<?xml version="1.0"?> 49 <doc> 50 <assembly> 51 <name>Point</name> 52 </assembly> 53 <members> 54 <member name="T:Graphics.Point"> 55 <remarks> 56 Class <c>Point</c> models a point in a two-dimensional plane. 57 </remarks> 58 </member> 59 <member name="F:Graphics.Point.x"> 60 <summary> 61 Instance variable <c>x</c> represents the Point's x-coordinate. 62 </summary> 63 </member> 64 <member name="F:Graphics.Point.y"> 65

Page 546: C# Language Specification

C# LANGUAGE SPECIFICATION

526

<summary> 1 Instance variable <c>y</c> represents the Point's y-coordinate. 2 </summary> 3 </member> 4 <member name="M:Graphics.Point.#ctor"> 5 <summary> 6 This constructor initializes the new Point to (0,0). 7 </summary> 8 </member> 9 <member name="M:Graphics.Point.#ctor(System.Int32,System.Int32)"> 10 <summary> 11 This constructor initializes the new Point to 12 (<paramref name="xor"/>,<paramref name="yor"/>). 13 </summary> 14 <param name="xor">The new Point's x-coordinate.</param> 15 <param name="yor">The new Point's y-coordinate.</param> 16 </member> 17 <member name="M:Graphics.Point.Move(System.Int32,System.Int32)"> 18 <summary> 19 This method changes the point's location to the given 20 coordinates. 21 </summary> 22 <param name="xor">The new x-coordinate.</param> 23 <param name="yor">The new y-coordinate.</param> 24 <seealso cref="M:Graphics.Point.Translate(System.Int32,System.Int32)"/> 25 </member> 26 <member name="M:Graphics.Point.Translate(System.Int32,System.Int32)"> 27 <summary> 28 This method changes the point's location by the given 29 x- and y-offsets. 30 </summary> 31 <example> 32 The following code: 33 <code> 34 Point p = new Point(3,5); 35 p.Translate(-1,3); 36 </code> 37 results in <c>p</c>'s having the value (2,8). 38 </example> 39 <param name="xor">The relative x-offset.</param> 40 <param name="yor">The relative y-offset.</param> 41 <seealso cref="M:Graphics.Point.Move(System.Int32,System.Int32)"/> 42 </member> 43 <member name="M:Graphics.Point.Equals(System.Object)"> 44 <summary> 45 This method determines whether two Points have the same 46 location. 47 </summary> 48 <param name="o"> 49 The object to be compared to the current object. 50 </param> 51 <returns> 52 True if the Points have the same location; otherwise, false. 53 </returns> 54 <seealso 55 cref="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)"/> 56 <seealso 57 cref="M:Graphics.Point.op_Inequality(Graphics.Point,Graphics.Point)"/> 58 </member> 59 <member name="M:Graphics.Point.GetHashCode"> 60 <summary> 61 Computes the hash code for a Point. 62 </summary> 63 <returns> 64 A hash code computed from the x and y coordinates. 65 </returns> 66 </member> 67 <member name="M:Graphics.Point.ToString"> 68 <summary> 69

Page 547: C# Language Specification

Annex E Documentation Comments

527

Report a point's location as a string. 1 </summary> 2 <returns> 3 A string representing a point's location, in the form (x,y), 4 without any leading, training, or embedded whitespace. 5 </returns> 6 </member> 7 <member name="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)"> 8 <summary> 9 This operator determines whether two Points have the same 10 location. 11 </summary> 12 <param name="p1">The first Point to be compared.</param> 13 <param name="p2">The second Point to be compared.</param> 14 <returns> 15 True if the Points have the same location; otherwise, false. 16 </returns> 17 <seealso cref="M:Graphics.Point.Equals(System.Object)"/> 18 <seealso 19 cref="M:Graphics.Point.op_Inequality(Graphics.Point,Graphics.Point)"/> 20 </member> 21 <member 22 name="M:Graphics.Point.op_Inequality(Graphics.Point,Graphics.Point)"> 23 <summary> 24 This operator determines whether two Points have the same 25 location. 26 </summary> 27 <param name="p1">The first Point to be compared.</param> 28 <param name="p2">The second Point to be compared.</param> 29 <returns> 30 True if the Points do not have the same location; 31 otherwise, false. 32 </returns> 33 <seealso cref="M:Graphics.Point.Equals(System.Object)"/> 34 <seealso 35 cref="M:Graphics.Point.op_Equality(Graphics.Point,Graphics.Point)"/> 36 </member> 37 <member name="M:Graphics.Point.Main"> 38 <summary> 39 <para> 40 This is the entry point of the Point class testing program. 41 </para> 42 <para> 43 This program tests each method and operator, and is intended 44 to be run after any non-trvial maintenance has been performed 45 on the Point class. 46 </para> 47 </summary> 48 </member> 49 <member name="P:Graphics.Point.X"> 50 <value> 51 The Point's x-coordinate. 52 </value> 53 </member> 54 <member name="P:Graphics.Point.Y"> 55 <value> 56 The Point's y-coordinate. 57 </value> 58 </member> 59 </members> 60 </doc> 61 62 End of informative text. 63

Page 548: C# Language Specification

528

Annex F. Bibliography 1

This annex is informative. 2

3

ANSI X3.274-1996, Programming Language REXX. (This document is useful in understanding floating-4 point decimal arithmetic rules.) 5

ISO 31-0:1992, Annex B (informative), Guide to the rounding of numbers (This document defines “banker’s 6 rounding.”) 7

ISO/IEC 9899:1999, Programming languages — C. 8

ISO/IEC 14882:1998, Programming languages — C++. 9

10

End of informative text. 11


Recommended