(Parte 1 de 10)

Language Specification Version 3.0

Copyright Microsoft Corporation 1999-2007. All Rights Reserved. Please send corrections, comments, and other feedback to sharp@microsoft.com

Notice © 1999-2007 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-2007. All Rights Reserved.

Table of Contents

1. Introduction1
1.1 Hello world1
1.2 Program structure2
1.3 Types and variables4
1.4 Expressions6
1.5 Statements8
1.6 Classes and objects12
1.6.1 Members12
1.6.2 Accessibility13
1.6.3 Type parameters13
1.6.4 Base classes14
1.6.5 Fields14
1.6.6 Methods15 Parameters15 Method body and local variables16 Static and instance methods17 Virtual, override, and abstract methods18 Method overloading20
1.6.7 Other function members21 Constructors2 Properties23 Indexers23 Events24 Operators24 Destructors25
1.7 Structs25
1.8 Arrays26
1.9 Interfaces27
1.10 Enums29
1.1 Delegates30
1.12 Attributes31
2. Lexical structure3
2.1 Programs3
2.2 Grammars3
2.2.1 Grammar notation3
2.2.2 Lexical grammar34
2.2.3 Syntactic grammar34
2.3 Lexical analysis34
2.3.1 Line terminators35
2.3.2 Comments35
2.3.3 White space37
2.4 Tokens37
2.4.1 Unicode character escape sequences37
2.4.2 Identifiers38
2.4.3 Keywords40
2.4.4 Literals40 Boolean literals40 Integer literals40

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.i Real literals42 Character literals42 String literals43 The null literal45
2.4.5 Operators and punctuators45
2.5 Pre-processing directives45
2.5.1 Conditional compilation symbols47
2.5.2 Pre-processing expressions47
2.5.3 Declaration directives48
2.5.4 Conditional compilation directives49
2.5.5 Diagnostic directives51
2.5.6 Region directives52
2.5.7 Line directives52
2.5.8 Pragma directives53 Pragma warning53
3. Basic concepts5
3.1 Application Startup5
3.2 Application termination56
3.3 Declarations56
3.4 Members58
3.4.1 Namespace members58
3.4.2 Struct members59
3.4.3 Enumeration members59
3.4.4 Class members59
3.4.5 Interface members60
3.4.6 Array members60
3.4.7 Delegate members60
3.5 Member access60
3.5.1 Declared accessibility60
3.5.2 Accessibility domains61
3.5.3 Protected access for instance members63
3.5.4 Accessibility constraints64
3.6 Signatures and overloading65
3.7 Scopes6
3.7.1 Name hiding69 Hiding through nesting69 Hiding through inheritance70
3.8 Namespace and type names71
3.8.1 Fully qualified names73
3.9 Automatic memory management74
3.10 Execution order76
4. Types7
4.1 Value types7
4.1.1 The System.ValueType type78
4.1.2 Default constructors78
4.1.3 Struct types79
4.1.4 Simple types79
4.1.5 Integral types80
4.1.6 Floating point types81

C# Language Specification ivCopyright Microsoft Corporation 1999-2007. All Rights Reserved.

