资源描述
C# Language ReferenceOwners:Anders Hejlsberg and Scott WiltamuthFile:C# Language Reference.docLast saved:6/27/2000Last printed:6/9/2000Version0.17bNoticeThis 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.Copyright Microsoft Corporation 1999-2000. All Rights Reserved.Table of ContentsTable of Contents1. Introduction11.1 Hello, world11.2 Automatic memory management21.3 Types41.4 Predefined types51.5 Array types71.6 Type system unification91.7 Statements101.7.1 Statement lists and blocks101.7.2 Labeled statements and goto statements101.7.3 Local declarations of constants and variables111.7.4 Expression statements111.7.5 The if statement111.7.6 The switch statement121.7.7 The while statement121.7.8 The do statement131.7.9 The for statement131.7.10 The foreach statement131.7.11 The break statement and the continue statement141.7.12 The return statement141.7.13 The throw statement141.7.14 The try statement141.7.15 The checked and unchecked statements141.7.16 The lock statement141.8 Classes141.9 Structs151.10 Interfaces151.11 Delegates171.12 Enums181.13 Namespaces181.14 Properties191.15 Indexers201.16 Events211.17 Versioning221.18 Attributes242. Lexical structure272.1 Phases of translation272.2 Grammar notation272.3 Pre-processing282.3.1 Pre-processing declarations282.3.2 #if, #elif, #else, #endif292.3.3 Pre-processing control lines302.3.4 #line312.3.5 Pre-processing identifiers312.3.6 Pre-processing expressions312.3.7 Interaction with white space322.4 Lexical analysis332.4.1 Input332.4.2 Input characters332.4.3 Line terminators332.4.4 Comments332.4.5 White space332.4.6 Tokens332.5 Processing of Unicode character escape sequences342.5.1 Identifiers342.5.2 Keywords362.5.3 Literals362.5.3.1 Boolean literals362.5.3.2 Integer literals362.5.3.3 Real literals372.5.3.4 Character literals382.5.3.5 String literals392.5.3.6 The null literal402.5.4 Operators and punctuators403. Basic concepts413.1 Declarations413.2 Members433.2.1 Namespace members433.2.2 Struct members433.2.3 Enumeration members443.2.4 Class members443.2.5 Interface members443.2.6 Array members443.2.7 Delegate members443.3 Member access443.3.1 Declared accessibility443.3.2 Accessibility domains453.3.3 Protected access473.3.4 Accessibility constraints483.4 Signatures and overloading493.5 Scopes503.5.1 Name hiding523.5.1.1 Hiding through nesting523.5.1.2 Hiding through inheritance533.6 Namespace and type names543.6.1 Fully qualified names554. Types574.1 Value types574.1.1 Default constructors584.1.2 Struct types594.1.3 Simple types594.1.4 Integral types604.1.5 Floating point types614.1.6 The decimal type624.1.7 The bool type634.1.8 Enumeration types634.2 Reference types634.2.1 Class types644.2.2 The object type644.2.3 The string type644.2.4 Interface types644.2.5 Array types644.2.6 Delegate types644.3 Boxing and unboxing654.3.1 Boxing conversions654.3.2 Unboxing conversions665. Variables675.1 Variable categories675.1.1 Static variables675.1.2 Instance variables675.1.2.1 Instance variables in classes675.1.2.2 Instance variables in structs685.1.3 Array elements685.1.4 Value parameters685.1.5 Reference parameters685.1.6 Output parameters685.1.7 Local variables695.2 Default values695.3 Definite assignment695.3.1 Initially assigned variables725.3.2 Initially unassigned variables725.4 Variable references726. Conversions736.1 Implicit conversions736.1.1 Identity conversion736.1.2 Implicit numeric conversions736.1.3 Implicit enumeration conversions746.1.4 Implicit reference conversions746.1.5 Boxing conversions746.1.6 Implicit constant expression conversions746.1.7 User-defined implicit conversions756.2 Explicit conversions756.2.1 Explicit numeric conversions756.2.2 Explicit enumeration conversions766.2.3 Explicit reference conversions766.2.4 Unboxing conversions776.2.5 User-defined explicit conversions776.3 Standard conversions776.3.1 Standard implicit conversions776.3.2 Standard explicit conversions786.4 User-defined conversions786.4.1 Permitted user-defined conversions786.4.2 Evaluation of user-defined conversions786.4.3 User-defined implicit conversions796.4.4 User-defined explicit conversions807. Expressions817.1 Expression classifications817.1.1 Values of expressions827.2 Operators827.2.1 Operator precedence and associativity827.2.2 Operator overloading837.2.3 Unary operator overload resolution847.2.4 Binary operator overload resolution857.2.5 Candidate user-defined operators857.2.6 Numeric promotions857.2.6.1 Unary numeric promotions867.2.6.2 Binary numeric promotions867.3 Member lookup867.3.1 Base types877.4 Function members877.4.1 Argument lists897.4.2 Overload resolution917.4.2.1 Applicable function member917.4.2.2 Better function member927.4.2.3 Better conversion927.4.3 Function member invocation927.4.3.1 Invocations on boxed instances937.4.4 Virtual function member lookup947.4.5 Interface function member lookup947.5 Primary expressions947.5.1 Literals947.5.2 Simple names947.5.2.1 Invariant meaning in blocks957.5.3 Parenthesized expressions967.5.4 Member access967.5.4.1 Identical simple names and type names987.5.5 Invocation expressions987.5.5.1 Method invocations997.5.5.2 Delegate invocations997.5.6 Element access1007.5.6.1 Array access1007.5.6.2 Indexer access1007.5.6.3 String indexing1017.5.7 This access1017.5.8 Base access1027.5.9 Postfix increment and decrement operators1027.5.10 new operator1037.5.10.1 Object creation expressions1037.5.10.2 Array creation expressions1047.5.10.3 Delegate creation expressions1067.5.11 typeof operator1077.5.12 sizeof operator1087.5.13 checked and unchecked operators1087.6 Unary expressions1107.6.1 Unary plus operator1107.6.2 Unary minus operator1117.6.3 Logical negation operator1117.6.4 Bitwise complement operator1117.6.5 Indirection operator1127.6.6 Address operator1127.6.7 Prefix increment and decrement operators1127.6.8 Cast expressions1137.7 Arithmetic operators1137.7.1 Multiplication operator1137.7.2 Division operator1147.7.3 Remainder operator1157.7.4 Addition operator1167.7.5 Subtraction operator1177.8 Shift operators1187.9 Relational operators1197.9.1 Integer comparison operators1207.9.2 Floating-point comparison operators1217.9.3 Decimal comparison operators1217.9.4 Boolean equality operators1227.9.5 Enumeration comparison operators1227.9.6 Reference type equality operators1227.9.7 String equality operators1237.9.8 Delegate equality operators1247.9.9 The is operator1247.10 Logical operators1247.10.1 Integer logical operators1247.10.2 Enumeration logical operators1257.10.3 Boolean logical operators1257.11 Conditional logical operators1257.11.1 Boolean conditional logical operators1267.11.2 User-defined conditional logical operators1267.12 Conditional operator1277.13 Assignment operators1277.13.1 Simple assignment1287.13.2 Compound assignment1307.13.3 Event assignment1307.14 Expression1307.15 Constant expressions1317.16 Boolean expressions1328. Statements1338.1 End points and reachability1338.2 Blocks1358.2.1 Statement lists1358.3 The empty statement1358.4 Labeled statements1368.5 Declaration statements1368.5.1 Local variable declarations1368.5.2 Local constant declarations1378.6 Expression statements1388.7 Selection statements1388.7.1 The if statement1388.7.2 The switch statement1398.8 Iteration statements1428.8.1 The while statement1438.8.2 The do statement1438.8.3 The for statement1448.8.4 The foreach statement1458.9 Jump statements1468.9.1 The break statement1468.9.2 The continue statement1478.9.3 The goto statement1478.9.4 The return statement1488.9.5 The throw statement1498.10 The try statement1508.11 The checked and unchecked statements1528.12 The lock statement1529. Namespaces1559.1 Compilation units1559.2 Namespace declarations1559.3 Using directives1569.3.1 Using alias directives1579.3.2 Using namespace directives1599.4 Namespace members1619.5 Type declarations16110. Classes16310.1 Class declarations16310.1.1 Class modifiers16310.1.1.1 Abstract classes16310.1.1.2 Sealed classes16410.1.2 Class base specification16410.1.2.1 Base classes16410.1.2.2 Interface implementations16510.1.3 Class body16610.2 Class members16610.2.1 Inheritance16710.2.2 The new modifier16710.2.3 Access modifiers16810.2.4 Constituent types16810.2.5 Static and instance members16810.2.6 Nested types16910.3 Constants16910.4 Fields17010.4.1 Static and instance fields17110.4.2 Readonly fields17210.4.2.1 Using static readonly fields for constants17210.4.2.2 Versioning of constants and static readonly fields17210.4.3 Field initialization17310.4.4 Variable initializers17310.4.4.1 Static field initialization17410.4.4.2 Instance field initialization17410.5 Methods17510.5.1 Method parameters17610.5.1.1 Value parameters17710.5.1.2 Reference parameters17710.5.1.3 Output parameters17810.5.1.4 Params parameters17810.5.2 Static and instance methods18010.5.3 Virtual methods18010.5.4 Override methods18210.5.5 Abstract methods18310.5.6 External methods18410.5.7 Method body18510.5.8 Method overloading18510.6 Properties18510.6.1 Static properties18610.6.2 Accessors18710.6.3 Virtual, override, and abstract accessors19110.7 Events19310.8 Indexers19610.8.1 Indexer overloading19910.9 Operators19910.9.1 Unary operators20010.9.2 Binary operators20010.9.3 Conversion operators20110.10 Instance constructors20210.10.1 Constructor initializers20310.10.2 Instance variable initializers20310.10.3 Constructor execution20310.10.4 Default constructors20510.10.5 Private constructors20610.10.6 Optional constructor parameters20610.11 Destructors20610.12 Static constructors20710.12.1 Class loading and initialization20811. Structs21111.1 Struct declarations21111.1.1 Struct modifiers21111.1.2 Interfaces21111.1.3 Struct body21111.2 Struct members21111.3 Struct examples21111.3.1 Database integer type21111.3.2 Database boolean type21312. Arrays21512.1 Array types21512.1.1 The System.Array type21612.2 Array creation21612.3 Array element access21612.4 Array members21612.5 Array covariance21612.6 Array initializers21713. Interfaces21913.1 Interface declarations21913.1.1 Interface modifiers21913.1.2 Base interfaces21913.1.3 Interface body22013.2 Interface members22013.2.1 Interface methods22113.2.2 Interface properties22113.2.3 Interface events22213.2.4 Interface indexers22213.2.5 Interface member access22213.3 Fully qualified interface member names22413.4 Interface implementations22413.4.1 Explicit interface member implementations22513.4.2 Interface mapping22713.4.3 Interface implementation inheritance22913.4.4 Interface re-implementation23113.4.5 Abstract classes and interfaces23214. Enums23314.1 Enum declarations23314.2 Enum members23414.3 Enum values and operations23615. Delegates23715.1 Delegate declarations23715.1.1 Delegate modifiers23716. Exceptions23917. Attributes24117.1 Attribute classes24117.1.1 The AttributeUsage attribute24117.1.2 Positional and named parameters24217.1.3 Attribute parameter types24217.2 Attribute specification24317.3 Attribute instances24517.3.1 Compilation of an attribute24517.3.2 Run-time retrieval of an attribute instance24517.4 Reserved attributes24517.4.1 The AttributeUsage attribute24617.4.2 The Conditional attribute24617.4.3 The Obsolete attribute24818. Versioning25119. Unsafe code25319.1 Unsafe code25319.2 Pointer types25320. Interoperability25520.1 Attributes25520.1.1 The COMImport attribute25520.1.2 The COMSourceInterfaces attribute25520.1.3 The COMVisibility attribute25520.1.4 The DispId attribute25620.1.5 The DllImport attribute25620.1.6 The GlobalObject attribute25720.1.7 The Guid attribute25720.1.8 The HasDefaultInterface attribute25720.1.9 The ImportedFromCOM attribute25720.1.10 The In and Out attributes25720.1.11 The InterfaceType attribute25820.1.12 The IsCOMRegisterFunction attribute25820.1.13 The Marshal attribute25820.1.14 The Name attribute25920.1.15 The NoIDispatch attribute25920.1.16 The NonSerialized attribute25920.1.17 The Predeclared attribute26020.1.18 The ReturnsHResult attribute26020.1.19 The Serializable attribute26020.1.20 The StructLayout attribute26020.1.21 The StructOffset attribute26120.1.22 The TypeLibFunc attribute26120.1.23 The TypeLibType attribute26120.1.24 The TypeLibVar attribute26220.2 Supporting enums26221. References265Copyright Microsoft Corporation 1999-2000. All Rights Reserved.265Chapter 21 References1. IntroductionC# 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 combine the high productivity of Visual Basic and the raw 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 Subset (CLS), a sort of lingua franca that ensures seamless 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 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, worldThe canonical “Hello, world” program can be written in C# as follows:using System;class Hellostatic void Main() Console.WriteLine(Hello, world);The default file extension for C# programs is .cs, as in hello.cs. Such a program can be compiled with the command line directivecsc hello.cswhich produces an executable program named hello.exe. The output of the program is:Hello, worldClose examination of this program is illuminating: The using System; directive references a namespace called System that is provided by the .NET runtime. This namespace contains the Console class referred to in the Main method. Namespaces provide a hierarchical means of organizing the elements of a class library. A “using” directive enables unqualified use of the members of a namespace. The “Hello, world” program uses Console.WriteLine as a shorthand for System.Console.WriteLine. What do these identifiers denote? System is a namespace, Console is a class defined in that namespace, and WriteLine is a static method defined on that class. The Main function is a static member of the class Hello. Functions and variables are not supported at the global level; such elements are always contained within type declarations (e.g., class and struct declarations). 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 a few things that do not appear in the “Hello, world” program. The program does not use either “:” or “-” operators. The “:” is not an operator in C# at all, and the “-” operator is used in only a small fraction of C# programs. C# programs use “.” as a separator in compound names such as Console.WriteLine. The program does not contain forward declarations. Forward declarations are never needed in C# programs, as declaration order is not significant. The program does not use #include to import program text. Dependencies between programs are handled symbolically rather than with program text. This system 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 managementManual 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 are freed from this burdensome task. In the vast majority of cases, this automatic memory management increases code quality and enhances developer productivity without negatively impacting either expressiveness or performance.The exampleusing System;public class Stackprivate Node first = null;public bool Empty get return (first = null);public object Pop() if (first = null) throw new Exception(Cant Pop from an empty Stack.);else object temp = first.Value;first = first.Next;return temp;public void Push(object o) first = new Node(o, first);class Nodepublic Node Next;public object Value;
展开阅读全文