Table of Contents Preface... 13 What this book is about?... 13 Who is this book for?... 14 What to read next?... 14 Personages... 14 Style conventions... 15 More information... 15 Bare Necessities... 17 C# C-Sharp... 17 CLI Common Language Infrastructure a.k.a. CLR Common Language Runtime... 17 Your first C# program Hello-o-o, world!... 18 Program entry point... 19 Good news:.net Framework SDK... 19 Compiling the first program... 19 Automatic memory management... 21 System.Console object... 21 Basics... 22 Identifiers... 22 Keywords... 23 Data types... 24 Unified type system... 24 Predefined simple types... 25 Value types... 26 Real numbers... 26 Enumeration (enum) types... 26 Structure (struct) types... 27 Reference types... 28 Predefined reference types... 28 Class types... 28 Interface types... 29 Delegate types... 30 Array types... 30 Boxing and Unboxing... 30 Unsafe code and Pointers... 31 Literals... 31 Boolean literals... 31 Integer literals... 32 Real literals... 32 Character literals... 33 String literals... 34 Assemblies... 35 Namespaces a.k.a. libraries, Members and Access... 35
8 Members... 37 Accessibility declaration and access modifiers... 38 Accessibility domains... 39 Scope... 40 Expressions... 43 Operator precedence... 43 Operators... 44 Overloadable operators... 46 Parenthesized expression (x)... 46 Member access x.y... 46 Invocation a.k.a. member (function) call f(x)... 46 Argument lists... 47 The ref and out parameters... 47 Variable number of parameters... 49 Element access a[x]... 49 Postfix increment x++ and decrement x--... 50 this and base access... 50 Create a new instance: new()... 51 Creating an object... 51 Creating an array... 52 Creating a delegate... 52 typeof... 53 checked and unchecked... 54 Unary + and -... 54 Unary! and ~... 55 Prefix increment ++x and decrement --x... 55 Typecast (T)x... 55 Implicit conversions... 56 Explicit conversions... 56 Multiplicative: Multiplication *, Division /, and Remainder %... 57 Additive: Addition + and Subtraction -... 58 Shift left << and shift right >>... 58 Relational: < > <= >=... 59 Equality: == and!=... 59 Relational: is... 60 No-exceptions typecast: as... 61 Logical and : &, exclusive or : ^, and or :... 61 Conditional and : &&, and or :... 62 Conditional?:... 62 Assignment = *= /= %= += -= <<= >>= &= ^= =... 62 Statements and flow control... 63 End points and reachability... 63
Statement lists and blocks... 63 Empty statement... 63 Local declarations... 64 Expression... 65 Labels and goto statement... 65 break and continue statements... 66 if statement... 66 switch statement... 67 while statement... 69 do statement... 69 for statement... 69 foreach statement... 70 return statement... 71 throw statement... 71 try statement... 72 checked and unchecked statements... 73 lock statement... 74 Preprocessor... 75 #define and #undef... 75 #if, #elif, #else and #endif... 76 Preprocessor expressions... 76 #error and #warning... 77 #region and #endregion... 77 #line... 78 Unsafe code... 79 Compare value, reference and pointer types... 79 Marking code as unsafe... 80 Unsafe code and Pointers... 80 sizeof()... 81 stackalloc()... 81 Fixed and movable variables... 82 fixed statement... 82 Classes and Objects... 85 Object-Oriented Language(s)... 85 Objects and Messages... 85 Abstract Classes, Properties, and Messages... 86 Classes and Objects, Instances... 86 Inheritance... 87 Interfaces... 87 Polymorphism... 88 Classes in C#... 88 Simple class... 88 Class declarations... 89 Static members... 90 Inheritance related modifiers... 92 abstract modifier... 92 9
10 virtual modifier... 92 override modifier... 92 new modifier... 92 sealed modifier... 92 extern modifier... 93 Fields... 93 Methods... 95 The ref and out parameters... 96 Operators... 96 Properties... 100 Indexers... 103 Events... 104 Instance constructors... 106 Static constructors... 109 Destructors... 111 Structures... 113 Treating this in structure methods... 115 Including itself... 116 Namespaces... 116 Namespace declarations... 116 Using namespaces using... 117 Enumerations... 118 Enumeration (enum) declarations... 118 Inheritance... 119 Inheritance... 119 Signatures, overloading methods... 120 Polymorphism, downcasting, upcasting... 122 Virtual methods, overriding and hiding methods... 123 Abstract classes and methods... 125 Sealed classes and methods... 126 Interfaces... 127 Interface declarations... 127 Using interfaces... 128 Explicit interface member implementations... 131 Ambiguity with interface member implementations... 132 Abstract class vs. interface... 133 Delegates... 133 Parameters and return value... 135 Nature of delegates... 135 Delegate declaration... 135 Delegate instantiation... 136 Delegate invocation... 136 Attributes... 136 AttributeUsage attribute... 137
Conditional attribute... 138 Obsolete attribute... 139 Interoperability with native code... 141 Attributes... 141 ComAliasName attribute... 141 ComImport attribute... 142 ComRegisterFunction attribute... 142 ComSourceInterfaces attribute... 143 ComUnregisterFunction attribute... 143 ComVisible attribute... 143 DispId attribute... 143 DllImport attribute... 143 FieldOffset attribute... 144 Guid attribute... 144 HasDefaultInterface attribute... 144 ImportedFromTypeLib attribute... 144 In and Out attributes... 144 IndexerName attribute... 145 InterfaceType attribute... 145 MarshalAs attribute... 145 NoIDispatch attribute... 145 PreserveSig attribute... 145 StructLayout attribute... 145 TypeLibFunc attribute... 145 TypeLibType attribute... 146 TypeLibVar attribute... 146 Calling COM and ActiveX Objects from C# - practical approach... 146 Creating interop assembly... 146 Using interop assembly... 147 Primary interop assembly (PIA)... 147 Dangers of alternative interop assembly... 148 Base Class Library.Net Framework SDK... 148 Appendix A. Keywords... 149 Appendix B. Formal Grammar... 157 Lexical grammar... 157 Line terminators... 158 White space... 158 Comments... 158 Tokens... 159 Unicode character escape sequences... 159 Identifiers... 159 Keywords... 160 Literals... 160 11
12 Operators and punctuators... 162 Pre-processing directives... 162 Syntactic grammar... 164 Basic concepts... 164 Types... 165 Variables... 166 Expressions... 166 Statements... 169 Namespaces... 172 Classes... 173 Structs... 179 Arrays... 179 Interfaces... 180 Enums... 181 Delegates... 181 Attributes... 182 Grammar extensions for unsafe code... 183 Unsafe contexts... 183 Pointer types... 184 Pointers in expressions... 184 Pointer indirection... 184 Pointer member access... 184 The address-of operator... 185 The sizeof operator... 185 The fixed statement... 185 Stack allocation... 185 Appendix C. C# Compiler Command-Line Options... 187 Index... 189