4.1.7 The decimal type82
4.1.8 The bool type83
4.1.9 Enumeration types83
4.1.10 Nullable types83
4.2 Reference types84
4.2.1 Class types84
4.2.2 The object type85
4.2.3 The string type85
4.2.4 Interface types85
4.2.5 Array types85
4.2.6 Delegate types85
4.3 Boxing and unboxing86
4.3.1 Boxing conversions86
4.3.2 Unboxing conversions87
4.4 Constructed types8
4.4.1 Type arguments89
4.4.2 Open and closed types89
4.4.3 Bound and unbound types89
4.4.4 Satisfying constraints89
4.5 Type parameters90
4.6 Expression tree types91
5. Variables93
5.1 Variable categories93
5.1.1 Static variables93
5.1.2 Instance variables93 Instance variables in classes94 Instance variables in structs94
5.1.3 Array elements94
5.1.4 Value parameters94
5.1.5 Reference parameters94
5.1.6 Output parameters95
5.1.7 Local variables95
5.2 Default values96
5.3 Definite assignment96
5.3.1 Initially assigned variables97
5.3.2 Initially unassigned variables97
5.3.3 Precise rules for determining definite assignment97 General rules for statements98 Block statements, checked, and unchecked statements98 Expression statements98 Declaration statements9 If statements9 Switch statements9 While statements9 Do statements100 For statements100 Break, continue, and goto statements100 Throw statements100 Return statements100 Try-catch statements101

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.v Try-finally statements101 Try-catch-finally statements101 Foreach statements102 Using statements102 Lock statements103 Yield statements103 General rules for simple expressions103 General rules for expressions with embedded expressions103 Invocation expressions and object creation expressions104 Simple assignment expressions104 && expressions104 || expressions105 ! expressions106 ?? expressions106 ?: expressions107 Anonymous functions107
5.4 Variable references108
5.5 Atomicity of variable references108
6. Conversions109
6.1 Implicit conversions109
6.1.1 Identity conversion109
6.1.2 Implicit numeric conversions109
6.1.3 Implicit enumeration conversions110
6.1.4 Implicit nullable conversions110
6.1.5 Null literal conversions110
6.1.6 Implicit reference conversions1
6.1.7 Boxing conversions1
6.1.8 Implicit constant expression conversions112
6.1.9 Implicit conversions involving type parameters112
6.1.10 User-defined implicit conversions112
6.1.1 Anonymous function conversions and method group conversions12
6.2 Explicit conversions112
6.2.1 Explicit numeric conversions113
6.2.2 Explicit enumeration conversions114
6.2.3 Explicit nullable conversions115
6.2.4 Explicit reference conversions115
6.2.5 Unboxing conversions116
6.2.6 Explicit conversions involving type parameters116
6.2.7 User-defined explicit conversions117
6.3 Standard conversions117
6.3.1 Standard implicit conversions117
6.3.2 Standard explicit conversions118
6.4 User-defined conversions118
6.4.1 Permitted user-defined conversions118
6.4.2 Lifted conversion operators118
6.4.3 Evaluation of user-defined conversions118
6.4.4 User-defined implicit conversions119
6.4.5 User-defined explicit conversions120
6.5 Anonymous function conversions121
6.5.1 Evaluation of anonymous function conversions to delegate types122

C# Language Specification viCopyright Microsoft Corporation 1999-2007. All Rights Reserved.

6.5.2 Evaluation of anonymous function conversions to expression tree types123
6.5.3 Implementation example123
6.6 Method group conversions125
7. Expressions128
7.1 Expression classifications128
7.1.1 Values of expressions129
7.2 Operators129
7.2.1 Operator precedence and associativity129
7.2.2 Operator overloading130
7.2.3 Unary operator overload resolution132
7.2.4 Binary operator overload resolution132
7.2.5 Candidate user-defined operators132
7.2.6 Numeric promotions133 Unary numeric promotions133 Binary numeric promotions133
7.2.7 Lifted operators134
7.3 Member lookup135
7.3.1 Base types136
7.4 Function members136
7.4.1 Argument lists138
7.4.2 Type inference141 The first phase142 The second phase142 Input types142 Output types142 Dependence142 Output type inferences142 Explicit parameter type inferences143 Exact inferences143 Lower-bound inferences143 Fixing143 Inferred return type143 Type inference for conversion of method groups145 Finding the best common type of a set of expressions145
7.4.3 Overload resolution145 Applicable function member146 Better function member146 Better conversion from expression147 Better conversion from type147 Overloading in generic classes148
7.4.4 Function member invocation149 Invocations on boxed instances150
7.5 Primary expressions150
7.5.1 Literals151
7.5.2 Simple names151 Invariant meaning in blocks152
7.5.3 Parenthesized expressions153
7.5.4 Member access153 Identical simple names and type names155 Grammar ambiguities155

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.vii

