《C#语言参考手册.docx》由会员分享,可在线阅读,更多相关《C#语言参考手册.docx(251页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C# Language ReferenceOwners:Anders Hejlsberg and Scott WiltamuthFile:C# Language Reference.docLast saved:1/2/2013Last printed: 1/2/2013Version 0.17bNoticeThis documentation is an early release of the final documentation, which may be changed substantially prior to final commercial release, and is in
2、formation of Microsoft Corporation.This document is proxided 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 d
3、ocument 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 doaiment may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electron
4、ic, 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
5、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, Vis
6、ual 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 Contents1. Introduction11.1 Hello, world11.2 Automatic memo
7、ry management21.3 Types41.4 Prlefined types51.5 Array types71.6 Type system unification91.7 Statements101.7.1 Statement lists and blocks101.7.2 Labeled statements and got o statements101.7.3 Local declarations of constants and variables111.7.4 Expression statements111.7.5 The if statement111.7.6 The
8、 switch statement121.7.7 The while statement121.7.8 The do statement131.7.9 The fbr statement131.7.10 The f or each statement131.7.11 The break statement and the cont i nue statement141.7.12 The return statement141.7.13 The throw statement141.7.14 The t r y statement141.7.15 The checked and unchecke
9、d statements141.7.16 The I oc k statement141.8 Classes141.9 Structs151.10 Interfaces151.11 Delegates171.12 Enums181.13 Namespaces181.14 Properties191.15 Indexers201.16 Events211.17 Versioning222. Lexical structure272.1 Phases of translation272.2 Grammar notation272.3 Pre-processing282.3.1 Pre-proces
10、sing declarations282.3.2 #if, #elif, #clse, #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.5 White space332.5 Processing of Unicode character escape sequences34
11、2.5.1 Literals362533 Reaf literals372.5.2 Operators and punctuators403. Basic concepts3.1 Members3.1.1 Namespace members3.1.2 Struct members3.1.3 Enumeration members3.1.4 Class members3.1.5 Interface members3.1.6 Array members3.1.7 Delegate members3.2 Member access3.2.1 Declared accessibility3.2.2 A
12、ccessibility domains3.2.3 Protected access3.2.4 Accessibility constraints3.3 Signatures and overloading3.4 Scopes3.4.1 Name hiding3.4.1.1 Hiding through nesting.3.4.1.2 Hiding through inheritan)3.5 Namespace and type names3.5.1 Fully qualified names4. Types4.1 Value types4.1.1 Default constructors4.
13、1.2 Struct types4.1.3 Simple types4.1.4 Integral types4.1.5 Floating point types4.1.6 The decimal type4.1.7 The bool type4.1.8 Enumeration types43434344444444444444454748495052525354554.2 Reference types4.2.1 Class types4.2.2 The object type4.2.3 The string type4.2.4 Interface types4.2.5 Array types
14、4.2.6 Delegate types4.3 Boxing and unboxing.4.3.1 Boxing conversions.4.3.2 Unboxing conversions5.1.2 Instance variables675.1.2.2 Instance variables instructs685.1.5 Reference parameters686. Conversions736.1 Implicit conversions736.1.1 Identity conversion736.1.2 Implicit numeric conversions736.1.3 Im
15、plicit enumeration conversions746.1.4 Implicit reference conversions746.1.6 Implicit constant expression conversions746.2.1 Explicit numeric conversions756.2.2 Explicit enumeration conversions766.2.4 Unboxing conversions776.2.5 User-defined explicit conversions776.3.1 Standard implicit conversions77
16、6.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 conversions 80817. Expressions7.2.1 Operator precedence and associativity *7
17、.2.3 Unary operator overload resolution* .7.2.4 Binary operator overload resolution 7.2.5 Candidate user-defined operators7.2.6.1 Una.ry numeric promotions7.3 Member lookup7.4.2.1 Applicable function member *.7.4.2.2 Better iu net ion member-.l.-7.4.3 Function member invocation7.4.3.1 Invocations on
18、 boxed instances7.4.4 Virtual function member lookup7.4.5 Interface function member lookup7.5.2.】 Invariant meaning in blocks7.5.4.1 Identical simple names and type names7.5.6.2 Indexer access7.5.8 Base access7.5.9 Postfix increment and decrement operators 1.1.10.1 Object creation expressions1.1.10.
19、2 Array creation expressions1.1.10.3 Delegate creation expressions7.5 . 13 checked and unchecked operators.,.八.*.55566677 9 11222344444566889 9 00 01122 G 346788001 - 888888 8 8 8.9 99999999999999999 0-0000 0 0000000011117.6 Unary expressions7.6.1 Bitwise complement opemtor Ill7.6.7 Prefix increment
20、 and decrement operatois. *1127 *6-8 C*ast express ions.+.4.+.*+.*+.+*+*+.+*.+4*+.*.+*.*4.+.*.1137.7.1 Multiplication operator1137.7.2 Division operator1147.7.4 Addition operator.1167.7.5 Subtraction operator 1177.9 Relational operators1197.9.2 Floating-point comparison operators 1217.9.3 Decimal co
21、mparison operators 1217.9.4 Boolean equality operators. 1227.9.5 Enumeration comparison operators 1227.9.6 Reference type equality operators 1227.9.7 String equality operators 1237.9.8 Delegate equality operators. 1247.9.9 The is operator1241.1.1 1 Integer logical operators 1241.1.3 3 Boolean logica
22、l operators. 1257.11 Conditional logical operators. 1257.11.1 Boolean conditional logical operators. 1267.11.2 User-defined conditional logical operatois 1267.12 Conditional operator 1277.13 Assignment operators 1277.15 Constant expressions1318. Statements1338.1 End points and reachability 1338.1.1
23、Statement lists 1358.4 Labeleu statemeni.* * 1368.5 DecIsrotion statements 1368.5.1 Local variable declarations1368.5.2 Local constant declarations1378.6 Expression statements13887 Selection statements.*. I381.1.1 The if statement1381.1.2 The switch statement1398.8 Iteration statements8.8.1 The whil
24、e statement,.8.8.2 The do statement8.8.3 The for statement8.8.4 The f or e ac h statement.8.9 Jump statements8.9.1 The break statement8.9.2 The continue statement8.9.3 The goto statement 8.9.4 The return statement 8.9.5 The throw statement8.10 The try statement8.11 The checked and unchecked statemen
25、ts8.12 The lock statement9. Namespaces9.1 Compilation units9.2 Namespace declarations9.3 Using directives9.3.1 Using alias diiectives9.3.2 Using namespace directives.9.4 Namespace members.9.5 Type declarations .9.5.1 1 Class modifiers9.5.1.1 1 Abstract classes 9.5.1.2 2 Sealed classes9.5.2 2 Class b
26、ase specification9.5.2.1 1 Base classes9.5.2.2 2 Interface implementations9.5.3 3 Class body10.2.1 Inheritance10.2.2 The new modifier.10.2.3 Access modifiers10.2.4 Constituent types10.2.5 Static and instance members 10.2.6 Nested types10.4.1 Static and instance fields.1.1.1.1 1 Using static readonly
27、 fields for constants1.1.1.2 2 Versioning of constants and static readonly fields10.4.3 Field initialization10.4.4 Variable initializers10.4.4.1 Static field initialization1421431431441451461461471471481491501521525 5 5 6 7 9 115 5 5 5 5 5 6 63334445667788899012 223344566 6 6886 86666666 6 777777777
28、710.4.4.2 Instance field initialization10.5.2 Static mid instsncc methods.10.5.4 Override methods. .,.*.10.6.3 Virtual, override, and abstract accessors10.9.3 Conversion operators*10.10 Instance constructors 10.10.1 Constructor initializers .*. .10.10.2 Instance variable initializers . 10.10.4 Defau
29、lt constructors10.10.5 Private constructors10.10.6 Optional constructor parameters10.11 Destructors1.1 12.1 Class loading and initialization.7677777878808082838485858586879l93969999ooooa)020203030505()60607081.2 1! 、truct n iodit ier亠.,.*+ *+*.,., . . 21112. Arrays 21512.1 Array types. 21512.6 Array
30、 initializers21713. Interfaces21913.1.2 Base interfaces21913.2 Interface members2201 3.2.3 Interface events/22213 2 4 Interface indexers22213.3 Fully qualified interface member names22413.4 Interface implementations 22413.4.1 Explicit interface member implementations22513.4.2 Interface mapping22713.
31、4.3 Interface implementation inheritance22913.4.4 Interface re-implementation23113.4.5 Abstract classes and interfaces 23214. Enums23314.1 Enum declarations23314.3 Enum values and operations23615. Delegates23715.3.1 Delegate modifiers23716. Exceptions23917. Attributes24117.1.1 The At tribute Us age
32、attribute24117.1.2 Positional and named parameters 24217.2 Attribute specification24317.3.2 Run-time retrieval of an attribute instance24517.4.1 The At tribute Us age attribute24618. Versioning25119. Unsafe code25320. Interoperability25520.1 Attnbutes20.1.1 The COM mpor t attribute20.1.2 The COMS ou
33、rcelmerfacei attribute20.1.3 The COMVi s i bi I i t y attribute20.1.4 The Di s p I d attribute20.1.5 The DI I I mp o r t attribute20.1.6 The Gl o b a I Ob j e c t attribute20.1.7 The Gu i d attribute20.1.8 The Has De faultinterface attribute20.1.9 The I mpor t e dFr omCOM attribute20.1.10 The 1 n an
34、d Ou t attributes20.1.11 The I nt er f aceType attribute20.1.12 The I s COMRe gisterFunction attribute20.1.13 The Ma r s ha I attribute20.1.14 The Na me attribute20.1.15 The No I Di s pa t c h attribute20.1.16 The NonSe r i a I i ze d attribute20.1.17 The Predeclared attribute20.1.18 The Rei ur nsHR
35、esul t attribute20.1.19 The Serializable attribute20.1.20 The Si ruct Layout attribute20.1.21 The Si r uc t Of f s et attribute20.1.22 The Type Li bFunc attribute20.1.23 The Type Li bType attribute20.1.24 The Type Li bVar attribute5555667777788555555555555522222222222229 9 9 0 0 0 012 日 555666666666
36、22222222222220.2 Supporting enums1. IntroductionC# is a simple, modem, 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
37、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 Genera
38、tion 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
39、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 lan
40、guage. 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
41、early programs and the reading of later chapters.1.1 Hello, worldThe canonical 4iHello, world program can be written in C# as follows:using System:class He I I oIstatic voi d Nfa i n() Cons ol e . W i t eLi ne( He I I o, wor I d); )The default Hie extension for C# programs is. cs,as in hello.es. Suc
42、h a program can be compiled with the command line directiveesc hello.eswhich produces an executable program named hello.exe. The output of the program is:Hei 1 o, worldClose examination of this program is illuminating: The using System; directive references a namespace called System that is provided
43、 by the .NET runtime. This namespace contains the Cons o 1 e class referred to in the Ma i n 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 Cons ole.WiieLine as a shorthand for Sys t e m. Cons o 1 e