C# Language Reference pdf

277 175 0
C# Language Reference pdf

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

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

Thông tin tài liệu

Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. C# Language Reference Owners: Anders Hejlsberg and Scott Wiltamuth File: C# Language Reference.doc Last saved: 6/12/2000 Last printed: 6/26/2000 Version 0.17b Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. Notice This documentation is an early release of the final documentation, which may be changed substantially prior to final commercial release, and is information of Microsoft Corporation. This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document. Information in this document is subject to change without notice. The entire risk of the use or the results of the use of this document remains with the user. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. Unpublished work. © 1999-2000 Microsoft Corporation. All rights reserved. Microsoft, Windows, Visual Basic, and Visual C++ are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. Table of Contents Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. iii Table of Contents 1. Introduction 1 1.1 Hello, world 1 1.2 Automatic memory management 2 1.3 Types 4 1.4 Predefined types 5 1.5 Array types 7 1.6 Type system unification 9 1.7 Statements 10 1.7.1 Statement lists and blocks 10 1.7.2 Labeled statements and goto statements 10 1.7.3 Local declarations of constants and variables 11 1.7.4 Expression statements 11 1.7.5 The if statement 11 1.7.6 The switch statement 12 1.7.7 The while statement 12 1.7.8 The do statement 13 1.7.9 The for statement 13 1.7.10 The foreach statement 13 1.7.11 The break statement and the continue statement 14 1.7.12 The return statement 14 1.7.13 The throw statement 14 1.7.14 The try statement 14 1.7.15 The checked and unchecked statements 14 1.7.16 The lock statement 14 1.8 Classes 14 1.9 Structs 15 1.10 Interfaces 15 1.11 Delegates 17 1.12 Enums 18 1.13 Namespaces 18 1.14 Properties 19 1.15 Indexers 20 1.16 Events 21 1.17 Versioning 22 1.18 Attributes 24 2. Lexical structure 27 2.1 Phases of translation 27 2.2 Grammar notation 27 2.3 Pre-processing 28 2.3.1 Pre-processing declarations 28 2.3.2 #if, #elif, #else, #endif 29 2.3.3 Pre-processing control lines 30 2.3.4 #line 31 2.3.5 Pre-processing identifiers 31 2.3.6 Pre-processing expressions 31 2.3.7 Interaction with white space 32 2.4 Lexical analysis 33 2.4.1 Input 33 C# LANGUAGE REFERENCE iv Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. 2.4.2 Input characters 33 2.4.3 Line terminators 33 2.4.4 Comments 33 2.4.5 White space 33 2.4.6 Tokens 33 2.5 Processing of Unicode character escape sequences 34 2.5.1 Identifiers 34 2.5.2 Keywords 36 2.5.3 Literals 36 2.5.3.1 Boolean literals 36 2.5.3.2 Integer literals 36 2.5.3.3 Real literals 37 2.5.3.4 Character literals 38 2.5.3.5 String literals 39 2.5.3.6 The null literal 40 2.5.4 Operators and punctuators 40 3. Basic concepts 41 3.1 Declarations 41 3.2 Members 43 3.2.1 Namespace members 43 3.2.2 Struct members 43 3.2.3 Enumeration members 44 3.2.4 Class members 44 3.2.5 Interface members 44 3.2.6 Array members 44 3.2.7 Delegate members 44 3.3 Member access 44 3.3.1 Declared accessibility 44 3.3.2 Accessibility domains 45 3.3.3 Protected access 47 3.3.4 Accessibility constraints 48 3.4 Signatures and overloading 49 3.5 Scopes 50 3.5.1 Name hiding 52 3.5.1.1 Hiding through nesting 52 3.5.1.2 Hiding through inheritance 53 3.6 Namespace and type names 54 3.6.1 Fully qualified names 55 4. Types 57 4.1 Value types 57 4.1.1 Default constructors 58 4.1.2 Struct types 59 4.1.3 Simple types 59 4.1.4 Integral types 60 4.1.5 Floating point types 61 4.1.6 The decimal type 62 4.1.7 The bool type 63 4.1.8 Enumeration types 63 4.2 Reference types 63 Table of Contents Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. v 4.2.1 Class types 64 4.2.2 The object type 64 4.2.3 The string type 64 4.2.4 Interface types 64 4.2.5 Array types 64 4.2.6 Delegate types 64 4.3 Boxing and unboxing 65 4.3.1 Boxing conversions 65 4.3.2 Unboxing conversions 66 5. Variables 67 5.1 Variable categories 67 5.1.1 Static variables 67 5.1.2 Instance variables 67 5.1.2.1 Instance variables in classes 67 5.1.2.2 Instance variables in structs 68 5.1.3 Array elements 68 5.1.4 Value parameters 68 5.1.5 Reference parameters 68 5.1.6 Output parameters 68 5.1.7 Local variables 69 5.2 Default values 69 5.3 Definite assignment 69 5.3.1 Initially assigned variables 72 5.3.2 Initially unassigned variables 72 5.4 Variable references 72 6. Conversions 73 6.1 Implicit conversions 73 6.1.1 Identity conversion 73 6.1.2 Implicit numeric conversions 73 6.1.3 Implicit enumeration conversions 74 6.1.4 Implicit reference conversions 74 6.1.5 Boxing conversions 74 6.1.6 Implicit constant expression conversions 74 6.1.7 User-defined implicit conversions 75 6.2 Explicit conversions 75 6.2.1 Explicit numeric conversions 75 6.2.2 Explicit enumeration conversions 76 6.2.3 Explicit reference conversions 76 6.2.4 Unboxing conversions 77 6.2.5 User-defined explicit conversions 77 6.3 Standard conversions 77 6.3.1 Standard implicit conversions 77 6.3.2 Standard explicit conversions 78 6.4 User-defined conversions 78 6.4.1 Permitted user-defined conversions 78 6.4.2 Evaluation of user-defined conversions 78 6.4.3 User-defined implicit conversions 79 6.4.4 User-defined explicit conversions 80 7. Expressions 81 C# LANGUAGE REFERENCE vi Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. 7.1 Expression classifications 81 7.1.1 Values of expressions 82 7.2 Operators 82 7.2.1 Operator precedence and associativity 82 7.2.2 Operator overloading 83 7.2.3 Unary operator overload resolution 84 7.2.4 Binary operator overload resolution 85 7.2.5 Candidate user-defined operators 85 7.2.6 Numeric promotions 85 7.2.6.1 Unary numeric promotions 86 7.2.6.2 Binary numeric promotions 86 7.3 Member lookup 86 7.3.1 Base types 87 7.4 Function members 87 7.4.1 Argument lists 89 7.4.2 Overload resolution 91 7.4.2.1 Applicable function member 91 7.4.2.2 Better function member 92 7.4.2.3 Better conversion 92 7.4.3 Function member invocation 92 7.4.3.1 Invocations on boxed instances 93 7.4.4 Virtual function member lookup 94 7.4.5 Interface function member lookup 94 7.5 Primary expressions 94 7.5.1 Literals 94 7.5.2 Simple names 94 7.5.2.1 Invariant meaning in blocks 95 7.5.3 Parenthesized expressions 96 7.5.4 Member access 96 7.5.4.1 Identical simple names and type names 98 7.5.5 Invocation expressions 98 7.5.5.1 Method invocations 99 7.5.5.2 Delegate invocations 99 7.5.6 Element access 100 7.5.6.1 Array access 100 7.5.6.2 Indexer access 100 7.5.6.3 String indexing 101 7.5.7 This access 101 7.5.8 Base access 102 7.5.9 Postfix increment and decrement operators 102 7.5.10 new operator 103 7.5.10.1 Object creation expressions 103 7.5.10.2 Array creation expressions 104 7.5.10.3 Delegate creation expressions 106 7.5.11 typeof operator 107 7.5.12 sizeof operator 108 7.5.13 checked and unchecked operators 108 7.6 Unary expressions 110 7.6.1 Unary plus operator 110 7.6.2 Unary minus operator 111 7.6.3 Logical negation operator 111 Table of Contents Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. vii 7.6.4 Bitwise complement operator 111 7.6.5 Indirection operator 112 7.6.6 Address operator 112 7.6.7 Prefix increment and decrement operators 112 7.6.8 Cast expressions 113 7.7 Arithmetic operators 113 7.7.1 Multiplication operator 113 7.7.2 Division operator 114 7.7.3 Remainder operator 115 7.7.4 Addition operator 116 7.7.5 Subtraction operator 117 7.8 Shift operators 118 7.9 Relational operators 119 7.9.1 Integer comparison operators 120 7.9.2 Floating-point comparison operators 121 7.9.3 Decimal comparison operators 121 7.9.4 Boolean equality operators 122 7.9.5 Enumeration comparison operators 122 7.9.6 Reference type equality operators 122 7.9.7 String equality operators 123 7.9.8 Delegate equality operators 124 7.9.9 The is operator 124 7.10 Logical operators 124 7.10.1 Integer logical operators 124 7.10.2 Enumeration logical operators 125 7.10.3 Boolean logical operators 125 7.11 Conditional logical operators 125 7.11.1 Boolean conditional logical operators 126 7.11.2 User-defined conditional logical operators 126 7.12 Conditional operator 127 7.13 Assignment operators 127 7.13.1 Simple assignment 128 7.13.2 Compound assignment 130 7.13.3 Event assignment 130 7.14 Expression 130 7.15 Constant expressions 131 7.16 Boolean expressions 132 8. Statements 133 8.1 End points and reachability 133 8.2 Blocks 135 8.2.1 Statement lists 135 8.3 The empty statement 135 8.4 Labeled statements 136 8.5 Declaration statements 136 8.5.1 Local variable declarations 136 8.5.2 Local constant declarations 137 8.6 Expression statements 138 8.7 Selection statements 138 8.7.1 The if statement 138 8.7.2 The switch statement 139 C# LANGUAGE REFERENCE viii Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. 8.8 Iteration statements 142 8.8.1 The while statement 143 8.8.2 The do statement 143 8.8.3 The for statement 144 8.8.4 The foreach statement 145 8.9 Jump statements 146 8.9.1 The break statement 146 8.9.2 The continue statement 147 8.9.3 The goto statement 147 8.9.4 The return statement 148 8.9.5 The throw statement 149 8.10 The try statement 150 8.11 The checked and unchecked statements 152 8.12 The lock statement 152 9. Namespaces 155 9.1 Compilation units 155 9.2 Namespace declarations 155 9.3 Using directives 156 9.3.1 Using alias directives 157 9.3.2 Using namespace directives 159 9.4 Namespace members 161 9.5 Type declarations 161 10. Classes 163 10.1 Class declarations 163 10.1.1 Class modifiers 163 10.1.1.1 Abstract classes 163 10.1.1.2 Sealed classes 164 10.1.2 Class base specification 164 10.1.2.1 Base classes 164 10.1.2.2 Interface implementations 165 10.1.3 Class body 166 10.2 Class members 166 10.2.1 Inheritance 167 10.2.2 The new modifier 167 10.2.3 Access modifiers 168 10.2.4 Constituent types 168 10.2.5 Static and instance members 168 10.2.6 Nested types 169 10.3 Constants 169 10.4 Fields 170 10.4.1 Static and instance fields 171 10.4.2 Readonly fields 172 10.4.2.1 Using static readonly fields for constants 172 10.4.2.2 Versioning of constants and static readonly fields 172 10.4.3 Field initialization 173 10.4.4 Variable initializers 173 10.4.4.1 Static field initialization 174 10.4.4.2 Instance field initialization 174 10.5 Methods 175 Table of Contents Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. ix 10.5.1 Method parameters 176 10.5.1.1 Value parameters 177 10.5.1.2 Reference parameters 177 10.5.1.3 Output parameters 178 10.5.1.4 Params parameters 178 10.5.2 Static and instance methods 180 10.5.3 Virtual methods 180 10.5.4 Override methods 182 10.5.5 Abstract methods 183 10.5.6 External methods 184 10.5.7 Method body 185 10.5.8 Method overloading 185 10.6 Properties 185 10.6.1 Static properties 186 10.6.2 Accessors 187 10.6.3 Virtual, override, and abstract accessors 191 10.7 Events 193 10.8 Indexers 196 10.8.1 Indexer overloading 199 10.9 Operators 199 10.9.1 Unary operators 200 10.9.2 Binary operators 200 10.9.3 Conversion operators 200 10.10 Instance constructors 202 10.10.1 Constructor initializers 202 10.10.2 Instance variable initializers 203 10.10.3 Constructor execution 203 10.10.4 Default constructors 205 10.10.5 Private constructors 205 10.10.6 Optional constructor parameters 206 10.11 Destructors 206 10.12 Static constructors 207 10.12.1 Class loading and initialization 208 11. Structs 211 11.1 Struct declarations 211 11.1.1 Struct modifiers 211 11.1.2 Interfaces 211 11.1.3 Struct body 211 11.2 Struct members 211 11.3 Struct examples 211 11.3.1 Database integer type 211 11.3.2 Database boolean type 213 12. Arrays 215 12.1 Array types 215 12.1.1 The System.Array type 216 12.2 Array creation 216 12.3 Array element access 216 12.4 Array members 216 12.5 Array covariance 216 C# LANGUAGE REFERENCE x Copyright  Microsoft Corporation 1999-2000. All Rights Reserved. 12.6 Array initializers 217 13. Interfaces 219 13.1 Interface declarations 219 13.1.1 Interface modifiers 219 13.1.2 Base interfaces 219 13.1.3 Interface body 220 13.2 Interface members 220 13.2.1 Interface methods 221 13.2.2 Interface properties 221 13.2.3 Interface events 222 13.2.4 Interface indexers 222 13.2.5 Interface member access 222 13.3 Fully qualified interface member names 224 13.4 Interface implementations 224 13.4.1 Explicit interface member implementations 225 13.4.2 Interface mapping 227 13.4.3 Interface implementation inheritance 229 13.4.4 Interface re-implementation 231 13.4.5 Abstract classes and interfaces 232 14. Enums 233 14.1 Enum declarations 233 14.2 Enum members 234 14.3 Enum values and operations 236 15. Delegates 237 15.1 Delegate declarations 237 15.1.1 Delegate modifiers 237 16. Exceptions 239 17. Attributes 241 17.1 Attribute classes 241 17.1.1 The AttributeUsage attribute 241 17.1.2 Positional and named parameters 242 17.1.3 Attribute parameter types 242 17.2 Attribute specification 243 17.3 Attribute instances 245 17.3.1 Compilation of an attribute 245 17.3.2 Run-time retrieval of an attribute instance 245 17.4 Reserved attributes 245 17.4.1 The AttributeUsage attribute 246 17.4.2 The Conditional attribute 246 17.4.3 The Obsolete attribute 248 18. Versioning 251 19. Unsafe code 253 19.1 Unsafe code 253 19.2 Pointer types 253 20. Interoperability 255 [...]... 262 21 References 265 Copyright © Microsoft Corporation 1999-2000 All Rights Reserved xi Chapter 1 Introduction 1 Introduction C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++ C# (pronounced “C sharp”) is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers C# aims to... interoperability between CLS-compliant languages and class libraries For C# developers, this means that even though C# is a new language, it has complete access to the same rich class libraries that are used by seasoned tools such as Visual Basic and Visual C++ C# itself does not include a class library The rest of this chapter describes the essential features of the language While later chapters describe... class and struct declarations) Copyright © Microsoft Corporation 1999-2000 All Rights Reserved 1 C# LANGUAGE REFERENCE • The “Hello, world” output is produced through the use of a class library C# does not itself provide a class library Instead, C# uses a common class library that is also used by other languages such as Visual Basic and Visual C++ For C and C++ developers, it is interesting to note... power of C++ C# is provided as a part of Microsoft Visual Studio 7.0 In addition to C#, Visual Studio supports Visual Basic, Visual C++, and the scripting languages VBScript and JScript All of these languages provide access to the Next Generation Windows Services (NWGS) platform, which includes a common execution engine and a rich class library The NET software development kit defines a "Common Language. .. change 1.3 Types C# supports two major kinds of types: value types and reference types Value types include simple types (e.g., char, int, and float), enum types, and struct types Reference types include class types, interface types, delegate types, and array types Value types differ from reference types in that variables of the value types directly contain their data, whereas variables of the reference types... Corporation 1999-2000 All Rights Reserved 7 C# LANGUAGE REFERENCE } } int[][] j2; int[][][] j3; // "jagged" array: array of (array of int) // array of (array of (array of int)) shows a variety of local variable declarations that use array types with int as the element type Arrays are reference types, and so the declaration of an array variable merely sets aside space for the reference to the array Array instances... Reserved 21 C# LANGUAGE REFERENCE The Button class could be rewritten to use a property-like event declaration rather than a field-like event declaration This change has no effect on client code public class { public get set } } Button: Control event EventHandler Click { { } { } public void Reset() { Click = null; } 1.17 Versioning Versioning is an after-thought in most languages, but not in C# “Versioning”... in that variables of the value types directly contain their data, whereas variables of the reference types store references to 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... developer mistakenly uses “=” when “==” should have been used In C#, the example int i = ; F(i); if (i = 0) // Bug: the test should be (i == 0) G(); is invalid because the expression i = 0 is of type int, and if statements require an expression of type bool Copyright © Microsoft Corporation 1999-2000 All Rights Reserved 5 C# LANGUAGE REFERENCE The char type is used to represent Unicode characters A... eliminates barriers between programs written in different languages For example, the Console class could be written in C# or in some other language 1.2 Automatic memory management Manual memory management requires developers to manage the allocation and de-allocation of blocks of memory Manual memory management is both time consuming and difficult C# provides automatic memory management so that developers . Microsoft Corporation 1999-2000. All Rights Reserved. C# Language Reference Owners: Anders Hejlsberg and Scott Wiltamuth File: C# Language Reference. doc Last saved: 6/12/2000 Last printed: 6/26/2000 Version 0.17b Copyright. programming language derived from C and C++. C# (pronounced “C sharp”) is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers. C# aims. C++. C# is provided as a part of Microsoft Visual Studio 7.0. In addition to C#, Visual Studio supports Visual Basic, Visual C++, and the scripting languages VBScript and JScript. All of these languages

Ngày đăng: 29/06/2014, 04:20

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

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

Tài liệu liên quan