7.5.5 Invocation expressions156 Method invocations156 Extension method invocations158 Delegate invocations160
7.5.6 Element access160 Array access160 Indexer access161
7.5.7 This access162
7.5.8 Base access162
7.5.9 Postfix increment and decrement operators163
7.5.10 The new operator164 Object creation expressions164 Object initializers165 Collection initializers167 Array creation expressions168 Delegate creation expressions170 Anonymous object creation expressions172
7.5.1 The typeof operator173
7.5.12 The checked and unchecked operators175
7.5.13 Default value expressions177
7.5.14 Anonymous method expressions177
7.6 Unary operators177
7.6.1 Unary plus operator178
7.6.2 Unary minus operator178
7.6.3 Logical negation operator179
7.6.4 Bitwise complement operator179
7.6.5 Prefix increment and decrement operators179
7.6.6 Cast expressions180
7.7 Arithmetic operators181
7.7.1 Multiplication operator181
7.7.2 Division operator182
7.7.3 Remainder operator183
7.7.4 Addition operator184
7.7.5 Subtraction operator185
7.8 Shift operators187
7.9 Relational and type-testing operators188
7.9.1 Integer comparison operators189
7.9.2 Floating-point comparison operators189
7.9.3 Decimal comparison operators190
7.9.4 Boolean equality operators190
7.9.5 Enumeration comparison operators191
7.9.6 Reference type equality operators191
7.9.7 String equality operators192
7.9.8 Delegate equality operators193
7.9.9 Equality operators and null193
7.9.10 The is operator193
7.9.1 The as operator194
7.10 Logical operators195
7.10.1 Integer logical operators195
7.10.2 Enumeration logical operators196
7.10.3 Boolean logical operators196

C# Language Specification viiiCopyright Microsoft Corporation 1999-2007. All Rights Reserved.

7.10.4 Nullable boolean logical operators196
7.1 Conditional logical operators197
7.1.1 Boolean conditional logical operators197
7.1.2 User-defined conditional logical operators197
7.12 The null coalescing operator198
7.13 Conditional operator199
7.14 Anonymous function expressions199
7.14.1 Anonymous function signatures201
7.14.2 Anonymous function bodies201
7.14.3 Overload resolution202
7.14.4 Outer variables203 Captured outer variables203 Instantiation of local variables204
7.14.5 Evaluation of anonymous function expressions206
7.15 Query expressions206
7.15.1 Ambiguities in query expressions207
7.15.2 Query expression translation207 Select and groupby clauses with continuations208 Explicit range variable types208 Degenerate query expressions209 From, let, where, join and orderby clauses209 Select clauses213 Groupby clauses213 Transparent identifiers213
7.15.3 The query expression pattern215
7.16 Assignment operators216
7.16.1 Simple assignment217
7.16.2 Compound assignment218
7.16.3 Event assignment219
7.17 Expression220
7.18 Constant expressions220
7.19 Boolean expressions221
8. Statements2
8.1 End points and reachability2
8.2 Blocks224
8.2.1 Statement lists224
8.3 The empty statement225
8.4 Labeled statements225
8.5 Declaration statements226
8.5.1 Local variable declarations226
8.5.2 Local constant declarations227
8.6 Expression statements228
8.7 Selection statements228
8.7.1 The if statement228
8.7.2 The switch statement229
8.8 Iteration statements232
8.8.1 The while statement233
8.8.2 The do statement233
8.8.3 The for statement234
8.8.4 The foreach statement235

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.ix

8.9 Jump statements237
8.9.1 The break statement238
8.9.2 The continue statement239
8.9.3 The goto statement239
8.9.4 The return statement240
8.9.5 The throw statement241
8.10 The try statement242
8.1 The checked and unchecked statements245
8.12 The lock statement245
8.13 The using statement246
8.14 The yield statement248
9. Namespaces250
9.1 Compilation units250
9.2 Namespace declarations250
9.3 Extern aliases252
9.4 Using directives252
9.4.1 Using alias directives253
9.4.2 Using namespace directives255
9.5 Namespace members257
9.6 Type declarations257
9.7 Namespace alias qualifiers258
9.7.1 Uniqueness of aliases259
10. Classes260
10.1 Class declarations260
10.1.1 Class modifiers260 Abstract classes261 Sealed classes261 Static classes261
10.1.2 Partial modifier262
10.1.3 Type parameters262
10.1.4 Class base specification263 Base classes263 Interface implementations264
10.1.5 Type parameter constraints265
10.1.6 Class body269
10.2 Partial types269
10.2.1 Attributes269
10.2.2 Modifiers269
10.2.3 Type parameters and constraints270
10.2.4 Base class270
10.2.5 Base interfaces270
10.2.6 Members271
10.2.7 Partial methods271
10.2.8 Name binding274
10.3 Class members274
10.3.1 The instance type276
10.3.2 Members of constructed types276
10.3.3 Inheritance277
10.3.4 The new modifier278

