About ................................................................................................................................................................................... 1
Chapter 1: Getting started with C# Language ............................................................................................... 2
Section 1.1: Creating a new console application (Visual Studio) ............................................................................... 2
Section 1.2: Creating a new project in Visual Studio (console application) and Running it in Debug mode
.................................................................................................................................................................................. 4
Section 1.3: Creating a new program using .NET Core .............................................................................................. 7
Section 1.4: Creating a new program using Mono ..................................................................................................... 9
Section 1.5: Creating a new query using LinqPad ...................................................................................................... 9
Section 1.6: Creating a new project using Xamarin Studio ...................................................................................... 12
Chapter 2: Verbatim Strings ................................................................................................................................. 18
Section 2.1: Interpolated Verbatim Strings ............................................................................................................... 18
Section 2.2: Escaping Double Quotes ....................................................................................................................... 18
Section 2.3: Verbatim strings instruct the compiler to not use character escapes .............................................. 18
Section 2.4: Multiline Strings ....................................................................................................................................... 19
Chapter 3: Operators ................................................................................................................................................ 20
Section 3.1: Overloadable Operators ......................................................................................................................... 20
Section 3.2: Overloading equality operators ............................................................................................................ 21
Section 3.3: Relational Operators .............................................................................................................................. 22
Section 3.4: Implicit Cast and Explicit Cast Operators ............................................................................................. 24
Section 3.5: Short-circuiting Operators ..................................................................................................................... 25
Section 3.6: ? : Ternary Operator ............................................................................................................................... 26
Section 3.7: ?. (Null Conditional Operator) ................................................................................................................ 27
Section 3.8: "Exclusive or" Operator .......................................................................................................................... 27
Section 3.9: default Operator ..................................................................................................................................... 27
Section 3.10: Assignment operator '=' ........................................................................................................................ 28
Section 3.11: sizeof ........................................................................................................................................................ 28
Section 3.12: ?? Null-Coalescing Operator ................................................................................................................ 29
Section 3.13: Bit-Shifting Operators ........................................................................................................................... 29
Section 3.14: => Lambda operator ............................................................................................................................. 29
Section 3.15: Class Member Operators: Null Conditional Member Access ............................................................ 30
Section 3.16: Class Member Operators: Null Conditional Indexing ......................................................................... 31
Section 3.17: Postfix and Prefix increment and decrement ..................................................................................... 31
Section 3.18: typeof ..................................................................................................................................................... 31
Section 3.19: Binary operators with assignment ...................................................................................................... 32
Section 3.20: nameof Operator ................................................................................................................................. 32
Section 3.21: Class Member Operators: Member Access ........................................................................................ 33
Section 3.22: Class Member Operators: Function Invocation ................................................................................. 33
Section 3.23: Class Member Operators: Aggregate Object Indexing .................................................................... 33
Chapter 4: Extension Methods ............................................................................................................................. 34
Section 4.1: Extension methods - overview ............................................................................................................... 34
Section 4.2: Null checking ........................................................................................................................................... 36
Section 4.3: Explicitly using an extension method .................................................................................................... 37
Section 4.4: Generic Extension Methods ................................................................................................................... 37
Section 4.5: Extension methods can only see public (or internal) members of the extended class .................. 39
Section 4.6: Extension methods for chaining ............................................................................................................ 39
Section 4.7: Extension methods with Enumeration .................................................................................................. 40
Section 4.8: Extension methods dispatch based on static type ............................................................................. 41
, Section 4.9: Extension methods on Interfaces ......................................................................................................... 42
Section 4.10: Extension methods in combination with interfaces ........................................................................... 43
Section 4.11: Extension methods aren't supported by dynamic code .................................................................... 43
Section 4.12: Extensions and interfaces together enable DRY code and mixin-like functionality ...................... 44
Section 4.13: IList<T> Extension Method Example: Comparing 2 Lists ................................................................... 45
Section 4.14: Extension methods as strongly typed wrappers ............................................................................... 46
Section 4.15: Using Extension methods to create beautiful mapper classes ........................................................ 46
Section 4.16: Using Extension methods to build new collection types (e.g. DictList) ............................................ 47
Section 4.17: Extension methods for handling special cases .................................................................................. 48
Section 4.18: Using Extension methods with Static methods and Callbacks ......................................................... 49
Chapter 5: Collection Initializers ......................................................................................................................... 51
Section 5.1: Collection initializers ................................................................................................................................ 51
Section 5.2: C# 6 Index Initializers .............................................................................................................................. 51
Section 5.3: Collection initializers in custom classes ................................................................................................ 52
Section 5.4: Using collection initializer inside object initializer ................................................................................ 53
Section 5.5: Collection Initializers with Parameter Arrays ....................................................................................... 54
Chapter 6: String Interpolation ........................................................................................................................... 55
Section 6.1: Format dates in strings ........................................................................................................................... 55
Section 6.2: Padding the output ................................................................................................................................. 55
Section 6.3: Expressions .............................................................................................................................................. 56
Section 6.4: Formatting numbers in strings .............................................................................................................. 56
Section 6.5: Simple Usage .......................................................................................................................................... 57
Chapter 7: An overview of C# collections ...................................................................................................... 58
Section 7.1: HashSet<T> .............................................................................................................................................. 58
Section 7.2: Dictionary<TKey, TValue> ..................................................................................................................... 58
Section 7.3: SortedSet<T> ........................................................................................................................................... 59
Section 7.4: T[ ] (Array of T) ...................................................................................................................................... 59
Section 7.5: List<T> ...................................................................................................................................................... 60
Section 7.6: Stack<T> .................................................................................................................................................. 60
Section 7.7: LinkedList<T> ........................................................................................................................................... 60
Section 7.8: Queue ....................................................................................................................................................... 61
Chapter 8: Constructors and Finalizers ........................................................................................................... 62
Section 8.1: Static constructor .................................................................................................................................... 62
Section 8.2: Singleton constructor pattern ............................................................................................................... 63
Section 8.3: Default Constructor ................................................................................................................................ 63
Section 8.4: Forcing a static constructor to be called ............................................................................................. 64
Section 8.5: Calling a constructor from another constructor ................................................................................. 65
Section 8.6: Calling the base class constructor ........................................................................................................ 65
Section 8.7: Finalizers on derived classes ................................................................................................................. 66
Section 8.8: Exceptions in static constructors .......................................................................................................... 66
Section 8.9: Constructor and Property Initialization ................................................................................................ 67
Section 8.10: Generic Static Constructors ................................................................................................................. 69
Section 8.11: Calling virtual methods in constructor ................................................................................................. 69
Chapter 9: Static Classes ........................................................................................................................................ 71
Section 9.1: Static Classes ........................................................................................................................................... 71
Section 9.2: Static class lifetime ................................................................................................................................. 71
Section 9.3: Static keyword ........................................................................................................................................ 72
Chapter 10: Keywords ............................................................................................................................................... 73
Section 10.1: as ............................................................................................................................................................. 73
Section 10.2: goto ........................................................................................................................................................ 74
,Section 10.3: volatile .................................................................................................................................................... 75
Section 10.4: checked, unchecked .............................................................................................................................. 76
Section 10.5: virtual, override, new ............................................................................................................................. 77
Section 10.6: stackalloc ............................................................................................................................................... 80
Section 10.7: break ....................................................................................................................................................... 81
Section 10.8: const ....................................................................................................................................................... 83
Section 10.9: async, await ........................................................................................................................................... 84
Section 10.10: for .......................................................................................................................................................... 85
Section 10.11: abstract .................................................................................................................................................. 86
Section 10.12: fixed ....................................................................................................................................................... 87
Section 10.13: default ................................................................................................................................................... 88
Section 10.14: sealed .................................................................................................................................................... 89
Section 10.15: is ............................................................................................................................................................. 89
Section 10.16: this ......................................................................................................................................................... 90
Section 10.17: readonly ................................................................................................................................................ 91
Section 10.18: typeof .................................................................................................................................................... 92
Section 10.19: foreach .................................................................................................................................................. 92
Section 10.20: dynamic ............................................................................................................................................... 93
Section 10.21: try, catch, finally, throw ....................................................................................................................... 94
Section 10.22: void ....................................................................................................................................................... 95
Section 10.23: namespace .......................................................................................................................................... 95
Section 10.24: ref, out .................................................................................................................................................. 96
Section 10.25: base ...................................................................................................................................................... 97
Section 10.26: float, double, decimal ......................................................................................................................... 99
Section 10.27: operator ............................................................................................................................................. 100
Section 10.28: char .................................................................................................................................................... 101
Section 10.29: params ............................................................................................................................................... 101
Section 10.30: while .................................................................................................................................................... 102
Section 10.31: null ....................................................................................................................................................... 104
Section 10.32: continue .............................................................................................................................................. 105
Section 10.33: string ................................................................................................................................................... 105
Section 10.34: return .................................................................................................................................................. 106
Section 10.35: unsafe ................................................................................................................................................. 106
Section 10.36: switch .................................................................................................................................................. 108
Section 10.37: var ....................................................................................................................................................... 109
Section 10.38: when ................................................................................................................................................... 110
Section 10.39: lock ..................................................................................................................................................... 111
Section 10.40: uint ...................................................................................................................................................... 112
Section 10.41: if, if...else, if... else if ............................................................................................................................. 112
Section 10.42: static ................................................................................................................................................... 113
Section 10.43: internal ............................................................................................................................................... 115
Section 10.44: using ................................................................................................................................................... 116
Section 10.45: where .................................................................................................................................................. 116
Section 10.46: int ........................................................................................................................................................ 118
Section 10.47: ulong ................................................................................................................................................... 119
Section 10.48: true, false ........................................................................................................................................... 119
Section 10.49: struct .................................................................................................................................................. 119
Section 10.50: extern ................................................................................................................................................. 120
Section 10.51: bool ...................................................................................................................................................... 121
Section 10.52: interface ............................................................................................................................................. 121
Section 10.53: delegate ............................................................................................................................................. 122
, Section 10.54: unchecked ......................................................................................................................................... 122
Section 10.55: ushort ................................................................................................................................................. 123
Section 10.56: sizeof .................................................................................................................................................. 123
Section 10.57: in ......................................................................................................................................................... 123
Section 10.58: implicit ................................................................................................................................................ 124
Section 10.59: do ........................................................................................................................................................ 124
Section 10.60: long ..................................................................................................................................................... 125
Section 10.61: enum ................................................................................................................................................... 125
Section 10.62: partial ................................................................................................................................................. 126
Section 10.63: event ................................................................................................................................................... 127
Section 10.64: sbyte ................................................................................................................................................... 128
Chapter 11: Object Oriented Programming In C# ..................................................................................... 129
Section 11.1: Classes: .................................................................................................................................................. 129
Chapter 12: Inheritance .......................................................................................................................................... 130
Section 12.1: Inheritance. Constructors' calls sequence ......................................................................................... 130
Section 12.2: Inheriting from a base class .............................................................................................................. 132
Section 12.3: Inheriting from a class and implementing an interface .................................................................. 133
Section 12.4: Inheriting from a class and implementing multiple interfaces ...................................................... 133
Section 12.5: Constructors In A Subclass ................................................................................................................. 134
Section 12.6: Inheritance Anti-patterns ................................................................................................................... 134
Section 12.7: Extending an abstract base class ...................................................................................................... 135
Section 12.8: Testing and navigating inheritance .................................................................................................. 136
Section 12.9: Inheriting methods .............................................................................................................................. 136
Section 12.10: Base class with recursive type specification ................................................................................... 137
Chapter 13: Generics ................................................................................................................................................ 140
Section 13.1: Implicit type inference (methods) ...................................................................................................... 140
Section 13.2: Type inference (classes) .................................................................................................................... 141
Section 13.3: Using generic method with an interface as a constraint type ....................................................... 141
Section 13.4: Type constraints (new-keyword) ...................................................................................................... 142
Section 13.5: Type constraints (classes and interfaces) ........................................................................................ 143
Section 13.6: Checking equality of generic values ................................................................................................. 144
Section 13.7: Reflecting on type parameters .......................................................................................................... 145
Section 13.8: Covariance ........................................................................................................................................... 145
Section 13.9: Contravariance .................................................................................................................................... 146
Section 13.10: Invariance ........................................................................................................................................... 147
Section 13.11: Variant interfaces ............................................................................................................................... 147
Section 13.12: Variant delegates ............................................................................................................................... 148
Section 13.13: Variant types as parameters and return values ............................................................................ 149
Section 13.14: Type Parameters (Interfaces) .......................................................................................................... 149
Section 13.15: Type constraints (class and struct) ................................................................................................. 150
Section 13.16: Explicit type parameters ................................................................................................................... 150
Section 13.17: Type Parameters (Classes) ............................................................................................................... 150
Section 13.18: Type Parameters (Methods) ............................................................................................................. 151
Section 13.19: Generic type casting .......................................................................................................................... 151
Section 13.20: Configuration reader with generic type casting ............................................................................ 152
Chapter 14: Using Statement ............................................................................................................................. 154
Section 14.1: Using Statement Basics ....................................................................................................................... 154
Section 14.2: Gotcha: returning the resource which you are disposing ............................................................... 155
Section 14.3: Multiple using statements with one block ......................................................................................... 156
Section 14.4: Gotcha: Exception in Dispose method masking other errors in Using blocks .............................. 157