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.
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Table of Contents
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
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
iii
C# LANGUAGE REFERENCE
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
iv
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Table of Contents
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
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
v
Chapter 21 References
21. References
Unicode Consortium. The Unicode Standard, Version 3.0. Addison-Wesley, Reading, Massachusetts, 2000,
ISBN 0-201-616335-5.
IEEEE. IEEE Standard for Binary Floating-Point Arithmetic. ANSI/IEEE Standard 754-1985. Available from
.
ISO/IEC. C++. ANSI/ISO/IEC 14882:1998.
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
265