C# Language Specification xCopyright Microsoft Corporation 1999-2007. All Rights Reserved.

10.3.5 Access modifiers278
10.3.6 Constituent types278
10.3.7 Static and instance members278
10.3.8 Nested types279 Fully qualified name280 Declared accessibility280 Hiding280 this access281 Access to private and protected members of the containing type282 Nested types in generic classes283
10.3.9 Reserved member names283 Member names reserved for properties284 Member names reserved for events284 Member names reserved for indexers284 Member names reserved for destructors285
10.4 Constants285
10.5 Fields286
10.5.1 Static and instance fields288
10.5.2 Readonly fields288 Using static readonly fields for constants289 Versioning of constants and static readonly fields289
10.5.3 Volatile fields290
10.5.4 Field initialization291
10.5.5 Variable initializers291 Static field initialization292 Instance field initialization293
10.6 Methods294
10.6.1 Method parameters295 Value parameters296 Reference parameters297 Output parameters298 Parameter arrays298
10.6.2 Static and instance methods301
10.6.3 Virtual methods301
10.6.4 Override methods303
10.6.5 Sealed methods305
10.6.6 Abstract methods306
10.6.7 External methods307
10.6.8 Partial methods308
10.6.9 Extension methods308
10.6.10 Method body308
10.6.1 Method overloading309
10.7 Properties309
10.7.1 Static and instance properties310
10.7.2 Accessors311
10.7.3 Automatically implemented properties315
10.7.4 Accessibility316
10.7.5 Virtual, sealed, override, and abstract accessors317
10.8 Events319
10.8.1 Field-like events321
10.8.2 Event accessors322

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.xi

10.8.3 Static and instance events323
10.8.4 Virtual, sealed, override, and abstract accessors324
10.9 Indexers324
10.9.1 Indexer overloading328
10.10 Operators328
10.10.1 Unary operators330
10.10.2 Binary operators330
10.10.3 Conversion operators331
10.1 Instance constructors3
10.1.1 Constructor initializers334
10.1.2 Instance variable initializers335
10.1.3 Constructor execution335
10.1.4 Default constructors337
10.1.5 Private constructors337
10.1.6 Optional instance constructor parameters338
10.12 Static constructors338
10.13 Destructors340
10.14 Iterators342
10.14.1 Enumerator interfaces342
10.14.2 Enumerable interfaces342
10.14.3 Yield type342
10.14.4 Enumerator objects342 The MoveNext method343 The Current property344 The Dispose method344
10.14.5 Enumerable objects344 The GetEnumerator method345
10.14.6 Implementation example345
1. Structs352
1.1 Struct declarations352
1.1.1 Struct modifiers352
1.1.2 Partial modifier353
1.1.3 Struct interfaces353
1.1.4 Struct body353
1.2 Struct members353
1.3 Class and struct differences353
1.3.1 Value semantics354
1.3.2 Inheritance355
1.3.3 Assignment355
1.3.4 Default values355
1.3.5 Boxing and unboxing356
1.3.6 Meaning of this357
1.3.7 Field initializers358
1.3.8 Constructors358
1.3.9 Destructors359
1.3.10 Static constructors359
1.4 Struct examples359
1.4.1 Database integer type359
1.4.2 Database boolean type361
12. Arrays363

C# Language Specification xiiCopyright Microsoft Corporation 1999-2007. All Rights Reserved.

12.1 Array types363
12.1.1 The System.Array type364
12.1.2 Arrays and the generic IList interface364
12.2 Array creation364
12.3 Array element access365
12.4 Array members365
12.5 Array covariance365
12.6 Array initializers365
13. Interfaces368
13.1 Interface declarations368
13.1.1 Interface modifiers368
13.1.2 Partial modifier368
13.1.3 Base interfaces369
13.1.4 Interface body369
13.2 Interface members369
13.2.1 Interface methods371
13.2.2 Interface properties371
13.2.3 Interface events371
13.2.4 Interface indexers371
13.2.5 Interface member access372
13.3 Fully qualified interface member names373
13.4 Interface implementations374
13.4.1 Explicit interface member implementations375
13.4.2 Uniqueness of implemented interfaces377
13.4.3 Implementation of generic methods378
13.4.4 Interface mapping378
13.4.5 Interface implementation inheritance381
13.4.6 Interface re-implementation382
13.4.7 Abstract classes and interfaces384
14. Enums385
14.1 Enum declarations385
14.2 Enum modifiers385
14.3 Enum members386
14.4 The System.Enum type388
14.5 Enum values and operations388
15. Delegates389
15.1 Delegate declarations389
15.2 Delegate compatibility391
15.3 Delegate instantiation391
15.4 Delegate invocation392
16. Exceptions395
16.1 Causes of exceptions395
16.2 The System.Exception class395
16.3 How exceptions are handled395
16.4 Common Exception Classes396
17. Attributes398
17.1 Attribute classes398

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.xiii

