# C Language Specification ppt

403 233 0
# C Language Specification ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

# C Language Specification Version 1.1.03 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Please send corrections, comments, and other feedback to sharp@microsoft.com Notice © 1999-2002 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 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Table of Contents Table of Contents Introduction 1.1 Getting started 1.2 Types 1.2.1 Predefined types 1.2.2 Conversions 1.2.3 Array types 1.2.4 Type system unification 1.3 Variables and parameters 1.4 Automatic memory management 12 1.5 Expressions 14 1.6 Statements 15 1.7 Classes 18 1.7.1 Constants 20 1.7.2 Fields 20 1.7.3 Methods 21 1.7.4 Properties 22 1.7.5 Events 23 1.7.6 Operators 24 1.7.7 Indexers 25 1.7.8 Instance constructors 26 1.7.9 Destructors 27 1.7.10 Static constructors 27 1.7.11 Inheritance 28 1.8 Structs 29 1.9 Interfaces 30 1.10 Delegates 31 1.11 Enums 32 1.12 Namespaces and assemblies 33 1.13 Versioning 34 1.14 Attributes 36 Lexical structure 39 2.1 Programs 39 2.2 Grammars 39 2.2.1 Grammar notation 39 2.2.2 Lexical grammar 40 2.2.3 Syntactic grammar 40 2.3 Lexical analysis 40 2.3.1 Line terminators 41 2.3.2 White space 41 2.3.3 Comments 41 2.4 Tokens 43 2.4.1 Unicode character escape sequences 43 2.4.2 Identifiers 44 2.4.3 Keywords 45 2.4.4 Literals 46 2.4.4.1 Boolean literals 46 2.4.4.2 Integer literals 46 2.4.4.3 Real literals 47 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved iii C# LANGUAGE SPECIFICATION 2.4.4.4 Character literals 48 2.4.4.5 String literals 49 2.4.4.6 The null literal 51 2.4.5 Operators and punctuators 51 2.5 Pre-processing directives 51 2.5.1 Conditional compilation symbols 52 2.5.2 Pre-processing expressions 53 2.5.3 Declaration directives 53 2.5.4 Conditional compilation directives 54 2.5.5 Diagnostic directives 57 2.5.6 Region directives 57 2.5.7 Line directives 58 Basic concepts 59 3.1 Application Startup 59 3.2 Application termination 60 3.3 Declarations 60 3.4 Members 62 3.4.1 Namespace members 62 3.4.2 Struct members 62 3.4.3 Enumeration members 63 3.4.4 Class members 63 3.4.5 Interface members 63 3.4.6 Array members 63 3.4.7 Delegate members 63 3.5 Member access 63 3.5.1 Declared accessibility 64 3.5.2 Accessibility domains 64 3.5.3 Protected access for instance members 67 3.5.4 Accessibility constraints 67 3.6 Signatures and overloading 68 3.7 Scopes 69 3.7.1 Name hiding 71 3.7.1.1 Hiding through nesting 72 3.7.1.2 Hiding through inheritance 72 3.8 Namespace and type names 73 3.8.1 Fully qualified names 75 3.9 Automatic memory management 75 3.10 Execution order 78 Types 79 4.1 Value types 79 4.1.1 The System.ValueType type 80 4.1.2 Default constructors 80 4.1.3 Struct types 81 4.1.4 Simple types 81 4.1.5 Integral types 82 4.1.6 Floating point types 83 4.1.7 The decimal type 84 4.1.8 The bool type 84 4.1.9 Enumeration types 84 iv Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Table of Contents 4.2 Reference types 85 4.2.1 Class types 85 4.2.2 The object type 86 4.2.3 The string type 86 4.2.4 Interface types 86 4.2.5 Array types 86 4.2.6 Delegate types 86 4.3 Boxing and unboxing 86 4.3.1 Boxing conversions 87 4.3.2 Unboxing conversions 88 Variables 89 5.1 Variable categories 89 5.1.1 Static variables 89 5.1.2 Instance variables 89 5.1.2.1 Instance variables in classes 89 5.1.2.2 Instance variables in structs 90 5.1.3 Array elements 90 5.1.4 Value parameters 90 5.1.5 Reference parameters 90 5.1.6 Output parameters 90 5.1.7 Local variables 91 5.2 Default values 91 5.3 Definite assignment 92 5.3.1 Initially assigned variables 92 5.3.2 Initially unassigned variables 93 5.3.3 Precise rules for determining definite assignment 93 5.3.3.1 General rules for statements 93 5.3.3.2 Block statements, checked, and unchecked statements 94 5.3.3.3 Expression statements 94 5.3.3.4 Declaration statements 94 5.3.3.5 If statements 94 5.3.3.6 Switch statements 95 5.3.3.7 While statements 95 5.3.3.8 Do statements 95 5.3.3.9 For statements 95 5.3.3.10 Break, continue, and goto statements 96 5.3.3.11 Throw statements 96 5.3.3.12 Return statements 96 5.3.3.13 Try-catch statements 96 5.3.3.14 Try-finally statements 96 5.3.3.15 Try-catch-finally statements 97 5.3.3.16 Foreach statements 97 5.3.3.17 Using statements 98 5.3.3.18 Lock statements 98 5.3.3.19 General rules for simple expressions 98 5.3.3.20 General rules for expressions with embedded expressions 98 5.3.3.21 Invocation expressions and object creation expressions 99 5.3.3.22 Simple assignment expressions 99 5.3.3.23 && expressions 99 5.3.3.24 || expressions 100 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved v C# LANGUAGE SPECIFICATION 5.3.3.25 ! expressions 101 5.3.3.26 ?: expressions 101 5.4 Variable references 102 5.5 Atomicity of variable references 102 Conversions 103 6.1 Implicit conversions 103 6.1.1 Identity conversion 103 6.1.2 Implicit numeric conversions 103 6.1.3 Implicit enumeration conversions 104 6.1.4 Implicit reference conversions 104 6.1.5 Boxing conversions 104 6.1.6 Implicit constant expression conversions 104 6.1.7 User-defined implicit conversions 105 6.2 Explicit conversions 105 6.2.1 Explicit numeric conversions 105 6.2.2 Explicit enumeration conversions 107 6.2.3 Explicit reference conversions 107 6.2.4 Unboxing conversions 108 6.2.5 User-defined explicit conversions 108 6.3 Standard conversions 108 6.3.1 Standard implicit conversions 108 6.3.2 Standard explicit conversions 108 6.4 User-defined conversions 108 6.4.1 Permitted user-defined conversions 108 6.4.2 Evaluation of user-defined conversions 109 6.4.3 User-defined implicit conversions 109 6.4.4 User-defined explicit conversions 110 Expressions 113 7.1 Expression classifications 113 7.1.1 Values of expressions 114 7.2 Operators 114 7.2.1 Operator precedence and associativity 114 7.2.2 Operator overloading 115 7.2.3 Unary operator overload resolution 116 7.2.4 Binary operator overload resolution 117 7.2.5 Candidate user-defined operators 117 7.2.6 Numeric promotions 117 7.2.6.1 Unary numeric promotions 118 7.2.6.2 Binary numeric promotions 118 7.3 Member lookup 119 7.3.1 Base types 119 7.4 Function members 119 7.4.1 Argument lists 122 7.4.2 Overload resolution 124 7.4.2.1 Applicable function member 124 7.4.2.2 Better function member 125 7.4.2.3 Better conversion 125 7.4.3 Function member invocation 126 7.4.3.1 Invocations on boxed instances 127 vi Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Table of Contents 7.5 Primary expressions 127 7.5.1 Literals 128 7.5.2 Simple names 128 7.5.2.1 Invariant meaning in blocks 129 7.5.3 Parenthesized expressions 130 7.5.4 Member access 130 7.5.4.1 Identical simple names and type names 132 7.5.5 Invocation expressions 132 7.5.5.1 Method invocations 132 7.5.5.2 Delegate invocations 133 7.5.6 Element access 134 7.5.6.1 Array access 134 7.5.6.2 Indexer access 134 7.5.7 This access 135 7.5.8 Base access 136 7.5.9 Postfix increment and decrement operators 136 7.5.10 The new operator 137 7.5.10.1 Object creation expressions 137 7.5.10.2 Array creation expressions 138 7.5.10.3 Delegate creation expressions 139 7.5.11 The typeof operator 141 7.5.12 The checked and unchecked operators 142 7.6 Unary operators 144 7.6.1 Unary plus operator 144 7.6.2 Unary minus operator 145 7.6.3 Logical negation operator 145 7.6.4 Bitwise complement operator 145 7.6.5 Prefix increment and decrement operators 146 7.6.6 Cast expressions 146 7.7 Arithmetic operators 147 7.7.1 Multiplication operator 147 7.7.2 Division operator 148 7.7.3 Remainder operator 149 7.7.4 Addition operator 150 7.7.5 Subtraction operator 152 7.8 Shift operators 153 7.9 Relational and type-testing operators 154 7.9.1 Integer comparison operators 155 7.9.2 Floating-point comparison operators 156 7.9.3 Decimal comparison operators 156 7.9.4 Boolean equality operators 157 7.9.5 Enumeration comparison operators 157 7.9.6 Reference type equality operators 157 7.9.7 String equality operators 158 7.9.8 Delegate equality operators 159 7.9.9 The is operator 159 7.9.10 The as operator 160 7.10 Logical operators 160 7.10.1 Integer logical operators 160 7.10.2 Enumeration logical operators 161 7.10.3 Boolean logical operators 161 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved vii C# LANGUAGE SPECIFICATION 7.11 Conditional logical operators 161 7.11.1 Boolean conditional logical operators 162 7.11.2 User-defined conditional logical operators 162 7.12 Conditional operator 163 7.13 Assignment operators 163 7.13.1 Simple assignment 164 7.13.2 Compound assignment 166 7.13.3 Event assignment 167 7.14 Expression 167 7.15 Constant expressions 167 7.16 Boolean expressions 168 Statements 169 8.1 End points and reachability 169 8.2 Blocks 171 8.2.1 Statement lists 171 8.3 The empty statement 171 8.4 Labeled statements 172 8.5 Declaration statements 172 8.5.1 Local variable declarations 173 8.5.2 Local constant declarations 173 8.6 Expression statements 174 8.7 Selection statements 174 8.7.1 The if statement 174 8.7.2 The switch statement 175 8.8 Iteration statements 178 8.8.1 The while statement 179 8.8.2 The statement 179 8.8.3 The for statement 180 8.8.4 The foreach statement 181 8.9 Jump statements 182 8.9.1 The break statement 183 8.9.2 The continue statement 184 8.9.3 The goto statement 184 8.9.4 The return statement 186 8.9.5 The throw statement 186 8.10 The try statement 187 8.11 The checked and unchecked statements 190 8.12 The lock statement 190 8.13 The using statement 191 Namespaces 195 9.1 Compilation units 195 9.2 Namespace declarations 195 9.3 Using directives 196 9.3.1 Using alias directives 197 9.3.2 Using namespace directives 199 9.4 Namespace members 201 9.5 Type declarations 201 10 Classes 203 10.1 Class declarations 203 viii Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Table of Contents 10.1.1 Class modifiers 203 10.1.1.1 Abstract classes 203 10.1.1.2 Sealed classes 204 10.1.2 Class base specification 204 10.1.2.1 Base classes 204 10.1.2.2 Interface implementations 206 10.1.3 Class body 206 10.2 Class members 206 10.2.1 Inheritance 207 10.2.2 The new modifier 208 10.2.3 Access modifiers 208 10.2.4 Constituent types 208 10.2.5 Static and instance members 208 10.2.6 Nested types 209 10.2.6.1 Fully qualified name 209 10.2.6.2 Declared accessibility 209 10.2.6.3 Hiding 210 10.2.6.4 this access 211 10.2.6.5 Access to private and protected members of the containing type 211 10.2.7 Reserved member names 212 10.2.7.1 Member names reserved for properties 213 10.2.7.2 Member names reserved for events 213 10.2.7.3 Member names reserved for indexers 213 10.2.7.4 Member names reserved for destructors 214 10.3 Constants 214 10.4 Fields 215 10.4.1 Static and instance fields 216 10.4.2 Readonly fields 217 10.4.2.1 Using static readonly fields for constants 217 10.4.2.2 Versioning of constants and static readonly fields 217 10.4.3 Volatile fields 218 10.4.4 Field initialization 219 10.4.5 Variable initializers 219 10.4.5.1 Static field initialization 220 10.4.5.2 Instance field initialization 221 10.5 Methods 222 10.5.1 Method parameters 223 10.5.1.1 Value parameters 224 10.5.1.2 Reference parameters 224 10.5.1.3 Output parameters 225 10.5.1.4 Parameter arrays 226 10.5.2 Static and instance methods 228 10.5.3 Virtual methods 229 10.5.4 Override methods 231 10.5.5 Sealed methods 232 10.5.6 Abstract methods 233 10.5.7 External methods 234 10.5.8 Method body 234 10.5.9 Method overloading 235 10.6 Properties 235 10.6.1 Static and instance properties 236 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved ix C# LANGUAGE SPECIFICATION 10.6.2 Accessors 237 10.6.3 Virtual, sealed, override, and abstract accessors 241 10.7 Events 242 10.7.1 Field-like events 244 10.7.2 Event accessors 246 10.7.3 Static and instance events 247 10.7.4 Virtual, sealed, override, and abstract accessors 247 10.8 Indexers 248 10.8.1 Indexer overloading 251 10.9 Operators 251 10.9.1 Unary operators 252 10.9.2 Binary operators 253 10.9.3 Conversion operators 253 10.10 Instance constructors 255 10.10.1 Constructor initializers 256 10.10.2 Instance variable initializers 256 10.10.3 Constructor execution 257 10.10.4 Default constructors 258 10.10.5 Private constructors 259 10.10.6 Optional instance constructor parameters 259 10.11 Static constructors 260 10.12 Destructors 261 11 Structs 265 11.1 Struct declarations 265 11.1.1 Struct modifiers 265 11.1.2 Struct interfaces 265 11.1.3 Struct body 266 11.2 Struct members 266 11.3 Class and struct differences 266 11.3.1 Value semantics 267 11.3.2 Inheritance 267 11.3.3 Assignment 267 11.3.4 Default values 268 11.3.5 Boxing and unboxing 268 11.3.6 Meaning of this 269 11.3.7 Field initializers 269 11.3.8 Constructors 269 11.3.9 Destructors 270 11.4 Struct examples 270 11.4.1 Database integer type 270 11.4.2 Database boolean type 272 12 Arrays 275 12.1 Array types 275 12.1.1 The System.Array type 276 12.2 Array creation 276 12.3 Array element access 276 12.4 Array members 276 12.5 Array covariance 276 12.6 Array initializers 277 x Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Appendix 0Grammar type-declaration: class-declaration struct-declaration interface-declaration enum-declaration delegate-declaration A.14.7 Classes class-declaration: attributesopt class-modifiersoptclass identifier class-baseopt class-body ;opt class-modifiers: class-modifier class-modifiers class-modifier class-modifier: new public protected internal private abstract sealed class-base: : class-type : interface-type-list : class-type , interface-type-list interface-type-list: interface-type interface-type-list , interface-type class-body: { class-member-declarationsopt} class-member-declarations: class-member-declaration class-member-declarations class-member-declaration class-member-declaration: constant-declaration field-declaration method-declaration property-declaration event-declaration indexer-declaration operator-declaration constructor-declaration destructor-declaration static-constructor-declaration type-declaration constant-declaration: attributesopt constant-modifiersoptconst type constant-declarators ; Copyright  Microsoft Corporation 1999-2002 All Rights Reserved 375 C# LANGUAGE SPECIFICATION constant-modifiers: constant-modifier constant-modifiers constant-modifier constant-modifier: new public protected internal private constant-declarators: constant-declarator constant-declarators , constant-declarator constant-declarator: identifier = constant-expression field-declaration: attributesopt field-modifiersopt type variable-declarators ; field-modifiers: field-modifier field-modifiers field-modifier field-modifier: new public protected internal private static readonly volatile variable-declarators: variable-declarator variable-declarators , variable-declarator variable-declarator: identifier identifier = variable-initializer variable-initializer: expression array-initializer method-declaration: method-header method-body method-header: attributesopt method-modifiersopt return-type member-name ( formal-parameter-listopt) method-modifiers: method-modifier method-modifiers method-modifier 376 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Appendix 0Grammar method-modifier: new public protected internal private static virtual sealed override abstract extern return-type: type void member-name: identifier interface-type identifier method-body: block ; formal-parameter-list: fixed-parameters fixed-parameters , parameter-array parameter-array fixed-parameters: fixed-parameter fixed-parameters , fixed-parameter fixed-parameter: attributesopt parameter-modifieropt type identifier parameter-modifier: ref out parameter-array: attributesoptparams array-type identifier property-declaration: attributesopt property-modifiersopt type member-name { accessor-declarations } property-modifiers: property-modifier property-modifiers property-modifier Copyright  Microsoft Corporation 1999-2002 All Rights Reserved 377 C# LANGUAGE SPECIFICATION property-modifier: new public protected internal private static virtual sealed override abstract extern member-name: identifier interface-type identifier accessor-declarations: get-accessor-declaration set-accessor-declarationopt set-accessor-declaration get-accessor-declarationopt get-accessor-declaration: attributesoptget accessor-body set-accessor-declaration: attributesoptset accessor-body accessor-body: block ; event-declaration: attributesopt event-modifiersoptevent type variable-declarators ; attributesopt event-modifiersoptevent type member-name { event-accessor-declarations } event-modifiers: event-modifier event-modifiers event-modifier event-modifier: new public protected internal private static virtual sealed override abstract extern event-accessor-declarations: add-accessor-declaration remove-accessor-declaration remove-accessor-declaration add-accessor-declaration 378 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Appendix 0Grammar add-accessor-declaration: attributesoptadd block remove-accessor-declaration: attributesoptremove block indexer-declaration: attributesopt indexer-modifiersopt indexer-declarator { accessor-declarations } indexer-modifiers: indexer-modifier indexer-modifiers indexer-modifier indexer-modifier: new public protected internal private virtual sealed override abstract extern indexer-declarator: type this[ formal-parameter-list ] type interface-type this[ formal-parameter-list ] operator-declaration: attributesopt operator-modifiers operator-declarator operator-body operator-modifiers: operator-modifier operator-modifiers operator-modifier operator-modifier: public static extern operator-declarator: unary-operator-declarator binary-operator-declarator conversion-operator-declarator unary-operator-declarator: type operator overloadable-unary-operator ( type identifier ) overloadable-unary-operator: one of + - ! ~ ++ true false binary-operator-declarator: type operator overloadable-binary-operator ( type identifier , type identifier ) overloadable-binary-operator: one of + - * / % & | ^ Copyright  Microsoft Corporation 1999-2002 All Rights Reserved == != >= identifier pointer-element-access: primary-no-array-creation-expression [ expression ] addressof-expression: & unary-expression sizeof-expression: sizeof( unmanaged-type ) embedded-statement: fixed-statement fixed-statement: fixed( pointer-type fixed-pointer-declarators ) embedded-statement fixed-pointer-declarators: fixed-pointer-declarator fixed-pointer-declarators , fixed-pointer-declarator fixed-pointer-declarator: identifier = fixed-pointer-initializer 386 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved Appendix 0Grammar fixed-pointer-initializer: & variable-reference expression local-variable-initializer: expression array-initializer stackalloc-initializer stackalloc-initializer: stackalloc unmanaged-type [ expression ] Copyright  Microsoft Corporation 1999-2002 All Rights Reserved 387 Appendix 0References References Unicode Consortium The Unicode Standard, Version 3.0 Addison-Wesley, Reading, Massachusetts, 2000, ISBN 0-201-616335-5 IEEE IEEE Standard for Binary Floating-Point Arithmetic ANSI/IEEE Standard 754-1985 Available from http://www.ieee.org ISO/IEC C++ ANSI/ISO/IEC 14882:1998 Copyright  Microsoft Corporation 1999-2002 All Rights Reserved 389 ... public class Stack { public static Stack Clone(Stack s) { } public static Stack Flip(Stack s) { } public object Pop() { } public void Push(object o) { } public override string ToString() { } } class... static fields: class Color { public static public static public static public static readonly readonly readonly readonly Color Color Color Color Red = new Color(0xFF, 0, 0); Blue = new Color(0,... write Copyright  Microsoft Corporation 1999-2002 All Rights Reserved 13 C# LANGUAGE SPECIFICATION “unsafe” code Such code can deal directly with pointer types and object addresses, however, C# requires

Ngày đăng: 15/03/2014, 17:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan