Professional Documents
Culture Documents
CSharp 5.0 Language Specification
CSharp 5.0 Language Specification
C
Language Specification
Version 4.0
Notice
1999-2012 Microsoft Corporation. All rights reserved.
Microsoft, Windows, Visual Basic, Visual C#, and Visual C++ are either registered trademarks or trademarks of Microsoft
Corporation in the U.S.A. and/or other countries/regions.
Other product and company names mentioned herein may be the trademarks of their respective owners.
Table of Contents
Table of Contents
1. Introduction......................................................................................................................................................1
1.1 Hello world...................................................................................................................................................1
1.2 Program structure.........................................................................................................................................2
1.3 Types and variables......................................................................................................................................4
1.4 Expressions..................................................................................................................................................6
1.5 Statements....................................................................................................................................................8
1.6 Classes and objects.....................................................................................................................................12
1.6.1 Members..............................................................................................................................................12
1.6.2 Accessibility........................................................................................................................................13
1.6.3 Type parameters...................................................................................................................................13
1.6.4 Base classes.........................................................................................................................................14
1.6.5 Fields...................................................................................................................................................14
1.6.6 Methods...............................................................................................................................................15
1.6.6.1 Parameters.....................................................................................................................................15
1.6.6.2 Method body and local variables...................................................................................................16
1.6.6.3 Static and instance methods..........................................................................................................17
1.6.6.4 Virtual, override, and abstract methods.........................................................................................18
1.6.6.5 Method overloading......................................................................................................................20
1.6.7 Other function members......................................................................................................................21
1.6.7.1 Constructors..................................................................................................................................22
1.6.7.2 Properties......................................................................................................................................23
1.6.7.3 Indexers.........................................................................................................................................23
1.6.7.4 Events............................................................................................................................................24
1.6.7.5 Operators.......................................................................................................................................24
1.6.7.6 Destructors....................................................................................................................................25
1.7 Structs.........................................................................................................................................................25
1.8 Arrays.........................................................................................................................................................26
1.9 Interfaces....................................................................................................................................................27
1.10 Enums.......................................................................................................................................................29
1.11 Delegates..................................................................................................................................................30
1.12 Attributes..................................................................................................................................................31
2. Lexical structure............................................................................................................................................33
2.1 Programs....................................................................................................................................................33
2.2 Grammars...................................................................................................................................................33
2.2.1 Grammar notation................................................................................................................................33
2.2.2 Lexical grammar..................................................................................................................................34
2.2.3 Syntactic grammar...............................................................................................................................34
2.3 Lexical analysis..........................................................................................................................................34
2.3.1 Line terminators...................................................................................................................................35
2.3.2 Comments............................................................................................................................................35
2.3.3 White space..........................................................................................................................................37
2.4 Tokens........................................................................................................................................................37
2.4.1 Unicode character escape sequences....................................................................................................37
2.4.2 Identifiers.............................................................................................................................................38
2.4.3 Keywords.............................................................................................................................................39
2.4.4 Literals.................................................................................................................................................40
2.4.4.1 Boolean literals.............................................................................................................................40
2.4.4.2 Integer literals...............................................................................................................................40
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
Table of Contents
B.2.4 Expressions.......................................................................................................................................477
B.2.5 Statements.........................................................................................................................................484
B.2.6 Namespaces.......................................................................................................................................487
B.2.7 Classes..............................................................................................................................................488
B.2.8 Structs...............................................................................................................................................495
B.2.9 Arrays................................................................................................................................................496
B.2.10 Interfaces.........................................................................................................................................496
B.2.11 Enums.............................................................................................................................................497
B.2.12 Delegates.........................................................................................................................................498
B.2.13 Attributes.........................................................................................................................................498
B.3 Grammar extensions for unsafe code.......................................................................................................500
C. References....................................................................................................................................................503