17.1.1 Attribute usage398
17.1.2 Positional and named parameters399
17.1.3 Attribute parameter types400
17.2 Attribute specification400
17.3 Attribute instances405
17.3.1 Compilation of an attribute406
17.3.2 Run-time retrieval of an attribute instance406
17.4 Reserved attributes406
17.4.1 The AttributeUsage attribute406
17.4.2 The Conditional attribute407 Conditional methods407 Conditional attribute classes409
17.4.3 The Obsolete attribute410
17.5 Attributes for Interoperation411
17.5.1 Interoperation with COM and Win32 components412
17.5.2 Interoperation with other .NET languages412 The IndexerName attribute412
18. Unsafe code413
18.1 Unsafe contexts413
18.2 Pointer types415
18.3 Fixed and moveable variables418
18.4 Pointer conversions418
18.5 Pointers in expressions419
18.5.1 Pointer indirection420
18.5.2 Pointer member access420
18.5.3 Pointer element access421
18.5.4 The address-of operator422
18.5.5 Pointer increment and decrement423
18.5.6 Pointer arithmetic423
18.5.7 Pointer comparison424
18.5.8 The sizeof operator424
18.6 The fixed statement425
18.7 Fixed size buffers428
18.7.1 Fixed size buffer declarations428
18.7.2 Fixed size buffers in expressions430
18.7.3 Definite assignment checking430
18.8 Stack allocation431
18.9 Dynamic memory allocation432
A. Documentation comments434
A.1 Introduction434
A.2 Recommended tags435
A.2.1 <c>436
A.2.2 <code>436
A.2.3 <example>437
A.2.4 <exception>437
A.2.5 <include>438
A.2.6 <list>438
A.2.7 <para>439
A.2.8 <param>440

C# Language Specification xivCopyright Microsoft Corporation 1999-2007. All Rights Reserved.

A.2.9 <paramref>440
A.2.10 <permission>440
A.2.1 <summary>441
A.2.12 <returns>441
A.2.13 <see>442
A.2.14 <seealso>442
A.2.15 <summary>443
A.2.16 <value>443
A.2.17 <typeparam>443
A.2.18 <typeparamref>4
A.3 Processing the documentation file4
A.3.1 ID string format4
A.3.2 ID string examples445
A.4 An example449
A.4.1 C# source code449
A.4.2 Resulting XML451
B. Grammar456
B.1 Lexical grammar456
B.1.1 Line terminators456
B.1.2 Comments456
B.1.3 White space457
B.1.4 Tokens457
B.1.5 Unicode character escape sequences457
B.1.6 Identifiers458
B.1.7 Keywords459
B.1.8 Literals459
B.1.9 Operators and punctuators461
B.1.10 Pre-processing directives461
B.2 Syntactic grammar464
B.2.1 Basic concepts464
B.2.2 Types464
B.2.3 Variables465
B.2.4 Expressions466
B.2.5 Statements472
B.2.6 Namespaces476
B.2.7 Classes477
B.2.8 Structs484
B.2.9 Arrays484
B.2.10 Interfaces485
B.2.1 Enums486
B.2.12 Delegates486
B.2.13 Attributes487
B.3 Grammar extensions for unsafe code488
C. References492

Table of Contents Copyright Microsoft Corporation 1999-2007. All Rights Reserved.xv

Chapter Erro! Estilo não definido. Erro! Estilo não definido. 1. 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"); }

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

Copyright Microsoft Corporation 1999-2007. All Rights Reserved.1

C# Language Specification 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; } class Entry { public Entry next; public object data;

2Copyright Microsoft Corporation 1999-2007. All Rights Reserved.

Chapter Erro! Estilo não definido. Erro! Estilo não definido.

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() {

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:

(Parte 1 de 10)