Tài liệu lập trình C# - Ngôn ngữ kỹ thuật
lượt xem 162
download
C # (phát âm là "Xem Sharp") là một đơn giản, hiện đại, ngôn ngữ lập trình hướng đối tượng, và kiểu an toàn. C # có nguồn gốc từ gia đình C của ngôn ngữ và sẽ được ngay lập tức quen thuộc với C, C + +, và các lập trình Java. C # được chuẩn hóa ECMA quốc tế như là tiêu chuẩn ECMA-334 và theo tiêu chuẩn ISO / IEC là ISO / IEC 23270 tiêu chuẩn. Microsoft C # trình biên dịch cho. NET Framework là một thực hiện phù hợp của cả hai tiêu chuẩn....
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Tài liệu lập trình C# - Ngôn ngữ kỹ thuật
- # C Language Specification Version 4.0 Copyright © Microsoft Corporation 1999-2010. All Rights Reserved. Please send corrections, comments, and other feedback to csharp@microsoft.com
- Notice © 1999-2010 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-2010. All Rights Reserved.
- Table of Contents Table of Contents Introduction..........................................................................................................................................................1 1.1 Hello world...................................................................................................................................................1 1.2 Program structure.........................................................................................................................................2 1.3 Types and variables......................................................................................................................................4 1.4 Expressions..................................................................................................................................................7 1.5 Statements....................................................................................................................................................9 1.6 Classes and objects.....................................................................................................................................14 1.6.1 Members..............................................................................................................................................15 1.6.2 Accessibility........................................................................................................................................15 1.6.3 Type parameters...................................................................................................................................16 1.6.4 Base classes.........................................................................................................................................16 1.6.5 Fields...................................................................................................................................................17 1.6.6 Methods...............................................................................................................................................17 1.6.6.1 Parameters.....................................................................................................................................18 1.6.6.2 Method body and local variables...................................................................................................19 1.6.6.3 Static and instance methods...........................................................................................................20 1.6.6.4 Virtual, override, and abstract methods.........................................................................................21 1.6.6.5 Method overloading......................................................................................................................23 1.6.7 Other function members.......................................................................................................................24 1.6.7.1 Constructors..................................................................................................................................26 1.6.7.2 Properties......................................................................................................................................27 1.6.7.3 Indexers.........................................................................................................................................27 1.6.7.4 Events............................................................................................................................................28 1.6.7.5 Operators.......................................................................................................................................28 1.6.7.6 Destructors....................................................................................................................................29 1.7 Structs.........................................................................................................................................................29 1.8 Arrays.........................................................................................................................................................30 1.9 Interfaces....................................................................................................................................................32 1.10 Enums.......................................................................................................................................................33 1.11 Delegates..................................................................................................................................................35 1.12 Attributes..................................................................................................................................................36 Lexical structure................................................................................................................................................38 1.13 Programs..................................................................................................................................................38 1.14 Grammars.................................................................................................................................................38 1.14.1 Grammar notation..............................................................................................................................38 1.14.2 Lexical grammar................................................................................................................................39 1.14.3 Syntactic grammar.............................................................................................................................39 1.15 Lexical analysis........................................................................................................................................39 1.15.1 Line terminators.................................................................................................................................40 1.15.2 Comments..........................................................................................................................................40 1.15.3 White space........................................................................................................................................42 1.16 Tokens......................................................................................................................................................42 1.16.1 Unicode character escape sequences..................................................................................................42 1.16.2 Identifiers...........................................................................................................................................43 1.16.3 Keywords...........................................................................................................................................45 1.16.4 Literals...............................................................................................................................................45 1.16.4.1 Boolean literals............................................................................................................................45 1.16.4.2 Integer literals..............................................................................................................................46 iii Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 1.16.4.3 Real literals..................................................................................................................................47 1.16.4.4 Character literals..........................................................................................................................47 1.16.4.5 String literals...............................................................................................................................48 1.16.4.6 The null literal.............................................................................................................................50 1.16.5 Operators and punctuators..................................................................................................................50 1.17 Pre-processing directives..........................................................................................................................51 1.17.1 Conditional compilation symbols.......................................................................................................52 1.17.2 Pre-processing expressions................................................................................................................52 1.17.3 Declaration directives........................................................................................................................53 1.17.4 Conditional compilation directives....................................................................................................54 1.17.5 Diagnostic directives..........................................................................................................................57 1.17.6 Region directives...............................................................................................................................57 1.17.7 Line directives....................................................................................................................................57 1.17.8 Pragma directives...............................................................................................................................58 1.17.8.1 Pragma warning...........................................................................................................................58 Basic concepts.....................................................................................................................................................60 1.18 Application Startup...................................................................................................................................60 1.19 Application termination............................................................................................................................61 1.20 Declarations..............................................................................................................................................61 1.21 Members...................................................................................................................................................63 1.21.1 Namespace members..........................................................................................................................64 1.21.2 Struct members..................................................................................................................................64 1.21.3 Enumeration members.......................................................................................................................64 1.21.4 Class members...................................................................................................................................64 1.21.5 Interface members..............................................................................................................................65 1.21.6 Array members..................................................................................................................................65 1.21.7 Delegate members..............................................................................................................................65 1.22 Member access.........................................................................................................................................65 1.22.1 Declared accessibility........................................................................................................................65 1.22.2 Accessibility domains........................................................................................................................66 1.22.3 Protected access for instance members..............................................................................................68 1.22.4 Accessibility constraints....................................................................................................................70 1.23 Signatures and overloading.......................................................................................................................71 1.24 Scopes......................................................................................................................................................72 1.24.1 Name hiding.......................................................................................................................................75 1.24.1.1 Hiding through nesting................................................................................................................75 1.24.1.2 Hiding through inheritance..........................................................................................................75 1.25 Namespace and type names......................................................................................................................77 1.25.1 Fully qualified names.........................................................................................................................79 1.26 Automatic memory management..............................................................................................................79 1.27 Execution order........................................................................................................................................82 Types...................................................................................................................................................................84 1.28 Value types...............................................................................................................................................84 1.28.1 The System.ValueType type..............................................................................................................85 1.28.2 Default constructors...........................................................................................................................85 1.28.3 Struct types........................................................................................................................................86 1.28.4 Simple types.......................................................................................................................................86 1.28.5 Integral types......................................................................................................................................87 1.28.6 Floating point types............................................................................................................................88 iv Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Table of Contents 1.28.7 The decimal type................................................................................................................................89 1.28.8 The bool type.....................................................................................................................................90 1.28.9 Enumeration types.............................................................................................................................90 1.28.10 Nullable types..................................................................................................................................90 1.29 Reference types........................................................................................................................................91 1.29.1 Class types.........................................................................................................................................91 1.29.2 The object type...................................................................................................................................92 1.29.3 The dynamic type...............................................................................................................................92 1.29.4 The string type...................................................................................................................................92 1.29.5 Interface types....................................................................................................................................92 1.29.6 Array types.........................................................................................................................................92 1.29.7 Delegate types....................................................................................................................................92 1.30 Boxing and unboxing...............................................................................................................................93 1.30.1 Boxing conversions............................................................................................................................93 1.30.2 Unboxing conversions........................................................................................................................94 1.31 Constructed types.....................................................................................................................................95 1.31.1 Type arguments..................................................................................................................................96 1.31.2 Open and closed types........................................................................................................................96 1.31.3 Bound and unbound types..................................................................................................................96 1.31.4 Satisfying constraints.........................................................................................................................97 1.32 Type parameters.......................................................................................................................................98 1.33 Expression tree types................................................................................................................................98 1.34 The dynamic type.....................................................................................................................................99 Variables...........................................................................................................................................................100 1.35 Variable categories.................................................................................................................................100 1.35.1 Static variables.................................................................................................................................100 1.35.2 Instance variables.............................................................................................................................100 1.35.2.1 Instance variables in classes......................................................................................................100 1.35.2.2 Instance variables in structs.......................................................................................................101 1.35.3 Array elements.................................................................................................................................101 1.35.4 Value parameters.............................................................................................................................101 1.35.5 Reference parameters.......................................................................................................................101 1.35.6 Output parameters............................................................................................................................101 1.35.7 Local variables.................................................................................................................................102 1.36 Default values.........................................................................................................................................103 1.37 Definite assignment................................................................................................................................103 1.37.1 Initially assigned variables...............................................................................................................104 1.37.2 Initially unassigned variables...........................................................................................................104 1.37.3 Precise rules for determining definite assignment............................................................................104 1.37.3.1 General rules for statements......................................................................................................105 1.37.3.2 Block statements, checked, and unchecked statements..............................................................105 1.37.3.3 Expression statements...............................................................................................................105 1.37.3.4 Declaration statements...............................................................................................................105 1.37.3.5 If statements..............................................................................................................................105 1.37.3.6 Switch statements......................................................................................................................106 1.37.3.7 While statements.......................................................................................................................106 1.37.3.8 Do statements............................................................................................................................106 1.37.3.9 For statements...........................................................................................................................106 1.37.3.10 Break, continue, and goto statements......................................................................................107 1.37.3.11 Throw statements....................................................................................................................107 v Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 1.37.3.12 Return statements....................................................................................................................107 1.37.3.13 Try-catch statements................................................................................................................107 1.37.3.14 Try-finally statements..............................................................................................................108 1.37.3.15 Try-catch-finally statements....................................................................................................108 1.37.3.16 Foreach statements..................................................................................................................109 1.37.3.17 Using statements......................................................................................................................109 1.37.3.18 Lock statements.......................................................................................................................109 1.37.3.19 Yield statements......................................................................................................................110 1.37.3.20 General rules for simple expressions.......................................................................................110 1.37.3.21 General rules for expressions with embedded expressions......................................................110 1.37.3.22 Invocation expressions and object creation expressions..........................................................110 1.37.3.23 Simple assignment expressions...............................................................................................111 1.37.3.24 && expressions.......................................................................................................................111 1.37.3.25 || expressions............................................................................................................................112 1.37.3.26 ! expressions............................................................................................................................113 1.37.3.27 ?? expressions..........................................................................................................................113 1.37.3.28 ?: expressions..........................................................................................................................113 1.37.3.29 Anonymous functions..............................................................................................................114 1.38 Variable references.................................................................................................................................115 1.39 Atomicity of variable references.............................................................................................................115 Conversions......................................................................................................................................................116 1.40 Implicit conversions...............................................................................................................................116 1.40.1 Identity conversion...........................................................................................................................116 1.40.2 Implicit numeric conversions...........................................................................................................117 1.40.3 Implicit enumeration conversions....................................................................................................117 1.40.4 Implicit nullable conversions...........................................................................................................117 1.40.5 Null literal conversions....................................................................................................................118 1.40.6 Implicit reference conversions.........................................................................................................118 1.40.7 Boxing conversions..........................................................................................................................118 1.40.8 Implicit dynamic conversions..........................................................................................................119 1.40.9 Implicit constant expression conversions.........................................................................................119 1.40.10 Implicit conversions involving type parameters.............................................................................119 1.40.11 User-defined implicit conversions..................................................................................................120 1.40.12 Anonymous function conversions and method group conversions.................................................120 1.41 Explicit conversions...............................................................................................................................120 1.41.1 Explicit numeric conversions...........................................................................................................121 1.41.2 Explicit enumeration conversions....................................................................................................122 1.41.3 Explicit nullable conversions...........................................................................................................122 1.41.4 Explicit reference conversions.........................................................................................................123 1.41.5 Unboxing conversions......................................................................................................................124 1.41.6 Explicit dynamic conversions..........................................................................................................124 1.41.7 Explicit conversions involving type parameters...............................................................................125 1.41.8 User-defined explicit conversions....................................................................................................126 1.42 Standard conversions..............................................................................................................................126 1.42.1 Standard implicit conversions..........................................................................................................126 1.42.2 Standard explicit conversions...........................................................................................................126 1.43 User-defined conversions.......................................................................................................................126 1.43.1 Permitted user-defined conversions.................................................................................................126 1.43.2 Lifted conversion operators..............................................................................................................127 1.43.3 Evaluation of user-defined conversions...........................................................................................127 vi Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Table of Contents 1.43.4 User-defined implicit conversions....................................................................................................128 1.43.5 User-defined explicit conversions....................................................................................................129 1.44 Anonymous function conversions...........................................................................................................129 1.44.1 Evaluation of anonymous function conversions to delegate types...................................................131 1.44.2 Evaluation of anonymous function conversions to expression tree types.........................................131 1.44.3 Implementation example..................................................................................................................131 1.45 Method group conversions.....................................................................................................................134 Expressions.......................................................................................................................................................137 1.46 Expression classifications.......................................................................................................................137 1.46.1 Values of expressions.......................................................................................................................138 1.47 Static and Dynamic Binding...................................................................................................................138 1.47.1 Binding-time....................................................................................................................................139 1.47.2 Dynamic binding..............................................................................................................................139 1.47.3 Types of constituent expressions......................................................................................................139 1.48 Operators................................................................................................................................................140 1.48.1 Operator precedence and associativity.............................................................................................140 1.48.2 Operator overloading.......................................................................................................................141 1.48.3 Unary operator overload resolution..................................................................................................143 1.48.4 Binary operator overload resolution.................................................................................................143 1.48.5 Candidate user-defined operators.....................................................................................................143 1.48.6 Numeric promotions........................................................................................................................143 1.48.6.1 Unary numeric promotions........................................................................................................144 1.48.6.2 Binary numeric promotions.......................................................................................................144 1.48.7 Lifted operators................................................................................................................................145 1.49 Member lookup......................................................................................................................................145 1.49.1 Base types........................................................................................................................................147 1.50 Function members..................................................................................................................................147 1.50.1 Argument lists..................................................................................................................................149 1.50.1.1 Corresponding parameters.........................................................................................................150 1.50.1.2 Run-time evaluation of argument lists.......................................................................................151 1.50.2 Type inference.................................................................................................................................153 1.50.2.1 The first phase...........................................................................................................................154 1.50.2.2 The second phase.......................................................................................................................154 1.50.2.3 Input types.................................................................................................................................154 1.50.2.4 Output types.............................................................................................................................154 1.50.2.5 Dependence...............................................................................................................................154 1.50.2.6 Output type inferences...............................................................................................................154 1.50.2.7 Explicit parameter type inferences............................................................................................155 1.50.2.8 Exact inferences........................................................................................................................155 1.50.2.9 Lower-bound inferences............................................................................................................155 1.50.2.10 Upper-bound inferences..........................................................................................................156 1.50.2.11 Fixing......................................................................................................................................156 1.50.2.12 Inferred return type..................................................................................................................157 1.50.2.13 Type inference for conversion of method groups....................................................................158 1.50.2.14 Finding the best common type of a set of expressions.............................................................158 1.50.3 Overload resolution..........................................................................................................................158 1.50.3.1 Applicable function member.....................................................................................................159 1.50.3.2 Better function member.............................................................................................................159 1.50.3.3 Better conversion from expression............................................................................................160 1.50.3.4 Better conversion from type......................................................................................................161 vii Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 1.50.3.5 Better conversion target.............................................................................................................161 1.50.3.6 Overloading in generic classes..................................................................................................161 1.50.4 Compile-time checking of dynamic overload resolution..................................................................162 1.50.5 Function member invocation............................................................................................................162 1.50.5.1 Invocations on boxed instances.................................................................................................163 1.51 Primary expressions................................................................................................................................163 1.51.1 Literals.............................................................................................................................................164 1.51.2 Simple names...................................................................................................................................164 1.51.2.1 Invariant meaning in blocks.......................................................................................................165 1.51.3 Parenthesized expressions................................................................................................................167 1.51.4 Member access.................................................................................................................................167 1.51.4.1 Identical simple names and type names.....................................................................................168 1.51.4.2 Grammar ambiguities................................................................................................................169 1.51.5 Invocation expressions.....................................................................................................................170 1.51.5.1 Method invocations...................................................................................................................170 1.51.5.2 Extension method invocations...................................................................................................171 1.51.5.3 Delegate invocations.................................................................................................................174 1.51.6 Element access.................................................................................................................................174 1.51.6.1 Array access..............................................................................................................................175 1.51.6.2 Indexer access...........................................................................................................................175 1.51.7 This access.......................................................................................................................................176 1.51.8 Base access......................................................................................................................................177 1.51.9 Postfix increment and decrement operators......................................................................................177 1.51.10 The new operator...........................................................................................................................178 1.51.10.1 Object creation expressions.....................................................................................................178 1.51.10.2 Object initializers....................................................................................................................180 1.51.10.3 Collection initializers...............................................................................................................182 1.51.10.4 Array creation expressions......................................................................................................183 1.51.10.5 Delegate creation expressions..................................................................................................185 1.51.10.6 Anonymous object creation expressions..................................................................................186 1.51.11 The typeof operator........................................................................................................................188 1.51.12 The checked and unchecked operators...........................................................................................190 1.51.13 Default value expressions..............................................................................................................192 1.51.14 Anonymous method expressions....................................................................................................192 1.52 Unary operators......................................................................................................................................193 1.52.1 Unary plus operator..........................................................................................................................193 1.52.2 Unary minus operator.......................................................................................................................193 1.52.3 Logical negation operator................................................................................................................194 1.52.4 Bitwise complement operator...........................................................................................................194 1.52.5 Prefix increment and decrement operators.......................................................................................194 1.52.6 Cast expressions...............................................................................................................................195 1.53 Arithmetic operators...............................................................................................................................196 1.53.1 Multiplication operator....................................................................................................................196 1.53.2 Division operator.............................................................................................................................197 1.53.3 Remainder operator..........................................................................................................................198 1.53.4 Addition operator.............................................................................................................................199 1.53.5 Subtraction operator.........................................................................................................................201 1.54 Shift operators........................................................................................................................................203 1.55 Relational and type-testing operators......................................................................................................204 1.55.1 Integer comparison operators...........................................................................................................205 1.55.2 Floating-point comparison operators................................................................................................206 viii Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Table of Contents 1.55.3 Decimal comparison operators.........................................................................................................206 1.55.4 Boolean equality operators...............................................................................................................207 1.55.5 Enumeration comparison operators..................................................................................................207 1.55.6 Reference type equality operators....................................................................................................207 1.55.7 String equality operators..................................................................................................................209 1.55.8 Delegate equality operators..............................................................................................................209 1.55.9 Equality operators and null..............................................................................................................210 1.55.10 The is operator...............................................................................................................................210 1.55.11 The as operator...............................................................................................................................210 1.56 Logical operators....................................................................................................................................211 1.56.1 Integer logical operators...................................................................................................................212 1.56.2 Enumeration logical operators.........................................................................................................212 1.56.3 Boolean logical operators.................................................................................................................212 1.56.4 Nullable boolean logical operators...................................................................................................212 1.57 Conditional logical operators..................................................................................................................213 1.57.1 Boolean conditional logical operators..............................................................................................214 1.57.2 User-defined conditional logical operators.......................................................................................214 1.58 The null coalescing operator...................................................................................................................214 1.59 Conditional operator...............................................................................................................................215 1.60 Anonymous function expressions...........................................................................................................216 1.60.1 Anonymous function signatures.......................................................................................................218 1.60.2 Anonymous function bodies.............................................................................................................218 1.60.3 Overload resolution..........................................................................................................................219 1.60.4 Anonymous functions and dynamic binding....................................................................................220 1.60.5 Outer variables.................................................................................................................................220 1.60.5.1 Captured outer variables............................................................................................................220 1.60.5.2 Instantiation of local variables...................................................................................................221 1.60.6 Evaluation of anonymous function expressions...............................................................................223 1.61 Query expressions..................................................................................................................................223 1.61.1 Ambiguities in query expressions....................................................................................................224 1.61.2 Query expression translation............................................................................................................225 1.61.2.1 Select and groupby clauses with continuations..........................................................................225 1.61.2.2 Explicit range variable types.....................................................................................................226 1.61.2.3 Degenerate query expressions...................................................................................................226 1.61.2.4 From, let, where, join and orderby clauses................................................................................227 1.61.2.5 Select clauses.............................................................................................................................230 1.61.2.6 Groupby clauses........................................................................................................................231 1.61.2.7 Transparent identifiers...............................................................................................................231 1.61.3 The query expression pattern...........................................................................................................232 1.62 Assignment operators.............................................................................................................................234 1.62.1 Simple assignment...........................................................................................................................234 1.62.2 Compound assignment.....................................................................................................................236 1.62.3 Event assignment.............................................................................................................................237 1.63 Expression..............................................................................................................................................238 1.64 Constant expressions..............................................................................................................................238 1.65 Boolean expressions...............................................................................................................................239 Statements........................................................................................................................................................241 1.66 End points and reachability....................................................................................................................241 1.67 Blocks.....................................................................................................................................................243 1.67.1 Statement lists..................................................................................................................................243 ix Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 1.68 The empty statement...............................................................................................................................244 1.69 Labeled statements.................................................................................................................................244 1.70 Declaration statements............................................................................................................................245 1.70.1 Local variable declarations..............................................................................................................245 1.70.2 Local constant declarations..............................................................................................................246 1.71 Expression statements.............................................................................................................................247 1.72 Selection statements...............................................................................................................................247 1.72.1 The if statement................................................................................................................................247 1.72.2 The switch statement........................................................................................................................248 1.73 Iteration statements.................................................................................................................................252 1.73.1 The while statement.........................................................................................................................252 1.73.2 The do statement..............................................................................................................................253 1.73.3 The for statement.............................................................................................................................253 1.73.4 The foreach statement......................................................................................................................254 1.74 Jump statements.....................................................................................................................................257 1.74.1 The break statement.........................................................................................................................258 1.74.2 The continue statement....................................................................................................................259 1.74.3 The goto statement...........................................................................................................................259 1.74.4 The return statement.........................................................................................................................260 1.74.5 The throw statement.........................................................................................................................261 1.75 The try statement....................................................................................................................................262 1.76 The checked and unchecked statements..................................................................................................265 1.77 The lock statement..................................................................................................................................265 1.78 The using statement................................................................................................................................266 1.79 The yield statement.................................................................................................................................268 Namespaces.......................................................................................................................................................271 1.80 Compilation units...................................................................................................................................271 1.81 Namespace declarations..........................................................................................................................271 1.82 Extern aliases..........................................................................................................................................273 1.83 Using directives......................................................................................................................................273 1.83.1 Using alias directives.......................................................................................................................274 1.83.2 Using namespace directives.............................................................................................................277 1.84 Namespace members..............................................................................................................................278 1.85 Type declarations....................................................................................................................................279 1.86 Namespace alias qualifiers.....................................................................................................................279 1.86.1 Uniqueness of aliases.......................................................................................................................280 Classes...............................................................................................................................................................282 1.87 Class declarations...................................................................................................................................282 1.87.1 Class modifiers................................................................................................................................282 1.87.1.1 Abstract classes.........................................................................................................................283 1.87.1.2 Sealed classes............................................................................................................................283 1.87.1.3 Static classes..............................................................................................................................283 1.87.2 Partial modifier................................................................................................................................284 1.87.3 Type parameters...............................................................................................................................284 1.87.4 Class base specification....................................................................................................................285 1.87.4.1 Base classes...............................................................................................................................285 1.87.4.2 Interface implementations.........................................................................................................287 1.87.5 Type parameter constraints..............................................................................................................287 1.87.6 Class body........................................................................................................................................291 x Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Table of Contents 1.88 Partial types............................................................................................................................................291 1.88.1 Attributes.........................................................................................................................................292 1.88.2 Modifiers..........................................................................................................................................292 1.88.3 Type parameters and constraints......................................................................................................292 1.88.4 Base class.........................................................................................................................................293 1.88.5 Base interfaces.................................................................................................................................293 1.88.6 Members..........................................................................................................................................294 1.88.7 Partial methods................................................................................................................................294 1.88.8 Name binding...................................................................................................................................297 1.89 Class members........................................................................................................................................297 1.89.1 The instance type ............................................................................................................................298 1.89.2 Members of constructed types..........................................................................................................299 1.89.3 Inheritance.......................................................................................................................................300 1.89.4 The new modifier.............................................................................................................................301 1.89.5 Access modifiers..............................................................................................................................301 1.89.6 Constituent types..............................................................................................................................301 1.89.7 Static and instance members............................................................................................................301 1.89.8 Nested types.....................................................................................................................................302 1.89.8.1 Fully qualified name..................................................................................................................303 1.89.8.2 Declared accessibility................................................................................................................303 1.89.8.3 Hiding........................................................................................................................................303 1.89.8.4 this access..................................................................................................................................304 1.89.8.5 Access to private and protected members of the containing type..............................................305 1.89.8.6 Nested types in generic classes..................................................................................................306 1.89.9 Reserved member names..................................................................................................................307 1.89.9.1 Member names reserved for properties......................................................................................307 1.89.9.2 Member names reserved for events...........................................................................................308 1.89.9.3 Member names reserved for indexers........................................................................................308 1.89.9.4 Member names reserved for destructors....................................................................................308 1.90 Constants................................................................................................................................................308 1.91 Fields......................................................................................................................................................310 1.91.1 Static and instance fields..................................................................................................................311 1.91.2 Readonly fields................................................................................................................................312 1.91.2.1 Using static readonly fields for constants..................................................................................312 1.91.2.2 Versioning of constants and static readonly fields.....................................................................313 1.91.3 Volatile fields...................................................................................................................................314 1.91.4 Field initialization............................................................................................................................315 1.91.5 Variable initializers..........................................................................................................................315 1.91.5.1 Static field initialization.............................................................................................................316 1.91.5.2 Instance field initialization........................................................................................................318 1.92 Methods..................................................................................................................................................318 1.92.1 Method parameters...........................................................................................................................320 1.92.1.1 Value parameters.......................................................................................................................321 1.92.1.2 Reference parameters................................................................................................................322 1.92.1.3 Output parameters.....................................................................................................................323 1.92.1.4 Parameter arrays........................................................................................................................324 1.92.2 Static and instance methods.............................................................................................................326 1.92.3 Virtual methods................................................................................................................................326 1.92.4 Override methods.............................................................................................................................329 1.92.5 Sealed methods................................................................................................................................331 1.92.6 Abstract methods.............................................................................................................................332 xi Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 1.92.7 External methods.............................................................................................................................333 1.92.8 Partial methods................................................................................................................................334 1.92.9 Extension methods...........................................................................................................................334 1.92.10 Method body..................................................................................................................................335 1.92.11 Method overloading.......................................................................................................................336 1.93 Properties................................................................................................................................................336 1.93.1 Static and instance properties...........................................................................................................337 1.93.2 Accessors.........................................................................................................................................337 1.93.3 Automatically implemented properties............................................................................................343 1.93.4 Accessibility.....................................................................................................................................343 1.93.5 Virtual, sealed, override, and abstract accessors..............................................................................345 1.94 Events.....................................................................................................................................................347 1.94.1 Field-like events...............................................................................................................................348 1.94.2 Event accessors................................................................................................................................350 1.94.3 Static and instance events.................................................................................................................351 1.94.4 Virtual, sealed, override, and abstract accessors..............................................................................352 1.95 Indexers..................................................................................................................................................352 1.95.1 Indexer overloading.........................................................................................................................356 1.96 Operators................................................................................................................................................356 1.96.1 Unary operators................................................................................................................................358 1.96.2 Binary operators...............................................................................................................................359 1.96.3 Conversion operators.......................................................................................................................359 1.97 Instance constructors..............................................................................................................................362 1.97.1 Constructor initializers.....................................................................................................................363 1.97.2 Instance variable initializers.............................................................................................................364 1.97.3 Constructor execution......................................................................................................................364 1.97.4 Default constructors.........................................................................................................................366 1.97.5 Private constructors..........................................................................................................................366 1.97.6 Optional instance constructor parameters.........................................................................................367 1.98 Static constructors..................................................................................................................................367 1.99 Destructors.............................................................................................................................................369 1.100 Iterators.................................................................................................................................................371 1.100.1 Enumerator interfaces....................................................................................................................371 1.100.2 Enumerable interfaces....................................................................................................................371 1.100.3 Yield type.......................................................................................................................................371 1.100.4 Enumerator objects........................................................................................................................372 1.100.4.1 The MoveNext method............................................................................................................372 1.100.4.2 The Current property...............................................................................................................373 1.100.4.3 The Dispose method................................................................................................................373 1.100.5 Enumerable objects........................................................................................................................374 1.100.5.1 The GetEnumerator method....................................................................................................374 1.100.6 Implementation example................................................................................................................374 Structs...............................................................................................................................................................382 1.101 Struct declarations................................................................................................................................382 1.101.1 Struct modifiers..............................................................................................................................382 1.101.2 Partial modifier..............................................................................................................................383 1.101.3 Struct interfaces.............................................................................................................................383 1.101.4 Struct body.....................................................................................................................................383 1.102 Struct members.....................................................................................................................................383 1.103 Class and struct differences..................................................................................................................383 xii Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Table of Contents 1.103.1 Value semantics.............................................................................................................................384 1.103.2 Inheritance.....................................................................................................................................385 1.103.3 Assignment....................................................................................................................................385 1.103.4 Default values................................................................................................................................385 1.103.5 Boxing and unboxing.....................................................................................................................386 1.103.6 Meaning of this..............................................................................................................................388 1.103.7 Field initializers.............................................................................................................................388 1.103.8 Constructors...................................................................................................................................388 1.103.9 Destructors.....................................................................................................................................389 1.103.10 Static constructors........................................................................................................................389 1.104 Struct examples....................................................................................................................................389 1.104.1 Database integer type.....................................................................................................................389 1.104.2 Database boolean type....................................................................................................................391 Arrays...............................................................................................................................................................394 1.105 Array types...........................................................................................................................................394 1.105.1 The System.Array type...................................................................................................................395 1.105.2 Arrays and the generic IList interface............................................................................................395 1.106 Array creation.......................................................................................................................................395 1.107 Array element access............................................................................................................................396 1.108 Array members.....................................................................................................................................396 1.109 Array covariance..................................................................................................................................396 1.110 Array initializers...................................................................................................................................396 Interfaces..........................................................................................................................................................399 1.111 Interface declarations............................................................................................................................399 1.111.1 Interface modifiers.........................................................................................................................399 1.111.2 Partial modifier..............................................................................................................................400 1.111.3 Variant type parameter lists............................................................................................................400 1.111.3.1 Variance safety........................................................................................................................400 1.111.3.2 Variance conversion................................................................................................................401 1.111.4 Base interfaces...............................................................................................................................401 1.111.5 Interface body................................................................................................................................402 1.112 Interface members................................................................................................................................402 1.112.1 Interface methods...........................................................................................................................403 1.112.2 Interface properties........................................................................................................................404 1.112.3 Interface events..............................................................................................................................404 1.112.4 Interface indexers...........................................................................................................................404 1.112.5 Interface member access................................................................................................................404 1.113 Fully qualified interface member names...............................................................................................406 1.114 Interface implementations....................................................................................................................407 1.114.1 Explicit interface member implementations...................................................................................408 1.114.2 Uniqueness of implemented interfaces...........................................................................................410 1.114.3 Implementation of generic methods...............................................................................................411 1.114.4 Interface mapping..........................................................................................................................412 1.114.5 Interface implementation inheritance.............................................................................................415 1.114.6 Interface re-implementation...........................................................................................................417 1.114.7 Abstract classes and interfaces.......................................................................................................418 Enums...............................................................................................................................................................420 1.115 Enum declarations................................................................................................................................420 1.116 Enum modifiers....................................................................................................................................420 xiii Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 1.117 Enum members.....................................................................................................................................421 1.118 The System.Enum type.........................................................................................................................423 1.119 Enum values and operations.................................................................................................................423 Delegates...........................................................................................................................................................424 1.120 Delegate declarations............................................................................................................................424 1.121 Delegate compatibility..........................................................................................................................426 1.122 Delegate instantiation...........................................................................................................................427 1.123 Delegate invocation..............................................................................................................................427 Exceptions.........................................................................................................................................................430 1.124 Causes of exceptions............................................................................................................................430 1.125 The System.Exception class.................................................................................................................430 1.126 How exceptions are handled.................................................................................................................430 1.127 Common Exception Classes.................................................................................................................431 Attributes..........................................................................................................................................................433 1.128 Attribute classes....................................................................................................................................433 1.128.1 Attribute usage...............................................................................................................................433 1.128.2 Positional and named parameters...................................................................................................435 1.128.3 Attribute parameter types...............................................................................................................435 1.129 Attribute specification..........................................................................................................................436 1.130 Attribute instances................................................................................................................................441 1.130.1 Compilation of an attribute............................................................................................................441 1.130.2 Run-time retrieval of an attribute instance.....................................................................................441 1.131 Reserved attributes...............................................................................................................................442 1.131.1 The AttributeUsage attribute..........................................................................................................442 1.131.2 The Conditional attribute...............................................................................................................443 1.131.2.1 Conditional methods................................................................................................................443 1.131.2.2 Conditional attribute classes....................................................................................................446 1.131.3 The Obsolete attribute....................................................................................................................446 1.132 Attributes for Interoperation.................................................................................................................448 1.132.1 Interoperation with COM and Win32 components.........................................................................448 1.132.2 Interoperation with other .NET languages......................................................................................448 1.132.2.1 The IndexerName attribute......................................................................................................448 Unsafe code.......................................................................................................................................................449 1.133 Unsafe contexts....................................................................................................................................449 1.134 Pointer types.........................................................................................................................................452 1.135 Fixed and moveable variables...............................................................................................................454 1.136 Pointer conversions...............................................................................................................................455 1.136.1 Pointer arrays.................................................................................................................................456 1.137 Pointers in expressions.........................................................................................................................457 1.137.1 Pointer indirection..........................................................................................................................457 1.137.2 Pointer member access...................................................................................................................457 1.137.3 Pointer element access...................................................................................................................458 1.137.4 The address-of operator..................................................................................................................459 1.137.5 Pointer increment and decrement...................................................................................................460 1.137.6 Pointer arithmetic...........................................................................................................................460 1.137.7 Pointer comparison........................................................................................................................461 1.137.8 The sizeof operator.........................................................................................................................462 1.138 The fixed statement..............................................................................................................................462 xiv Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Table of Contents 1.139 Fixed size buffers.................................................................................................................................466 1.139.1 Fixed size buffer declarations........................................................................................................466 1.139.2 Fixed size buffers in expressions....................................................................................................467 1.139.3 Definite assignment checking........................................................................................................468 1.140 Stack allocation....................................................................................................................................469 1.141 Dynamic memory allocation.................................................................................................................470 Documentation comments...............................................................................................................................473 Introduction...................................................................................................................................................473 Recommended tags........................................................................................................................................474 .............................................................................................................................................................475 .......................................................................................................................................................475 .................................................................................................................................................476 ...............................................................................................................................................476 ...................................................................................................................................................477 ..........................................................................................................................................................478 ........................................................................................................................................................479 .....................................................................................................................................................479 ................................................................................................................................................480 .............................................................................................................................................480 ...................................................................................................................................................481 ....................................................................................................................................................481 .........................................................................................................................................................481 ...................................................................................................................................................482 ................................................................................................................................................482 ......................................................................................................................................................483 ..............................................................................................................................................483 .........................................................................................................................................483 Processing the documentation file.................................................................................................................484 ID string format.........................................................................................................................................484 ID string examples.....................................................................................................................................485 An example...................................................................................................................................................489 C# source code...........................................................................................................................................489 Resulting XML..........................................................................................................................................492 Grammar..........................................................................................................................................................496 Lexical grammar............................................................................................................................................496 Line terminators.........................................................................................................................................496 Comments..................................................................................................................................................496 White space................................................................................................................................................497 Tokens.......................................................................................................................................................497 Unicode character escape sequences..........................................................................................................497 Identifiers...................................................................................................................................................497 Keywords...................................................................................................................................................498 Literals.......................................................................................................................................................499 Operators and punctuators.........................................................................................................................501 Pre-processing directives...........................................................................................................................501 Syntactic grammar.........................................................................................................................................503 Basic concepts...........................................................................................................................................503 Types.........................................................................................................................................................503 Variables....................................................................................................................................................505 xv Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification Expressions................................................................................................................................................505 Statements..................................................................................................................................................512 Namespaces...............................................................................................................................................515 Classes.......................................................................................................................................................516 Structs........................................................................................................................................................523 Arrays........................................................................................................................................................524 Interfaces...................................................................................................................................................524 Enums........................................................................................................................................................525 Delegates...................................................................................................................................................526 Attributes...................................................................................................................................................526 Grammar extensions for unsafe code.............................................................................................................528 References.........................................................................................................................................................531 xvi Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Chapter 2 Lexical structure Introduction C# (pronounced “See Sharp”) is a simple, modern, object-oriented, and type-safe programming language. C# has its roots in the C family of languages and will be immediately familiar to C, C++, and Java programmers. C# is standardized by ECMA International as the ECMA-334 standard and by ISO/IEC as the ISO/IEC 23270 standard. Microsoft’s C# compiler for the .NET Framework is a conforming implementation of both of these standards. C# is an object-oriented language, but C# further includes support for component-oriented programming. Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality. Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they incorporate their own documentation. C# provides language constructs to directly support these concepts, making C# a very natural language in which to create and use software components. Several C# features aid in the construction of robust and durable applications: Garbage collection automatically reclaims memory occupied by unused objects; exception handling provides a structured and extensible approach to error detection and recovery; and the type-safe design of the language makes it impossible to read from uninitialized variables, to index arrays beyond their bounds, or to perform unchecked type casts. C# has a unified type system. All C# types, including primitive types such as int and double, inherit from a single root object type. Thus, all types share a set of common operations, and values of any type can be stored, transported, and operated upon in a consistent manner. Furthermore, C# supports both user-defined reference types and value types, allowing dynamic allocation of objects as well as in-line storage of lightweight structures. To ensure that C# programs and libraries can evolve over time in a compatible manner, much emphasis has been placed on versioning in C#’s design. Many programming languages pay little attention to this issue, and, as a result, programs written in those languages break more often than necessary when newer versions of dependent libraries are introduced. Aspects of C#’s design that were directly influenced by versioning considerations include the separate virtual and override modifiers, the rules for method overload resolution, and support for explicit interface member declarations. The rest of this chapter describes the essential features of the C# language. Although later chapters describe rules and exceptions in a detail-oriented and sometimes mathematical manner, this chapter strives for clarity and brevity at the expense of completeness. The intent is to provide the reader with an introduction to the language that will facilitate the writing of early programs and the reading of later chapters. 1.1 Hello world The “Hello, World” program is traditionally used to introduce a programming language. Here it is in C#: using System; class Hello { static void Main() { Console.WriteLine("Hello, World"); } } 1 Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification C# source files typically have the file extension .cs. Assuming that the “Hello, World” program is stored in the file hello.cs, the program can be compiled with the Microsoft C# compiler using the command line csc hello.cs which produces an executable assembly named hello.exe. The output produced by this application when it is run is Hello, World The “Hello, World” program starts with a using directive that references the System namespace. Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other namespaces—for example, the System namespace contains a number of types, such as the Console class referenced in the program, and a number of other namespaces, such as IO and Collections. A using directive that references a given namespace enables unqualified use of the types that are members of that namespace. Because of the using directive, the program can use Console.WriteLine as shorthand for System.Console.WriteLine. The Hello class declared by the “Hello, World” program has a single member, the method named Main. The Main method is declared with the static modifier. While instance methods can reference a particular enclosing object instance using the keyword this, static methods operate without reference to a particular object. By convention, a static method named Main serves as the entry point of a program. The output of the program is produced by the WriteLine method of the Console class in the System namespace. This class is provided by the .NET Framework class libraries, which, by default, are automatically referenced by the Microsoft C# compiler. Note that C# itself does not have a separate runtime library. Instead, the .NET Framework is the runtime library of C#. 1.2 Program structure The key organizational concepts in C# are programs, namespaces, types, members, and assemblies. C# programs consist of one or more source files. Programs declare types, which contain members and can be organized into namespaces. Classes and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they are physically packaged into assemblies. Assemblies typically have the file extension .exe or .dll, depending on whether they implement applications or libraries. The example using System; namespace Acme.Collections { public class Stack { Entry top; public void Push(object data) { top = new Entry(top, data); } public object Pop() { if (top == null) throw new InvalidOperationException(); object result = top.data; top = top.next; return result; } 2 Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- Chapter 2 Lexical structure class Entry { public Entry next; public object data; public Entry(Entry next, object data) { this.next = next; this.data = data; } } } } declares a class named Stack in a namespace called Acme.Collections. The fully qualified name of this class is Acme.Collections.Stack. The class contains several members: a field named top, two methods named Push and Pop, and a nested class named Entry. The Entry class further contains three members: a field named next, a field named data, and a constructor. Assuming that the source code of the example is stored in the file acme.cs, the command line csc /t:library acme.cs compiles the example as a library (code without a Main entry point) and produces an assembly named acme.dll. Assemblies contain executable code in the form of Intermediate Language (IL) instructions, and symbolic information in the form of metadata. Before it is executed, the IL code in an assembly is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime. Because an assembly is a self-describing unit of functionality containing both code and metadata, there is no need for #include directives and header files in C#. The public types and members contained in a particular assembly are made available in a C# program simply by referencing that assembly when compiling the program. For example, this program uses the Acme.Collections.Stack class from the acme.dll assembly: using System; using Acme.Collections; class Test { static void Main() { Stack s = new Stack(); s.Push(1); s.Push(10); s.Push(100); Console.WriteLine(s.Pop()); Console.WriteLine(s.Pop()); Console.WriteLine(s.Pop()); } } If the program is stored in the file test.cs, when test.cs is compiled, the acme.dll assembly can be referenced using the compiler’s /r option: csc /r:acme.dll test.cs This creates an executable assembly named test.exe, which, when run, produces the output: 3 Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
- C# Language Specification 100 10 1 C# permits the source text of a program to be stored in several source files. When a multi-file C# program is compiled, all of the source files are processed together, and the source files can freely reference each other— conceptually, it is as if all the source files were concatenated into one large file before being processed. Forward declarations are never needed in C# because, with very few exceptions, declaration order is insignificant. C# does not limit a source file to declaring only one public type nor does it require the name of the source file to match a type declared in the source file. 1.3 Types and variables There are two kinds of types in C#: value types and reference types. Variables of value types directly contain their data whereas variables of reference types store references to their data, the latter being known as objects. With reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable. With value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other (except in the case of ref and out parameter variables). C#’s value types are further divided into simple types, enum types, struct types, and nullable types, and C#’s reference types are further divided into class types, interface types, array types, and delegate types. The following table provides an overview of C#’s type system. Category Description Value Simple types Signed integral: sbyte, short, int, long types Unsigned integral: byte, ushort, uint, ulong Unicode characters: char IEEE floating point: float, double High-precision decimal: decimal Boolean: bool Enum types User-defined types of the form enum E {...} Struct types User-defined types of the form struct S {...} Nullable types Extensions of all other value types with a null value Reference Class types Ultimate base class of all other types: object types Unicode strings: string User-defined types of the form class C {...} Interface types User-defined types of the form interface I {...} Array types Single- and multi-dimensional, for example, int[] and int[,] Delegate types User-defined types of the form e.g. delegate int D(...) The eight integral types provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned form. 4 Copyright © Microsoft Corporation 1999-2010. All Rights Reserved.
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Tài liệu Lập trình C cho vi điều khiển
13 p | 595 | 264
-
Lập trình C# từ cơ bản đến nâng cao part 1
25 p | 292 | 95
-
Tài liệu lập trình C# và cấu hình Visual Studio 2013
159 p | 399 | 91
-
Lập trình C# từ cơ bản đến nâng cao part 2
25 p | 242 | 84
-
Tài liệu lập trình C++ tiếng Việt
200 p | 295 | 74
-
Tài liệu Lập trình C# tiếng Việt
212 p | 341 | 74
-
Lập trình C# từ cơ bản đến nâng cao part 3
25 p | 156 | 62
-
Lập trình C# từ cơ bản đến nâng cao part 4
25 p | 206 | 52
-
Lập trình C trong kỹ thuật điện tử part 1
42 p | 144 | 48
-
Tài liệu Lập trình C tiếng Việt
117 p | 180 | 43
-
Lập trình C# từ cơ bản đến nâng cao part 5
25 p | 152 | 42
-
Lập trình C trong kỹ thuật điện tử part 3
42 p | 134 | 41
-
Lập trình C# từ cơ bản đến nâng cao part 6
25 p | 121 | 36
-
Lập trình C# từ cơ bản đến nâng cao part 8
25 p | 98 | 34
-
Lập trình C# từ cơ bản đến nâng cao part 7
25 p | 119 | 34
-
Lập trình C# từ cơ bản đến nâng cao part 10
17 p | 108 | 33
-
Lập trình C trong kỹ thuật điện tử part 2
42 p | 143 | 32
-
Lập trình C# từ cơ bản đến nâng cao part 9
25 p | 129 | 31
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn