ECMA C# Language Specification.pdf

(1060 KB) Pobierz
104465700 UNPDF
ECMA/TC39/TG2/2000/3
Draft S tandard ECMA-xxx
October 2000
Standardizing Information and Communication Systems
C# Language Specification
Draft 01 – October 2000
Phone: +41 22 849.60.00 - Fax: +41 22 849.60.01 - URL: http://www.ecma.ch - Internet: helpdesk@ecma.ch
104465700.001.png
Table of Contents
Table of Contents
1. Introduction....................................................................................................................................................... 1
1.1 Getting started ............................................................................................................................................... 1
1.2 Types ............................................................................................................................................................. 2
1.2.1 Predefined types...................................................................................................................................... 4
1.2.2 Conversions ............................................................................................................................................ 6
1.2.3 Array types.............................................................................................................................................. 7
1.2.4 Type system unification .......................................................................................................................... 9
1.3 Variables and parameters............................................................................................................................... 9
1.4 Automatic memory management................................................................................................................. 13
1.5 Expressions.................................................................................................................................................. 15
1.6 Statements.................................................................................................................................................... 16
1.7 Classes ......................................................................................................................................................... 19
1.7.1 Constants............................................................................................................................................... 22
1.7.2 Fields..................................................................................................................................................... 22
1.7.3 Methods ................................................................................................................................................ 23
1.7.4 Properties .............................................................................................................................................. 24
1.7.5 Events.................................................................................................................................................... 25
1.7.6 Operators............................................................................................................................................... 26
1.7.7 Indexers................................................................................................................................................. 28
1.7.8 Instance constructors............................................................................................................................. 29
1.7.9 Destructors ............................................................................................................................................ 30
1.7.10 Static constructors............................................................................................................................... 31
1.7.11 Inheritance .......................................................................................................................................... 31
1.8 Structs .......................................................................................................................................................... 33
1.9 Interfaces ..................................................................................................................................................... 33
1.10 Delegates ................................................................................................................................................... 35
1.11 Enums ........................................................................................................................................................ 36
1.12 Namespaces and assemblies ...................................................................................................................... 37
1.13 Versioning ................................................................................................................................................. 38
1.14 Attributes ................................................................................................................................................... 41
2. Lexical structure.............................................................................................................................................. 43
2.1 Phases of translation .................................................................................................................................... 43
2.2 Grammar notation........................................................................................................................................ 43
2.3 Lexical analysis ........................................................................................................................................... 44
2.3.1 Input ...................................................................................................................................................... 44
2.3.2 Input characters..................................................................................................................................... 44
2.3.3 Line terminators .................................................................................................................................... 44
2.3.4 Comments ............................................................................................................................................. 45
2.3.5 White space........................................................................................................................................... 46
2.4 Tokens ......................................................................................................................................................... 46
2.4.1 Unicode character escape sequences..................................................................................................... 46
2.4.2 Identifiers .............................................................................................................................................. 47
2.4.3 Keywords .............................................................................................................................................. 48
2.4.4 Literals .................................................................................................................................................. 49
2.4.4.1 Boolean literals............................................................................................................................... 49
2.4.4.2 Integer literals................................................................................................................................. 49
2.4.4.3 Real literals..................................................................................................................................... 50
2.4.4.4 Character literals ............................................................................................................................ 51
iii
C# LANGUAGE SPECIFICATION
2.4.4.5 String literals .................................................................................................................................. 52
2.4.4.6 The null literal ................................................................................................................................ 54
2.4.5 Operators and punctuators .................................................................................................................... 54
2.5 Pre-processing directives............................................................................................................................. 54
2.5.1 Pre-processing identifiers...................................................................................................................... 54
2.5.2 Pre-processing expressions ................................................................................................................... 54
2.5.3 Pre-processing declarations .................................................................................................................. 55
2.5.4 #if, #elif, #else, #endif .......................................................................................................................... 56
2.5.5 #error and #warning.............................................................................................................................. 59
2.5.6 #region and #endregion ........................................................................................................................ 59
2.5.7 #line ...................................................................................................................................................... 59
3. Basic concepts.................................................................................................................................................. 61
3.1 Program Startup........................................................................................................................................... 61
3.2 Program Termination................................................................................................................................... 62
3.3 Declarations ................................................................................................................................................. 62
3.4 Members ...................................................................................................................................................... 64
3.4.1 Namespace members ............................................................................................................................ 64
3.4.2 Struct members ..................................................................................................................................... 65
3.4.3 Enumeration members .......................................................................................................................... 65
3.4.4 Class members ...................................................................................................................................... 65
3.4.5 Interface members................................................................................................................................. 65
3.4.6 Array members ..................................................................................................................................... 65
3.4.7 Delegate members................................................................................................................................. 66
3.5 Member access ............................................................................................................................................ 66
3.5.1 Declared accessibility ........................................................................................................................... 66
3.5.2 Accessibility domains ........................................................................................................................... 67
3.5.3 Protected access .................................................................................................................................... 69
3.5.4 Accessibility constraints ....................................................................................................................... 70
3.6 Signatures and overloading ......................................................................................................................... 70
3.7 Scopes.......................................................................................................................................................... 71
3.7.1 Name hiding.......................................................................................................................................... 73
3.7.1.1 Hiding through nesting................................................................................................................... 74
3.7.1.2 Hiding through inheritance............................................................................................................. 74
3.8 Namespace and type names ......................................................................................................................... 76
3.8.1 Fully qualified names............................................................................................................................ 77
4. Types ................................................................................................................................................................ 79
4.1 Value types .................................................................................................................................................. 79
4.1.1 Default constructors .............................................................................................................................. 80
4.1.2 Struct types ........................................................................................................................................... 81
4.1.3 Simple types.......................................................................................................................................... 81
4.1.4 Integral types......................................................................................................................................... 82
4.1.5 Floating point types .............................................................................................................................. 83
4.1.6 The decimal type................................................................................................................................... 84
4.1.7 The bool type........................................................................................................................................ 85
4.1.8 Enumeration types ................................................................................................................................ 85
4.2 Reference types ........................................................................................................................................... 85
4.2.1 Class types ............................................................................................................................................ 86
4.2.2 The object type...................................................................................................................................... 86
4.2.3 The string type ...................................................................................................................................... 86
iv
Table of Contents
4.2.4 Interface types....................................................................................................................................... 87
4.2.5 Array types............................................................................................................................................ 87
4.2.6 Delegate types....................................................................................................................................... 87
4.3 Boxing and unboxing .................................................................................................................................. 87
4.3.1 Boxing conversions............................................................................................................................... 87
4.3.2 Unboxing conversions .......................................................................................................................... 88
5. Variables .......................................................................................................................................................... 91
5.1 Variable categories ...................................................................................................................................... 91
5.1.1 Static variables ...................................................................................................................................... 91
5.1.2 Instance variables.................................................................................................................................. 91
5.1.2.1 Instance variables in classes ........................................................................................................... 92
5.1.2.2 Instance variables in structs............................................................................................................ 92
5.1.3 Array elements ...................................................................................................................................... 92
5.1.4 Value parameters .................................................................................................................................. 92
5.1.5 Reference parameters............................................................................................................................ 92
5.1.6 Output parameters................................................................................................................................. 92
5.1.7 Local variables ...................................................................................................................................... 93
5.2 Default values .............................................................................................................................................. 93
5.3 Definite assignment ..................................................................................................................................... 94
5.3.1 Initially assigned variables.................................................................................................................... 96
5.3.2 Initially unassigned variables................................................................................................................ 96
5.4 Variable references ...................................................................................................................................... 96
6. Conversions...................................................................................................................................................... 97
6.1 Implicit conversions .................................................................................................................................... 97
6.1.1 Identity conversion ............................................................................................................................... 97
6.1.2 Implicit numeric conversions................................................................................................................ 97
6.1.3 Implicit enumeration conversions......................................................................................................... 98
6.1.4 Implicit reference conversions .............................................................................................................. 98
6.1.5 Boxing conversions............................................................................................................................... 98
6.1.6 Implicit constant expression conversions ............................................................................................. 98
6.1.7 User-defined implicit conversions ........................................................................................................ 99
6.2 Explicit conversions .................................................................................................................................... 99
6.2.1 Explicit numeric conversions................................................................................................................ 99
6.2.2 Explicit enumeration conversions....................................................................................................... 100
6.2.3 Explicit reference conversions ............................................................................................................ 100
6.2.4 Unboxing conversions ........................................................................................................................ 101
6.2.5 User-defined explicit conversions....................................................................................................... 101
6.3 Standard conversions................................................................................................................................. 101
6.3.1 Standard implicit conversions............................................................................................................. 101
6.3.2 Standard explicit conversions ............................................................................................................. 102
6.4 User-defined conversions .......................................................................................................................... 102
6.4.1 Permitted user-defined conversions .................................................................................................... 102
6.4.2 Evaluation of user-defined conversions.............................................................................................. 102
6.4.3 User-defined implicit conversions ...................................................................................................... 103
6.4.4 User-defined explicit conversions....................................................................................................... 104
7. Expressions .................................................................................................................................................... 105
7.1 Expression classifications.......................................................................................................................... 105
7.1.1 Values of expressions ......................................................................................................................... 106
7.2 Operators ................................................................................................................................................... 106
v
Zgłoś jeśli naruszono regulamin