Notes For Professionals: 700+ Pages
Notes For Professionals: 700+ Pages
C#
Notes for Professionals
700+ pages
of professional hints and tricks
Disclaimer
[Link] This is an unocial free book created for educational purposes and is
not aliated with ocial C# group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
Contents
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: Literals ...................................................................................................................................................... 18
Section 2.1: uint literals ................................................................................................................................................ 18
Section 2.2: int literals ................................................................................................................................................. 18
Section 2.3: sbyte literals ............................................................................................................................................ 18
Section 2.4: decimal literals ........................................................................................................................................ 18
Section 2.5: double literals .......................................................................................................................................... 18
Section 2.6: float literals .............................................................................................................................................. 18
Section 2.7: long literals .............................................................................................................................................. 18
Section 2.8: ulong literal ............................................................................................................................................. 18
Section 2.9: string literals ............................................................................................................................................ 19
Section 2.10: char literals ............................................................................................................................................ 19
Section 2.11: byte literals ............................................................................................................................................. 19
Section 2.12: short literal ............................................................................................................................................. 19
Section 2.13: ushort literal ........................................................................................................................................... 19
Section 2.14: bool literals ............................................................................................................................................. 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 ..................................................................................................................................... 28
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 ............................................................ 31
Section 3.16: Class Member Operators: Null Conditional Indexing ......................................................................... 31
Section 3.17: Postfix and Prefix increment and decrement ..................................................................................... 31
Section 3.18: typeof ..................................................................................................................................................... 32
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: Conditional Statements .................................................................................................................. 34
Section 4.1: If-Else Statement ..................................................................................................................................... 34
Section 4.2: If statement conditions are standard boolean expressions and values .......................................... 34
Section 4.3: If-Else If-Else Statement ......................................................................................................................... 35
Chapter 5: Equality Operator ............................................................................................................................... 36
Section 5.1: Equality kinds in c# and equality operator ........................................................................................... 36
Chapter 6: Equals and GetHashCode ............................................................................................................... 37
Section 6.1: Writing a good GetHashCode override ................................................................................................. 37
Section 6.2: Default Equals behavior ......................................................................................................................... 37
Section 6.3: Override Equals and GetHashCode on custom types ........................................................................ 38
Section 6.4: Equals and GetHashCode in IEqualityComparator ............................................................................. 39
Chapter 7: Null-Coalescing Operator ............................................................................................................... 41
Section 7.1: Basic usage .............................................................................................................................................. 41
Section 7.2: Null fall-through and chaining .............................................................................................................. 41
Section 7.3: Null coalescing operator with method calls ......................................................................................... 42
Section 7.4: Use existing or create new .................................................................................................................... 43
Section 7.5: Lazy properties initialization with null coalescing operator ............................................................... 43
Chapter 8: Null-conditional Operators ............................................................................................................ 44
Section 8.1: Null-Conditional Operator ...................................................................................................................... 44
Section 8.2: The Null-Conditional Index .................................................................................................................... 44
Section 8.3: Avoiding NullReferenceExceptions ....................................................................................................... 45
Section 8.4: Null-conditional Operator can be used with Extension Method ........................................................ 45
Chapter 9: nameof Operator ................................................................................................................................ 47
Section 9.1: Basic usage: Printing a variable name .................................................................................................. 47
Section 9.2: Raising PropertyChanged event ........................................................................................................... 47
Section 9.3: Argument Checking and Guard Clauses .............................................................................................. 48
Section 9.4: Strongly typed MVC action links ........................................................................................................... 48
Section 9.5: Handling PropertyChanged events ...................................................................................................... 49
Section 9.6: Applied to a generic type parameter ................................................................................................... 49
Section 9.7: Printing a parameter name ................................................................................................................... 50
Section 9.8: Applied to qualified identifiers ............................................................................................................... 50
Chapter 10: Verbatim Strings ............................................................................................................................... 51
Section 10.1: Interpolated Verbatim Strings .............................................................................................................. 51
Section 10.2: Escaping Double Quotes ...................................................................................................................... 51
Section 10.3: Verbatim strings instruct the compiler to not use character escapes ............................................ 51
Section 10.4: Multiline Strings ..................................................................................................................................... 52
Chapter 11: Common String Operations .......................................................................................................... 53
Section 11.1: Formatting a string ................................................................................................................................. 53
Section 11.2: Correctly reversing a string .................................................................................................................. 53
Section 11.3: Padding a string to a fixed length ........................................................................................................ 54
Section 11.4: Getting x characters from the right side of a string ........................................................................... 55
Section 11.5: Checking for empty String using [Link]() and [Link]()
................................................................................................................................................................................ 56
Section 11.6: Trimming Unwanted Characters O the Start and/or End of Strings ............................................. 57
Section 11.7: Convert Decimal Number to Binary,Octal and Hexadecimal Format .............................................. 57
Section 11.8: Construct a string from Array .............................................................................................................. 57
Section 11.9: Formatting using ToString .................................................................................................................... 58
Section 11.10: Splitting a String by another string ..................................................................................................... 59
Section 11.11: Splitting a String by specific character ................................................................................................ 59
Section 11.12: Getting Substrings of a given string ................................................................................................... 59
Section 11.13: Determine whether a string begins with a given sequence ............................................................. 59
Section 11.14: Getting a char at specific index and enumerating the string ........................................................... 59
Section 11.15: Joining an array of strings into a new one ........................................................................................ 60
Section 11.16: Replacing a string within a string ........................................................................................................ 60
Section 11.17: Changing the case of characters within a String .............................................................................. 60
Section 11.18: Concatenate an array of strings into a single string ........................................................................ 61
Section 11.19: String Concatenation ............................................................................................................................ 61
Chapter 12: [Link] ...................................................................................................................................... 62
Section 12.1: Since C# 6.0 ............................................................................................................................................ 62
Section 12.2: Places where [Link] is 'embedded' in the framework .......................................................... 62
Section 12.3: Create a custom format provider ....................................................................................................... 62
Section 12.4: Date Formatting .................................................................................................................................... 63
Section 12.5: Currency Formatting ............................................................................................................................ 64
Section 12.6: Using custom number format .............................................................................................................. 65
Section 12.7: Align left/ right, pad with spaces ......................................................................................................... 65
Section 12.8: Numeric formats ................................................................................................................................... 66
Section 12.9: ToString() ............................................................................................................................................... 66
Section 12.10: Escaping curly brackets inside a [Link]() expression ......................................................... 67
Section 12.11: Relationship with ToString() ................................................................................................................ 67
Chapter 13: String Concatenate .......................................................................................................................... 68
Section 13.1: + Operator ............................................................................................................................................... 68
Section 13.2: Concatenate strings using [Link] ....................................................................... 68
Section 13.3: Concat string array elements using [Link] ................................................................................. 68
Section 13.4: Concatenation of two strings using $ ................................................................................................. 69
Chapter 14: String Manipulation ......................................................................................................................... 70
Section 14.1: Replacing a string within a string ......................................................................................................... 70
Section 14.2: Finding a string within a string ............................................................................................................. 70
Section 14.3: Removing (Trimming) white-space from a string ............................................................................. 70
Section 14.4: Splitting a string using a delimiter ....................................................................................................... 71
Section 14.5: Concatenate an array of strings into a single string ......................................................................... 71
Section 14.6: String Concatenation ............................................................................................................................ 71
Section 14.7: Changing the case of characters within a String ............................................................................... 71
Chapter 15: String Interpolation ......................................................................................................................... 73
Section 15.1: Format dates in strings ......................................................................................................................... 73
Section 15.2: Padding the output ............................................................................................................................... 73
Section 15.3: Expressions ............................................................................................................................................. 74
Section 15.4: Formatting numbers in strings ............................................................................................................ 74
Section 15.5: Simple Usage ......................................................................................................................................... 75
Chapter 16: String Escape Sequences .............................................................................................................. 76
Section 16.1: Escaping special symbols in string literals .......................................................................................... 76
Section 16.2: Unicode character escape sequences ................................................................................................ 76
Section 16.3: Escaping special symbols in character literals .................................................................................. 76
Section 16.4: Using escape sequences in identifiers ................................................................................................ 76
Section 16.5: Unrecognized escape sequences produce compile-time errors ..................................................... 77
Chapter 17: StringBuilder ........................................................................................................................................ 78
Section 17.1: What a StringBuilder is and when to use one ..................................................................................... 78
Section 17.2: Use StringBuilder to create string from a large number of records ............................................... 79
Chapter 18: Regex Parsing ..................................................................................................................................... 80
Section 18.1: Single match ........................................................................................................................................... 80
Section 18.2: Multiple matches ................................................................................................................................... 80
Chapter 19: DateTime Methods ........................................................................................................................... 81
Section 19.1: DateTime Formatting ............................................................................................................................ 81
Section 19.2: [Link](Double) ................................................................................................................ 82
Section 19.3: [Link](Double) ............................................................................................................... 82
Section 19.4: [Link](String) ........................................................................................................................ 82
Section 19.5: [Link](String, DateTime) ................................................................................................ 82
Section 19.6: [Link](Double) .................................................................................................... 83
Section 19.7: [Link](DateTime t1, DateTime t2 ) ................................................................................ 83
Section 19.8: [Link](Int32, Int32) ................................................................................................. 83
Section 19.9: [Link](Int32) ................................................................................................................... 84
Section 19.10: Pure functions warning when dealing with DateTime ..................................................................... 84
Section 19.11: [Link](String, String, IFormatProvider, DateTimeStyles, DateTime)
................................................................................................................................................................................ 84
Section 19.12: [Link](TimeSpan) ................................................................................................................... 86
Section 19.13: Parse and TryParse with culture info ................................................................................................. 86
Section 19.14: DateTime as initializer in for-loop ...................................................................................................... 87
Section 19.15: [Link](String, String, IFormatProvider) ................................................................. 87
Section 19.16: DateTime ToString, ToShortDateString, ToLongDateString and ToString formatted ................. 88
Section 19.17: Current Date ......................................................................................................................................... 88
Chapter 20: Arrays ..................................................................................................................................................... 89
Section 20.1: Declaring an array ................................................................................................................................ 89
Section 20.2: Initializing an array filled with a repeated non-default value ......................................................... 89
Section 20.3: Copying arrays ..................................................................................................................................... 90
Section 20.4: Comparing arrays for equality ........................................................................................................... 90
Section 20.5: Multi-dimensional arrays ..................................................................................................................... 91
Section 20.6: Getting and setting array values ........................................................................................................ 91
Section 20.7: Iterate over an array ............................................................................................................................ 91
Section 20.8: Creating an array of sequential numbers ......................................................................................... 92
Section 20.9: Jagged arrays ...................................................................................................................................... 92
Section 20.10: Array covariance ................................................................................................................................ 94
Section 20.11: Arrays as IEnumerable<> instances ................................................................................................... 94
Section 20.12: Checking if one array contains another array ................................................................................ 94
Chapter 21: O(n) Algorithm for circular rotation of an array .............................................................. 96
Section 21.1: Example of a generic method that rotates an array by a given shift ............................................. 96
Chapter 22: Enum ....................................................................................................................................................... 98
Section 22.1: Enum basics ........................................................................................................................................... 98
Section 22.2: Enum as flags ....................................................................................................................................... 99
Section 22.3: Using << notation for flags ................................................................................................................ 101
Section 22.4: Test flags-style enum values with bitwise logic .............................................................................. 101
Section 22.5: Add and remove values from flagged enum .................................................................................. 102
Section 22.6: Enum to string and back ................................................................................................................... 102
Section 22.7: Enums can have unexpected values ................................................................................................ 103
Section 22.8: Default value for enum == ZERO ...................................................................................................... 103
Section 22.9: Adding additional description information to an enum value ....................................................... 104
Section 22.10: Get all the members values of an enum ........................................................................................ 105
Section 22.11: Bitwise Manipulation using enums ................................................................................................... 105
Chapter 23: Tuples ................................................................................................................................................... 106
Section 23.1: Accessing tuple elements ................................................................................................................... 106
Section 23.2: Creating tuples ................................................................................................................................... 106
Section 23.3: Comparing and sorting Tuples ......................................................................................................... 106
Section 23.4: Return multiple values from a method ............................................................................................ 107
Chapter 24: Guid ....................................................................................................................................................... 108
Section 24.1: Getting the string representation of a Guid ..................................................................................... 108
Section 24.2: Creating a Guid ................................................................................................................................... 108
Section 24.3: Declaring a nullable GUID ................................................................................................................. 108
Chapter 25: BigInteger .......................................................................................................................................... 110
Section 25.1: Calculate the First 1,000-Digit Fibonacci Number ............................................................................ 110
Chapter 26: Collection Initializers ..................................................................................................................... 111
Section 26.1: Collection initializers ............................................................................................................................ 111
Section 26.2: C# 6 Index Initializers ......................................................................................................................... 111
Section 26.3: Collection initializers in custom classes ............................................................................................ 112
Section 26.4: Using collection initializer inside object initializer ............................................................................ 113
Section 26.5: Collection Initializers with Parameter Arrays ................................................................................... 114
Chapter 27: An overview of C# collections ................................................................................................. 115
Section 27.1: HashSet<T> .......................................................................................................................................... 115
Section 27.2: Dictionary<TKey, TValue> ................................................................................................................. 115
Section 27.3: SortedSet<T> ....................................................................................................................................... 116
Section 27.4: T[ ] (Array of T) .................................................................................................................................. 116
Section 27.5: List<T> .................................................................................................................................................. 117
Section 27.6: Stack<T> .............................................................................................................................................. 117
Section 27.7: LinkedList<T> ...................................................................................................................................... 117
Section 27.8: Queue .................................................................................................................................................. 118
Chapter 28: Looping ................................................................................................................................................ 119
Section 28.1: For Loop ............................................................................................................................................... 119
Section 28.2: Do - While Loop .................................................................................................................................. 120
Section 28.3: Foreach Loop ...................................................................................................................................... 120
Section 28.4: Looping styles ..................................................................................................................................... 121
Section 28.5: Nested loops ....................................................................................................................................... 122
Section 28.6: continue ............................................................................................................................................... 122
Section 28.7: While loop ............................................................................................................................................ 123
Section 28.8: break ................................................................................................................................................... 123
Chapter 29: Iterators .............................................................................................................................................. 125
Section 29.1: Creating Iterators Using Yield ............................................................................................................ 125
Section 29.2: Simple Numeric Iterator Example ..................................................................................................... 126
Chapter 30: IEnumerable ...................................................................................................................................... 127
Section 30.1: IEnumerable with custom Enumerator ............................................................................................. 127
Section 30.2: IEnumerable<int> ................................................................................................................................ 128
Chapter 31: Value type vs Reference type .................................................................................................. 129
Section 31.1: Passing by reference using ref keyword ........................................................................................... 129
Section 31.2: Changing values elsewhere ............................................................................................................... 130
Section 31.3: ref vs out parameters ......................................................................................................................... 131
Section 31.4: Assignment .......................................................................................................................................... 132
Section 31.5: Dierence with method parameters ref and out ............................................................................ 132
Section 31.6: Passing by reference .......................................................................................................................... 133
Chapter 32: Built-in Types .................................................................................................................................... 134
Section 32.1: Conversion of boxed value types ...................................................................................................... 134
Section 32.2: Comparisons with boxed value types .............................................................................................. 134
Section 32.3: Immutable reference type - string ................................................................................................... 134
Section 32.4: Value type - char ................................................................................................................................ 135
Section 32.5: Value type - short, int, long (signed 16 bit, 32 bit, 64 bit integers) ................................................. 135
Section 32.6: Value type - ushort, uint, ulong (unsigned 16 bit, 32 bit, 64 bit integers) ...................................... 135
Section 32.7: Value type - bool ................................................................................................................................ 136
Chapter 33: Aliases of built-in types ............................................................................................................... 137
Section 33.1: Built-In Types Table ............................................................................................................................ 137
Chapter 34: Anonymous types .......................................................................................................................... 138
Section 34.1: Anonymous vs dynamic ..................................................................................................................... 138
Section 34.2: Creating an anonymous type ........................................................................................................... 138
Section 34.3: Anonymous type equality .................................................................................................................. 138
Section 34.4: Generic methods with anonymous types ........................................................................................ 139
Section 34.5: Instantiating generic types with anonymous types ........................................................................ 139
Section 34.6: Implicitly typed arrays ....................................................................................................................... 139
Chapter 35: Dynamic type ................................................................................................................................... 141
Section 35.1: Creating a dynamic object with properties ...................................................................................... 141
Section 35.2: Creating a dynamic variable ............................................................................................................. 141
Section 35.3: Returning dynamic ............................................................................................................................. 141
Section 35.4: Handling Specific Types Unknown at Compile Time ...................................................................... 141
Chapter 36: Type Conversion ............................................................................................................................. 143
Section 36.1: Explicit Type Conversion ..................................................................................................................... 143
Section 36.2: MSDN implicit operator example ...................................................................................................... 143
Chapter 37: Casting ................................................................................................................................................. 145
Section 37.1: Checking compatibility without casting ............................................................................................ 145
Section 37.2: Cast an object to a base type ........................................................................................................... 145
Section 37.3: Conversion Operators ........................................................................................................................ 145
Section 37.4: LINQ Casting operations .................................................................................................................... 147
Section 37.5: Explicit Casting .................................................................................................................................... 147
Section 37.6: Safe Explicit Casting (`as` operator) ................................................................................................. 147
Section 37.7: Implicit Casting .................................................................................................................................... 148
Section 37.8: Explicit Numeric Conversions ............................................................................................................ 148
Chapter 38: Nullable types .................................................................................................................................. 149
Section 38.1: Initialising a nullable ............................................................................................................................ 149
Section 38.2: Check if a Nullable has a value ......................................................................................................... 149
Section 38.3: Get the value of a nullable type ........................................................................................................ 149
Section 38.4: Getting a default value from a nullable ........................................................................................... 150
Section 38.5: Default value of nullable types is null .............................................................................................. 150
Section 38.6: Eective usage of underlying Nullable<T> argument .................................................................... 151
Section 38.7: Check if a generic type parameter is a nullable type .................................................................... 152
Chapter 39: Constructors and Finalizers ...................................................................................................... 153
Section 39.1: Static constructor ................................................................................................................................ 153
Section 39.2: Singleton constructor pattern ........................................................................................................... 154
Section 39.3: Default Constructor ............................................................................................................................ 154
Section 39.4: Forcing a static constructor to be called ......................................................................................... 155
Section 39.5: Calling a constructor from another constructor ............................................................................. 156
Section 39.6: Calling the base class constructor .................................................................................................... 156
Section 39.7: Finalizers on derived classes ............................................................................................................. 157
Section 39.8: Exceptions in static constructors ...................................................................................................... 157
Section 39.9: Constructor and Property Initialization ............................................................................................ 158
Section 39.10: Generic Static Constructors ............................................................................................................. 160
Section 39.11: Calling virtual methods in constructor ............................................................................................. 160
Chapter 40: Access Modifiers ............................................................................................................................ 162
Section 40.1: Access Modifiers Diagrams ................................................................................................................ 162
Section 40.2: public ................................................................................................................................................... 163
Section 40.3: private ................................................................................................................................................. 163
Section 40.4: protected internal ............................................................................................................................... 164
Section 40.5: internal ................................................................................................................................................ 165
Section 40.6: protected ............................................................................................................................................. 166
Chapter 41: Interfaces ........................................................................................................................................... 167
Section 41.1: Implementing an interface .................................................................................................................. 167
Section 41.2: Explicit interface implementation ...................................................................................................... 167
Section 41.3: Interface Basics ................................................................................................................................... 169
Section 41.4: IComparable<T> as an Example of Implementing an Interface .................................................... 171
Section 41.5: Implementing multiple interfaces ...................................................................................................... 172
Section 41.6: Why we use interfaces ....................................................................................................................... 172
Section 41.7: "Hiding" members with Explicit Implementation .............................................................................. 173
Chapter 42: Static Classes ................................................................................................................................... 175
Section 42.1: Static Classes ....................................................................................................................................... 175
Section 42.2: Static class lifetime ............................................................................................................................. 175
Section 42.3: Static keyword .................................................................................................................................... 176
Chapter 43: Singleton Implementation ........................................................................................................ 177
Section 43.1: Statically Initialized Singleton ............................................................................................................. 177
Section 43.2: Lazy, thread-safe Singleton (using Lazy<T>) .................................................................................. 177
Section 43.3: Lazy, thread-safe Singleton (using Double Checked Locking) ...................................................... 177
Section 43.4: Lazy, thread safe singleton (for .NET 3.5 or older, alternate implementation) ........................... 178
Chapter 44: Dependency Injection ................................................................................................................. 180
Section 44.1: Dependency Injection C# and [Link] with Unity .......................................................................... 180
Section 44.2: Dependency injection using MEF ...................................................................................................... 182
Chapter 45: Partial class and methods ........................................................................................................ 185
Section 45.1: Partial classes ...................................................................................................................................... 185
Section 45.2: Partial classes inheriting from a base class .................................................................................... 185
Section 45.3: Partial methods .................................................................................................................................. 186
Chapter 46: Object initializers ........................................................................................................................... 187
Section 46.1: Simple usage ....................................................................................................................................... 187
Section 46.2: Usage with non-default constructors ............................................................................................... 187
Section 46.3: Usage with anonymous types ........................................................................................................... 187
Chapter 47: Methods .............................................................................................................................................. 189
Section 47.1: Calling a Method ................................................................................................................................. 189
Section 47.2: Anonymous method ........................................................................................................................... 189
Section 47.3: Declaring a Method ............................................................................................................................ 190
Section 47.4: Parameters and Arguments .............................................................................................................. 190
Section 47.5: Return Types ....................................................................................................................................... 190
Section 47.6: Default Parameters ............................................................................................................................ 191
Section 47.7: Method overloading ........................................................................................................................... 192
Section 47.8: Access rights ....................................................................................................................................... 193
Chapter 48: Extension Methods ........................................................................................................................ 194
Section 48.1: Extension methods - overview ........................................................................................................... 194
Section 48.2: Null checking ....................................................................................................................................... 196
Section 48.3: Explicitly using an extension method ............................................................................................... 197
Section 48.4: Generic Extension Methods ............................................................................................................... 197
Section 48.5: Extension methods can only see public (or internal) members of the extended class .............. 199
Section 48.6: Extension methods for chaining ....................................................................................................... 199
Section 48.7: Extension methods with Enumeration .............................................................................................. 200
Section 48.8: Extension methods dispatch based on static type ......................................................................... 201
Section 48.9: Extension methods on Interfaces ..................................................................................................... 202
Section 48.10: Extension methods in combination with interfaces ....................................................................... 203
Section 48.11: Extension methods aren't supported by dynamic code ................................................................ 203
Section 48.12: Extensions and interfaces together enable DRY code and mixin-like functionality .................. 204
Section 48.13: IList<T> Extension Method Example: Comparing 2 Lists ............................................................... 205
Section 48.14: Extension methods as strongly typed wrappers ........................................................................... 206
Section 48.15: Using Extension methods to create beautiful mapper classes .................................................... 206
Section 48.16: Using Extension methods to build new collection types (e.g. DictList) ........................................ 207
Section 48.17: Extension methods for handling special cases .............................................................................. 208
Section 48.18: Using Extension methods with Static methods and Callbacks .................................................... 209
Chapter 49: Named Arguments ........................................................................................................................ 211
Section 49.1: Argument order is not necessary ...................................................................................................... 211
Section 49.2: Named arguments and optional parameters ................................................................................. 211
Section 49.3: Named Arguments can make your code more clear .................................................................... 211
Chapter 50: Named and Optional Arguments ........................................................................................... 213
Section 50.1: Optional Arguments ........................................................................................................................... 213
Section 50.2: Named Arguments ............................................................................................................................. 214
Chapter 51: Data Annotation .............................................................................................................................. 217
Section 51.1: Data Annotation Basics ....................................................................................................................... 217
Section 51.2: Creating a custom validation attribute ............................................................................................. 217
Section 51.3: Manually Execute Validation Attributes ............................................................................................ 218
Section 51.4: Validation Attributes ........................................................................................................................... 218
Section 51.5: EditableAttribute (data modeling attribute) ..................................................................................... 220
Chapter 52: Keywords ............................................................................................................................................ 222
Section 52.1: as .......................................................................................................................................................... 222
Section 52.2: goto ...................................................................................................................................................... 223
Section 52.3: volatile ................................................................................................................................................. 224
Section 52.4: checked, unchecked ........................................................................................................................... 225
Section 52.5: virtual, override, new .......................................................................................................................... 226
Section 52.6: stackalloc ............................................................................................................................................ 229
Section 52.7: break .................................................................................................................................................... 230
Section 52.8: const .................................................................................................................................................... 232
Section 52.9: async, await ........................................................................................................................................ 233
Section 52.10: for ....................................................................................................................................................... 234
Section 52.11: abstract ............................................................................................................................................... 235
Section 52.12: fixed .................................................................................................................................................... 236
Section 52.13: default ................................................................................................................................................ 237
Section 52.14: sealed ................................................................................................................................................. 238
Section 52.15: is .......................................................................................................................................................... 238
Section 52.16: this ...................................................................................................................................................... 239
Section 52.17: readonly ............................................................................................................................................. 240
Section 52.18: typeof ................................................................................................................................................. 241
Section 52.19: foreach ............................................................................................................................................... 241
Section 52.20: dynamic ............................................................................................................................................ 242
Section 52.21: try, catch, finally, throw .................................................................................................................... 243
Section 52.22: void .................................................................................................................................................... 244
Section 52.23: namespace ....................................................................................................................................... 244
Section 52.24: ref, out ............................................................................................................................................... 245
Section 52.25: base ................................................................................................................................................... 246
Section 52.26: float, double, decimal ....................................................................................................................... 248
Section 52.27: operator ............................................................................................................................................ 249
Section 52.28: char .................................................................................................................................................... 250
Section 52.29: params .............................................................................................................................................. 250
Section 52.30: while ................................................................................................................................................... 251
Section 52.31: null ...................................................................................................................................................... 253
Section 52.32: continue ............................................................................................................................................. 254
Section 52.33: string .................................................................................................................................................. 254
Section 52.34: return ................................................................................................................................................. 255
Section 52.35: unsafe ................................................................................................................................................ 255
Section 52.36: switch ................................................................................................................................................. 257
Section 52.37: var ...................................................................................................................................................... 258
Section 52.38: when .................................................................................................................................................. 259
Section 52.39: lock ..................................................................................................................................................... 260
Section 52.40: uint ..................................................................................................................................................... 261
Section 52.41: if, if...else, if... else if ............................................................................................................................. 261
Section 52.42: static .................................................................................................................................................. 262
Section 52.43: internal ............................................................................................................................................... 264
Section 52.44: using .................................................................................................................................................. 265
Section 52.45: where ................................................................................................................................................. 265
Section 52.46: int ....................................................................................................................................................... 267
Section 52.47: ulong .................................................................................................................................................. 268
Section 52.48: true, false .......................................................................................................................................... 268
Section 52.49: struct .................................................................................................................................................. 268
Section 52.50: extern ................................................................................................................................................. 269
Section 52.51: bool ..................................................................................................................................................... 270
Section 52.52: interface ............................................................................................................................................ 270
Section 52.53: delegate ............................................................................................................................................ 271
Section 52.54: unchecked ......................................................................................................................................... 271
Section 52.55: ushort ................................................................................................................................................. 272
Section 52.56: sizeof .................................................................................................................................................. 272
Section 52.57: in ......................................................................................................................................................... 272
Section 52.58: implicit ............................................................................................................................................... 273
Section 52.59: do ....................................................................................................................................................... 273
Section 52.60: long .................................................................................................................................................... 274
Section 52.61: enum ................................................................................................................................................... 274
Section 52.62: partial ................................................................................................................................................ 275
Section 52.63: event .................................................................................................................................................. 276
Section 52.64: sbyte .................................................................................................................................................. 277
Chapter 53: Object Oriented Programming In C# ................................................................................... 278
Section 53.1: Classes: ................................................................................................................................................. 278
Chapter 54: Recursion ........................................................................................................................................... 279
Section 54.1: Recursion in plain English ................................................................................................................... 279
Section 54.2: Fibonacci Sequence ........................................................................................................................... 279
Section 54.3: PowerOf calculation ........................................................................................................................... 280
Section 54.4: Recursively describe an object structure ........................................................................................ 280
Section 54.5: Using Recursion to Get Directory Tree ............................................................................................ 281
Section 54.6: Factorial calculation ........................................................................................................................... 284
Chapter 55: Naming Conventions .................................................................................................................... 285
Section 55.1: Capitalization conventions ................................................................................................................. 285
Section 55.2: Enums .................................................................................................................................................. 286
Section 55.3: Interfaces ............................................................................................................................................. 286
Section 55.4: Exceptions ........................................................................................................................................... 286
Section 55.5: Private fields ....................................................................................................................................... 287
Section 55.6: Namespaces ....................................................................................................................................... 287
Chapter 56: XML Documentation Comments ............................................................................................. 288
Section 56.1: Simple method annotation ................................................................................................................. 288
Section 56.2: Generating XML from documentation comments .......................................................................... 288
Section 56.3: Method documentation comment with param and returns elements ......................................... 290
Section 56.4: Interface and class documentation comments .............................................................................. 290
Section 56.5: Referencing another class in documentation ................................................................................. 291
Chapter 57: Comments and regions ............................................................................................................... 292
Section 57.1: Comments ............................................................................................................................................ 292
Section 57.2: Regions ................................................................................................................................................ 292
Section 57.3: Documentation comments ................................................................................................................ 293
Chapter 58: Inheritance ........................................................................................................................................ 295
Section 58.1: Inheritance. Constructors' calls sequence ........................................................................................ 295
Section 58.2: Inheriting from a base class .............................................................................................................. 297
Section 58.3: Inheriting from a class and implementing an interface ................................................................. 298
Section 58.4: Inheriting from a class and implementing multiple interfaces ...................................................... 298
Section 58.5: Constructors In A Subclass ................................................................................................................ 299
Section 58.6: Inheritance Anti-patterns ................................................................................................................... 299
Section 58.7: Extending an abstract base class ..................................................................................................... 300
Section 58.8: Testing and navigating inheritance .................................................................................................. 301
Section 58.9: Inheriting methods ............................................................................................................................. 301
Section 58.10: Base class with recursive type specification .................................................................................. 302
Chapter 59: Generics ............................................................................................................................................... 305
Section 59.1: Implicit type inference (methods) ..................................................................................................... 305
Section 59.2: Type inference (classes) ................................................................................................................... 306
Section 59.3: Using generic method with an interface as a constraint type ...................................................... 306
Section 59.4: Type constraints (new-keyword) ..................................................................................................... 307
Section 59.5: Type constraints (classes and interfaces) ....................................................................................... 308
Section 59.6: Checking equality of generic values ................................................................................................ 309
Section 59.7: Reflecting on type parameters ......................................................................................................... 310
Section 59.8: Covariance .......................................................................................................................................... 310
Section 59.9: Contravariance ................................................................................................................................... 311
Section 59.10: Invariance .......................................................................................................................................... 312
Section 59.11: Variant interfaces .............................................................................................................................. 312
Section 59.12: Variant delegates .............................................................................................................................. 313
Section 59.13: Variant types as parameters and return values ........................................................................... 314
Section 59.14: Type Parameters (Interfaces) ......................................................................................................... 314
Section 59.15: Type constraints (class and struct) ................................................................................................. 315
Section 59.16: Explicit type parameters .................................................................................................................. 315
Section 59.17: Type Parameters (Classes) .............................................................................................................. 315
Section 59.18: Type Parameters (Methods) ............................................................................................................ 316
Section 59.19: Generic type casting ......................................................................................................................... 316
Section 59.20: Configuration reader with generic type casting ........................................................................... 317
Chapter 60: Using Statement ............................................................................................................................. 319
Section 60.1: Using Statement Basics ...................................................................................................................... 319
Section 60.2: Gotcha: returning the resource which you are disposing .............................................................. 320
Section 60.3: Multiple using statements with one block ........................................................................................ 321
Section 60.4: Gotcha: Exception in Dispose method masking other errors in Using blocks ............................. 322
Section 60.5: Using statements are null-safe ......................................................................................................... 322
Section 60.6: Using Dispose Syntax to define custom scope ............................................................................... 322
Section 60.7: Using Statements and Database Connections ............................................................................... 323
Section 60.8: Executing code in constraint context ............................................................................................... 325
Chapter 61: Using Directive .................................................................................................................................. 326
Section 61.1: Associate an Alias to Resolve Conflicts ............................................................................................. 326
Section 61.2: Using alias directives .......................................................................................................................... 326
Section 61.3: Access Static Members of a Class ..................................................................................................... 326
Section 61.4: Basic Usage ......................................................................................................................................... 327
Section 61.5: Reference a Namespace .................................................................................................................... 327
Section 61.6: Associate an Alias with a Namespace .............................................................................................. 327
Chapter 62: IDisposable interface ................................................................................................................... 329
Section 62.1: In a class that contains only managed resources ........................................................................... 329
Section 62.2: In a class with managed and unmanaged resources .................................................................... 329
Section 62.3: IDisposable, Dispose .......................................................................................................................... 330
Section 62.4: using keyword .................................................................................................................................... 330
Section 62.5: In an inherited class with managed resources ................................................................................ 331
Chapter 63: Reflection ........................................................................................................................................... 332
Section 63.1: Get the members of a type ................................................................................................................ 332
Section 63.2: Get a method and invoke it ............................................................................................................... 332
Section 63.3: Creating an instance of a Type ......................................................................................................... 333
Section 63.4: Get a Strongly-Typed Delegate to a Method or Property via Reflection .................................... 336
Section 63.5: Get a generic method and invoke it ................................................................................................. 337
Section 63.6: Get a [Link] ............................................................................................................................. 338
Section 63.7: Getting and setting properties .......................................................................................................... 338
Section 63.8: Create an instance of a Generic Type and invoke it's method ..................................................... 338
Section 63.9: Custom Attributes ............................................................................................................................... 339
Section 63.10: Instantiating classes that implement an interface (e.g. plugin activation) ................................. 340
Section 63.11: Get a Type by name with namespace ............................................................................................. 340
Section 63.12: Determining generic arguments of instances of generic types ................................................... 341
Section 63.13: Looping through all the properties of a class ................................................................................ 342
Chapter 64: IQueryable interface .................................................................................................................... 343
Section 64.1: Translating a LINQ query to a SQL query ........................................................................................ 343
Chapter 65: Linq to Objects ................................................................................................................................ 344
Section 65.1: Using LINQ to Objects in C# ............................................................................................................... 344
Chapter 66: LINQ Queries ..................................................................................................................................... 348
Section 66.1: Chaining methods ............................................................................................................................... 348
Section 66.2: First, FirstOrDefault, Last, LastOrDefault, Single, and SingleOrDefault ........................................ 349
Section 66.3: Except .................................................................................................................................................. 352
Section 66.4: SelectMany .......................................................................................................................................... 354
Section 66.5: Any ....................................................................................................................................................... 355
Section 66.6: JOINS ................................................................................................................................................... 355
Section 66.7: Skip and Take ..................................................................................................................................... 358
Section 66.8: Defining a variable inside a Linq query (let keyword) ................................................................... 358
Section 66.9: Zip ........................................................................................................................................................ 359
Section 66.10: Range and Repeat ............................................................................................................................ 359
Section 66.11: Basics .................................................................................................................................................. 360
Section 66.12: All ........................................................................................................................................................ 360
Section 66.13: Aggregate .......................................................................................................................................... 361
Section 66.14: Distinct ................................................................................................................................................ 362
Section 66.15: SelectMany: Flattening a sequence of sequences ......................................................................... 362
Section 66.16: GroupBy ............................................................................................................................................. 364
Section 66.17: Query collection by type / cast elements to type ......................................................................... 365
Section 66.18: Enumerating the Enumerable .......................................................................................................... 366
Section 66.19: Using Range with various Linq methods ........................................................................................ 367
Section 66.20: Where ................................................................................................................................................ 368
Section 66.21: Using SelectMany instead of nested loops .................................................................................... 368
Section 66.22: Contains ............................................................................................................................................ 368
Section 66.23: Joining multiple sequences ............................................................................................................. 370
Section 66.24: Joining on multiple keys .................................................................................................................. 372
Section 66.25: ToLookup .......................................................................................................................................... 372
Section 66.26: SkipWhile ........................................................................................................................................... 372
Section 66.27: Query Ordering - OrderBy() ThenBy() OrderByDescending() ThenByDescending() .............. 373
Section 66.28: Sum .................................................................................................................................................... 374
Section 66.29: GroupBy one or multiple fields ....................................................................................................... 374
Section 66.30: OrderBy ............................................................................................................................................. 375
Section 66.31: Any and First(OrDefault) - best practice ........................................................................................ 376
Section 66.32: GroupBy Sum and Count ................................................................................................................. 376
Section 66.33: SequenceEqual ................................................................................................................................. 377
Section 66.34: ElementAt and ElementAtOrDefault ............................................................................................... 377
Section 66.35: DefaultIfEmpty .................................................................................................................................. 378
Section 66.36: ToDictionary ...................................................................................................................................... 379
Section 66.37: Concat ................................................................................................................................................ 380
Section 66.38: Build your own Linq operators for IEnumerable<T> ..................................................................... 380
Section 66.39: Select - Transforming elements ..................................................................................................... 381
Section 66.40: OrderByDescending ......................................................................................................................... 382
Section 66.41: Union .................................................................................................................................................. 382
Section 66.42: GroupJoin with outer range variable ............................................................................................. 383
Section 66.43: Linq Quantifiers ................................................................................................................................. 383
Section 66.44: TakeWhile .......................................................................................................................................... 383
Section 66.45: Reverse .............................................................................................................................................. 384
Section 66.46: Count and LongCount ...................................................................................................................... 385
Section 66.47: Incrementally building a query ....................................................................................................... 385
Section 66.48: Select with Func<TSource, int, TResult> selector - Use to get ranking of elements ................. 387
Chapter 67: LINQ to XML ...................................................................................................................................... 389
Section 67.1: Read XML using LINQ to XML ............................................................................................................ 389
Chapter 68: Parallel LINQ (PLINQ) ................................................................................................................... 391
Section 68.1: Simple example ................................................................................................................................... 391
Section 68.2: WithDegreeOfParallelism .................................................................................................................. 391
Section 68.3: AsOrdered ........................................................................................................................................... 391
Section 68.4: AsUnordered ....................................................................................................................................... 391
Chapter 69: XmlDocument and the [Link] namespace ............................................................ 392
Section 69.1: XmlDocument vs XDocument (Example and comparison) ............................................................ 392
Section 69.2: Reading from XML document ........................................................................................................... 394
Section 69.3: Basic XML document interaction ...................................................................................................... 395
Chapter 70: XDocument and the [Link] namespace ...................................................... 396
Section 70.1: Generate an XML document .............................................................................................................. 396
Section 70.2: Generate an XML document using fluent syntax ............................................................................ 396
Section 70.3: Modify XML File ................................................................................................................................... 397
Chapter 71: C# 7.0 Features ................................................................................................................................ 399
Section 71.1: Language support for Tuples ............................................................................................................. 399
Section 71.2: Local functions .................................................................................................................................... 403
Section 71.3: out var declaration .............................................................................................................................. 404
Section 71.4: Pattern Matching ................................................................................................................................. 405
Section 71.5: Digit separators ................................................................................................................................... 407
Section 71.6: Binary literals ....................................................................................................................................... 407
Section 71.7: throw expressions ................................................................................................................................ 408
Section 71.8: Extended expression bodied members list ....................................................................................... 409
Section 71.9: ref return and ref local ....................................................................................................................... 410
Section 71.10: ValueTask<T> ..................................................................................................................................... 411
Chapter 72: C# 6.0 Features ............................................................................................................................... 413
Section 72.1: Exception filters ................................................................................................................................... 413
Section 72.2: String interpolation ............................................................................................................................. 417
Section 72.3: Auto-property initializers ................................................................................................................... 423
Section 72.4: Null propagation ................................................................................................................................. 426
Section 72.5: Expression-bodied function members ............................................................................................. 429
Section 72.6: Operator nameof ............................................................................................................................... 431
Section 72.7: Using static type ................................................................................................................................. 433
Section 72.8: Index initializers .................................................................................................................................. 433
Section 72.9: Improved overload resolution ........................................................................................................... 435
Section 72.10: Await in catch and finally ................................................................................................................. 436
Section 72.11: Minor changes and bugfixes ............................................................................................................. 437
Section 72.12: Using an extension method for collection initialization ................................................................ 437
Section 72.13: Disable Warnings Enhancements .................................................................................................... 438
Chapter 73: C# 5.0 Features ............................................................................................................................... 440
Section 73.1: Async & Await ...................................................................................................................................... 440
Section 73.2: Caller Information Attributes ............................................................................................................. 441
Chapter 74: C# 4.0 Features .............................................................................................................................. 442
Section 74.1: Optional parameters and named arguments .................................................................................. 442
Section 74.2: Variance .............................................................................................................................................. 443
Section 74.3: Dynamic member lookup .................................................................................................................. 443
Section 74.4: Optional ref keyword when using COM ........................................................................................... 444
Chapter 75: C# 3.0 Features ............................................................................................................................... 445
Section 75.1: Implicitly typed variables (var) .......................................................................................................... 445
Section 75.2: Language Integrated Queries (LINQ) .............................................................................................. 445
Section 75.3: Lambda expresions ............................................................................................................................ 446
Section 75.4: Anonymous types ............................................................................................................................... 446
Chapter 76: Exception Handling ........................................................................................................................ 448
Section 76.1: Creating Custom Exceptions .............................................................................................................. 448
Section 76.2: Finally block ........................................................................................................................................ 450
Section 76.3: Best Practices ...................................................................................................................................... 451
Section 76.4: Exception Anti-patterns ..................................................................................................................... 453
Section 76.5: Basic Exception Handling .................................................................................................................. 455
Section 76.6: Handling specific exception types .................................................................................................... 455
Section 76.7: Aggregate exceptions / multiple exceptions from one method ................................................... 456
Section 76.8: Throwing an exception ...................................................................................................................... 457
Section 76.9: Unhandled and Thread Exception .................................................................................................... 457
Section 76.10: Implementing IErrorHandler for WCF Services .............................................................................. 458
Section 76.11: Using the exception object ................................................................................................................ 460
Section 76.12: Nesting of Exceptions & try catch blocks ....................................................................................... 462
Chapter 77: NullReferenceException ............................................................................................................. 463
Section 77.1: NullReferenceException explained .................................................................................................... 463
Chapter 78: Handling FormatException when converting string to other types .................... 464
Section 78.1: Converting string to integer ............................................................................................................... 464
Chapter 79: Read & Understand Stacktraces ........................................................................................... 466
Section 79.1: Stack trace for a simple NullReferenceException in Windows Forms ........................................... 466
Chapter 80: Diagnostics ........................................................................................................................................ 468
Section 80.1: Redirecting log output with TraceListeners ..................................................................................... 468
Section 80.2: [Link] ................................................................................................................................. 468
Chapter 81: Overflow .............................................................................................................................................. 469
Section 81.1: Integer overflow ................................................................................................................................... 469
Section 81.2: Overflow during operation ................................................................................................................. 469
Section 81.3: Ordering matters ................................................................................................................................. 469
Chapter 82: Getting Started: Json with C# ................................................................................................. 470
Section 82.1: Simple Json Example .......................................................................................................................... 470
Section 82.2: First things First: Library to work with Json ..................................................................................... 470
Section 82.3: C# Implementation ............................................................................................................................ 470
Section 82.4: Serialization ......................................................................................................................................... 471
Section 82.5: Deserialization .................................................................................................................................... 471
Section 82.6: Serialization & De-Serialization Common Utilities function ........................................................... 471
Chapter 83: Using [Link] ................................................................................................................................... 473
Section 83.1: Using JsonConverter on simple values ............................................................................................ 473
Section 83.2: Collect all fields of JSON object ........................................................................................................ 475
Chapter 84: Lambda expressions .................................................................................................................... 477
Section 84.1: Lambda Expressions as Shorthand for Delegate Initialization ...................................................... 477
Section 84.2: Lambda Expression as an Event Handler ........................................................................................ 477
Section 84.3: Lambda Expressions with Multiple Parameters or No Parameters .............................................. 478
Section 84.4: Lambdas can be emitted both as `Func` and `Expression` ............................................................ 478
Section 84.5: Put Multiple Statements in a Statement Lambda ........................................................................... 478
Section 84.6: Lambdas for both `Func` and `Action` .............................................................................................. 479
Section 84.7: Using lambda syntax to create a closure ........................................................................................ 479
Section 84.8: Passing a Lambda Expression as a Parameter to a Method ........................................................ 479
Section 84.9: Basic lambda expressions ................................................................................................................. 479
Section 84.10: Basic lambda expressions with LINQ .............................................................................................. 480
Section 84.11: Lambda syntax with statement block body ................................................................................... 480
Section 84.12: Lambda expressions with [Link] ...................................................................... 480
Chapter 85: Generic Lambda Query Builder .............................................................................................. 481
Section 85.1: QueryFilter class .................................................................................................................................. 481
Section 85.2: GetExpression Method ....................................................................................................................... 481
Section 85.3: GetExpression Private overload ........................................................................................................ 482
Section 85.4: ConstantExpression Method ............................................................................................................. 483
Section 85.5: Usage ................................................................................................................................................... 484
Chapter 86: Properties ........................................................................................................................................... 485
Section 86.1: Auto-implemented properties ........................................................................................................... 485
Section 86.2: Default Values for Properties ............................................................................................................ 485
Section 86.3: Public Get ............................................................................................................................................. 486
Section 86.4: Public Set ............................................................................................................................................. 486
Section 86.5: Accessing Properties .......................................................................................................................... 486
Section 86.6: Read-only properties ......................................................................................................................... 488
Section 86.7: Various Properties in Context ............................................................................................................ 488
Chapter 87: Initializing Properties ................................................................................................................... 490
Section 87.1: C# 6.0: Initialize an Auto-Implemented Property ............................................................................. 490
Section 87.2: Initializing Property with a Backing Field ......................................................................................... 490
Section 87.3: Property Initialization during object instantiation ........................................................................... 490
Section 87.4: Initializing Property in Constructor ................................................................................................... 490
Chapter 88: INotifyPropertyChanged interface ...................................................................................... 491
Section 88.1: Implementing INotifyPropertyChanged in C# 6 .............................................................................. 491
Section 88.2: INotifyPropertyChanged With Generic Set Method ........................................................................ 492
Chapter 89: Events ................................................................................................................................................... 494
Section 89.1: Declaring and Raising Events ............................................................................................................ 494
Section 89.2: Event Properties ................................................................................................................................. 495
Section 89.3: Creating cancelable event ................................................................................................................. 496
Section 89.4: Standard Event Declaration .............................................................................................................. 497
Section 89.5: Anonymous Event Handler Declaration .......................................................................................... 498
Section 89.6: Non-Standard Event Declaration ..................................................................................................... 498
Section 89.7: Creating custom EventArgs containing additional data ................................................................ 499
Chapter 90: Expression Trees ............................................................................................................................ 501
Section 90.1: Create Expression Trees with a lambda expression ....................................................................... 501
Section 90.2: Creating Expression Trees by Using the API ................................................................................... 501
Section 90.3: Compiling Expression Trees .............................................................................................................. 501
Section 90.4: Parsing Expression Trees .................................................................................................................. 502
Section 90.5: Expression Tree Basic ........................................................................................................................ 502
Section 90.6: Examining the Structure of an Expression using Visitor ................................................................. 503
Section 90.7: Understanding the expressions API .................................................................................................. 503
Chapter 91: Overload Resolution ...................................................................................................................... 505
Section 91.1: Basic Overloading Example ................................................................................................................ 505
Section 91.2: "params" is not expanded, unless necessary .................................................................................. 505
Section 91.3: Passing null as one of the arguments ............................................................................................... 506
Chapter 92: BindingList<T> .................................................................................................................................. 507
Section 92.1: Add item to list ..................................................................................................................................... 507
Section 92.2: Avoiding N*2 iteration ........................................................................................................................ 507
Chapter 93: Preprocessor directives .............................................................................................................. 508
Section 93.1: Conditional Expressions ...................................................................................................................... 508
Section 93.2: Other Compiler Instructions .............................................................................................................. 508
Section 93.3: Defining and Undefining Symbols .................................................................................................... 509
Section 93.4: Region Blocks ..................................................................................................................................... 510
Section 93.5: Disabling and Restoring Compiler Warnings .................................................................................. 510
Section 93.6: Generating Compiler Warnings and Errors ..................................................................................... 510
Section 93.7: Custom Preprocessors at project level ............................................................................................ 511
Section 93.8: Using the Conditional attribute ......................................................................................................... 511
Chapter 94: Structs ................................................................................................................................................. 513
Section 94.1: Declaring a struct ................................................................................................................................ 513
Section 94.2: Struct usage ........................................................................................................................................ 514
Section 94.3: Structs are copied on assignment .................................................................................................... 515
Section 94.4: Struct implementing interface .......................................................................................................... 515
Chapter 95: Attributes ........................................................................................................................................... 516
Section 95.1: Creating a custom attribute ............................................................................................................... 516
Section 95.2: Reading an attribute .......................................................................................................................... 516
Section 95.3: Using an attribute ............................................................................................................................... 517
Section 95.4: DebuggerDisplay Attribute ............................................................................................................... 517
Section 95.5: Caller info attributes ........................................................................................................................... 518
Section 95.6: Obsolete Attribute .............................................................................................................................. 519
Section 95.7: Reading an attribute from interface ................................................................................................ 519
Chapter 96: Delegates ........................................................................................................................................... 521
Section 96.1: Declaring a delegate type .................................................................................................................. 521
Section 96.2: The Func<T, TResult>, Action<T> and Predicate<T> delegate types ............................................ 522
Section 96.3: Combine Delegates (Multicast Delegates) ...................................................................................... 523
Section 96.4: Safe invoke multicast delegate ......................................................................................................... 524
Section 96.5: Delegate Equality ............................................................................................................................... 525
Section 96.6: Underlying references of named method delegates ..................................................................... 525
Section 96.7: Assigning a named method to a delegate ...................................................................................... 526
Section 96.8: Assigning to a delegate by lambda ................................................................................................. 527
Section 96.9: Encapsulating transformations in funcs .......................................................................................... 527
Section 96.10: Passing delegates as parameters ................................................................................................... 527
Section 96.11: Closure inside a delegate .................................................................................................................. 528
Chapter 97: File and Stream I/O ...................................................................................................................... 529
Section 97.1: Reading from a file using the [Link] class .......................................................................... 529
Section 97.2: Lazily reading a file line-by-line via an IEnumerable ..................................................................... 529
Section 97.3: Async write text to a file using StreamWriter .................................................................................. 529
Section 97.4: Copy File .............................................................................................................................................. 529
Section 97.5: Writing lines to a file using the [Link] class ...................................................... 530
Section 97.6: Writing to a file using the [Link] class ................................................................................ 530
Section 97.7: Create File ........................................................................................................................................... 531
Section 97.8: Move File ............................................................................................................................................. 531
Section 97.9: Delete File ............................................................................................................................................ 532
Section 97.10: Files and Directories ......................................................................................................................... 532
Chapter 98: Networking ........................................................................................................................................ 533
Section 98.1: Basic TCP Communication Client ...................................................................................................... 533
Section 98.2: Download a file from a web server .................................................................................................. 533
Section 98.3: Async TCP Client ................................................................................................................................. 534
Section 98.4: Basic UDP Client ................................................................................................................................. 535
Chapter 99: Performing HTTP requests ........................................................................................................ 536
Section 99.1: Creating and sending an HTTP POST request ................................................................................. 536
Section 99.2: Creating and sending an HTTP GET request .................................................................................. 536
Section 99.3: Error handling of specific HTTP response codes (such as 404 Not Found) ................................ 537
Section 99.4: Retrieve HTML for Web Page (Simple) ............................................................................................ 537
Section 99.5: Sending asynchronous HTTP POST request with JSON body ...................................................... 537
Chapter 100: Reading and writing .zip files ................................................................................................. 539
Section 100.1: Writing to a zip file ............................................................................................................................. 539
Section 100.2: Writing Zip Files in-memory ............................................................................................................ 539
Section 100.3: Get files from a Zip file ..................................................................................................................... 539
Section 100.4: The following example shows how to open a zip archive and extract all .txt files to a folder
............................................................................................................................................................................. 540
Chapter 101: FileSystemWatcher ...................................................................................................................... 541
Section 101.1: IsFileReady .......................................................................................................................................... 541
Section 101.2: Basic FileWatcher .............................................................................................................................. 541
Chapter 102: Access network shared folder with username and password .............................. 543
Section 102.1: Code to access network shared file ................................................................................................ 543
Chapter 103: Asynchronous Socket ................................................................................................................. 545
Section 103.1: Asynchronous Socket (Client / Server) example ........................................................................... 545
Chapter 104: Action Filters .................................................................................................................................. 552
Section 104.1: Custom Action Filters ......................................................................................................................... 552
Chapter 105: Polymorphism ................................................................................................................................ 553
Section 105.1: Types of Polymorphism .................................................................................................................... 553
Section 105.2: Another Polymorphism Example ..................................................................................................... 554
Chapter 106: Immutability .................................................................................................................................... 557
Section 106.1: [Link] class ............................................................................................................................ 557
Section 106.2: Strings and immutability .................................................................................................................. 557
Chapter 107: Indexer ............................................................................................................................................... 558
Section 107.1: A simple indexer ................................................................................................................................. 558
Section 107.2: Overloading the indexer to create a SparseArray ........................................................................ 558
Section 107.3: Indexer with 2 arguments and interface ........................................................................................ 559
Chapter 108: Checked and Unchecked .......................................................................................................... 560
Section 108.1: Checked and Unchecked .................................................................................................................. 560
Section 108.2: Checked and Unchecked as a scope ............................................................................................. 560
Chapter 109: Stream ............................................................................................................................................... 561
Section 109.1: Using Streams .................................................................................................................................... 561
Chapter 110: Timers ................................................................................................................................................. 563
Section 110.1: Multithreaded Timers ......................................................................................................................... 563
Section 110.2: Creating an Instance of a Timer ...................................................................................................... 564
Section 110.3: Assigning the "Tick" event handler to a Timer ............................................................................... 565
Section 110.4: Example: Using a Timer to perform a simple countdown ............................................................. 565
Chapter 111: Stopwatches ..................................................................................................................................... 567
Section 111.1: IsHighResolution .................................................................................................................................. 567
Section 111.2: Creating an Instance of a Stopwatch ............................................................................................... 567
Chapter 112: Threading .......................................................................................................................................... 569
Section 112.1: Avoiding Reading and Writing Data Simultaneously ..................................................................... 569
Section 112.2: Creating and Starting a Second Thread ......................................................................................... 570
Section 112.3: [Link] Loop ........................................................................................................................ 570
Section 112.4: Deadlocks (hold resource and wait) ................................................................................................ 571
Section 112.5: Simple Complete Threading Demo .................................................................................................. 573
Section 112.6: Creating One Thread Per Processor ................................................................................................ 574
Section 112.7: Simple Complete Threading Demo using Tasks ............................................................................ 574
Section 112.8: Deadlocks (two threads waiting on each other) ............................................................................ 575
Section 112.9: Explicit Task Parallism ....................................................................................................................... 576
Section 112.10: Implicit Task Parallelism .................................................................................................................. 576
Section 112.11: Starting a thread with parameters .................................................................................................. 577
Chapter 113: Async/await, Backgroundworker, Task and Thread Examples ............................ 578
Section 113.1: [Link] Configure Await ................................................................................................................... 578
Section 113.2: Task "run and forget" extension ...................................................................................................... 580
Section 113.3: Async/await ........................................................................................................................................ 580
Section 113.4: BackgroundWorker ........................................................................................................................... 581
Section 113.5: Task ..................................................................................................................................................... 582
Section 113.6: Thread ................................................................................................................................................. 583
Chapter 114: Async-Await ..................................................................................................................................... 584
Section 114.1: Await operator and async keyword ................................................................................................. 584
Section 114.2: Concurrent calls ................................................................................................................................. 585
Section 114.3: Try/Catch/Finally .............................................................................................................................. 586
Section 114.4: Returning a Task without await ....................................................................................................... 587
Section 114.5: Async/await will only improve performance if it allows the machine to do additional work
............................................................................................................................................................................. 587
Section 114.6: [Link]fig setup to target 4.5 for correct async behaviour ........................................................ 588
Section 114.7: Simple consecutive calls .................................................................................................................... 588
Section 114.8: Blocking on async code can cause deadlocks ............................................................................... 589
Chapter 115: Synchronization Context in Async-Await ........................................................................... 591
Section 115.1: Pseudocode for async/await keywords .......................................................................................... 591
Section 115.2: Disabling synchronization context ................................................................................................... 591
Section 115.3: Why SynchronizationContext is so important? ............................................................................... 592
Chapter 116: BackgroundWorker ...................................................................................................................... 593
Section 116.1: Using a BackgroundWorker to complete a task ............................................................................. 593
Section 116.2: Assigning Event Handlers to a BackgroundWorker ...................................................................... 594
Section 116.3: Creating a new BackgroundWorker instance ................................................................................. 595
Section 116.4: Assigning Properties to a BackgroundWorker ............................................................................... 595
Chapter 117: Task Parallel Library ................................................................................................................... 596
Section 117.1: [Link] .................................................................................................................................. 596
Section 117.2: [Link] .......................................................................................................................................... 596
Section 117.3: [Link] .................................................................................................................................... 597
Chapter 118: Making a variable thread safe ............................................................................................... 598
Section 118.1: Controlling access to a variable in a [Link] loop ................................................................... 598
Chapter 119: Lock Statement .............................................................................................................................. 599
Section 119.1: Throwing exception in a lock statement .......................................................................................... 599
Section 119.2: Simple usage ...................................................................................................................................... 599
Section 119.3: Return in a lock statement ................................................................................................................ 600
Section 119.4: Anti-Patterns and gotchas ................................................................................................................ 600
Section 119.5: Using instances of Object for lock ................................................................................................... 604
Chapter 120: Yield Keyword ................................................................................................................................. 605
Section 120.1: Simple Usage ..................................................................................................................................... 605
Section 120.2: Correctly checking arguments ........................................................................................................ 605
Section 120.3: Early Termination ............................................................................................................................. 606
Section 120.4: More Pertinent Usage ....................................................................................................................... 607
Section 120.5: Lazy Evaluation ................................................................................................................................. 608
Section 120.6: Try...finally .......................................................................................................................................... 609
Section 120.7: Eager evaluation ............................................................................................................................... 610
Section 120.8: Using yield to create an IEnumerator<T> when implementing IEnumerable<T> ...................... 610
Section 120.9: Lazy Evaluation Example: Fibonacci Numbers .............................................................................. 611
Section 120.10: The dierence between break and yield break ........................................................................... 612
Section 120.11: Return another Enumerable within a method returning Enumerable ........................................ 613
Chapter 121: Task Parallel Library (TPL) Dataflow Constructs ......................................................... 614
Section 121.1: ActionBlock<T> ................................................................................................................................... 614
Section 121.2: BroadcastBlock<T> ............................................................................................................................ 614
Section 121.3: BuerBlock<T> ................................................................................................................................... 615
Section 121.4: JoinBlock<T1, T2,…> ............................................................................................................................ 616
Section 121.5: WriteOnceBlock<T> ........................................................................................................................... 617
Section 121.6: BatchedJoinBlock<T1, T2,…> .............................................................................................................. 618
Section 121.7: TransformBlock<TInput, TOutput> .................................................................................................. 619
Section 121.8: TransformManyBlock<TInput, TOutput> ......................................................................................... 619
Section 121.9: BatchBlock<T> ................................................................................................................................... 620
Chapter 122: Functional Programming .......................................................................................................... 622
Section 122.1: Func and Action ................................................................................................................................. 622
Section 122.2: Higher-Order Functions .................................................................................................................... 622
Section 122.3: Avoid Null References ....................................................................................................................... 622
Section 122.4: Immutability ....................................................................................................................................... 624
Section 122.5: Immutable collections ...................................................................................................................... 625
Chapter 123: Func delegates ............................................................................................................................... 626
Section 123.1: Without parameters .......................................................................................................................... 626
Section 123.2: With multiple variables ..................................................................................................................... 626
Section 123.3: Lambda & anonymous methods ..................................................................................................... 627
Section 123.4: Covariant & Contravariant Type Parameters ................................................................................ 627
Chapter 124: Function with multiple return values ................................................................................. 629
Section 124.1: "anonymous object" + "dynamic keyword" solution ..................................................................... 629
Section 124.2: Tuple solution .................................................................................................................................... 629
Section 124.3: Ref and Out Parameters .................................................................................................................. 629
Chapter 125: Binary Serialization ..................................................................................................................... 631
Section 125.1: Controlling serialization behavior with attributes ........................................................................... 631
Section 125.2: Serialization Binder ........................................................................................................................... 631
Section 125.3: Some gotchas in backward compatibility ...................................................................................... 633
Section 125.4: Making an object serializable .......................................................................................................... 635
Section 125.5: Serialization surrogates (Implementing ISerializationSurrogate) ................................................ 636
Section 125.6: Adding more control by implementing ISerializable ..................................................................... 638
Chapter 126: ICloneable ......................................................................................................................................... 640
Section 126.1: Implementing ICloneable in a class ................................................................................................. 640
Section 126.2: Implementing ICloneable in a struct ............................................................................................... 640
Chapter 127: IComparable .................................................................................................................................... 642
Section 127.1: Sort versions ....................................................................................................................................... 642
Chapter 128: Accessing Databases .................................................................................................................. 644
Section 128.1: Connection Strings ............................................................................................................................. 644
Section 128.2: Entity Framework Connections ....................................................................................................... 644
Section 128.3: [Link] Connections ...................................................................................................................... 645
Chapter 129: Using SQLite in C# ........................................................................................................................ 648
Section 129.1: Creating simple CRUD using SQLite in C# ...................................................................................... 648
Section 129.2: Executing Query ................................................................................................................................ 651
Chapter 130: Caching .............................................................................................................................................. 653
Section 130.1: MemoryCache .................................................................................................................................... 653
Chapter 131: Code Contracts ............................................................................................................................... 654
Section 131.1: Postconditions ..................................................................................................................................... 654
Section 131.2: Invariants ............................................................................................................................................ 654
Section 131.3: Defining Contracts on Interface ....................................................................................................... 655
Section 131.4: Preconditions ...................................................................................................................................... 656
Chapter 132: Code Contracts and Assertions ............................................................................................. 658
Section 132.1: Assertions to check logic should always be true ............................................................................ 658
Chapter 133: Structural Design Patterns ...................................................................................................... 659
Section 133.1: Adapter Design Pattern ..................................................................................................................... 659
Chapter 134: Creational Design Patterns ..................................................................................................... 663
Section 134.1: Singleton Pattern ................................................................................................................................ 663
Section 134.2: Factory Method pattern ................................................................................................................... 664
Section 134.3: Abstract Factory Pattern .................................................................................................................. 667
Section 134.4: Builder Pattern ................................................................................................................................... 669
Section 134.5: Prototype Pattern .............................................................................................................................. 673
Chapter 135: Implementing Decorator Design Pattern ........................................................................ 676
Section 135.1: Simulating cafeteria ........................................................................................................................... 676
Chapter 136: Implementing Flyweight Design Pattern .......................................................................... 678
Section 136.1: Implementing map in RPG game ..................................................................................................... 678
Chapter 137: [Link] ....................................................................................... 681
Section 137.1: Invoke simple synchronous pipeline ................................................................................................ 681
Chapter 138: [Link] .............................................. 682
Section 138.1: Authenticated SSL LDAP connection, SSL cert does not match reverse DNS ............................. 682
Section 138.2: Super Simple anonymous LDAP ...................................................................................................... 683
Chapter 139: C# Authentication handler ....................................................................................................... 684
Section 139.1: Authentication handler ...................................................................................................................... 684
Chapter 140: Pointers ............................................................................................................................................. 686
Section 140.1: Pointers for array access .................................................................................................................. 686
Section 140.2: Pointer arithmetic ............................................................................................................................. 686
Section 140.3: The asterisk is part of the type ........................................................................................................ 687
Section 140.4: void* .................................................................................................................................................... 687
Section 140.5: Member access using -> .................................................................................................................. 687
Section 140.6: Generic pointers ................................................................................................................................ 688
Chapter 141: Pointers & Unsafe Code ............................................................................................................. 689
Section 141.1: Introduction to unsafe code .............................................................................................................. 689
Section 141.2: Accessing Array Elements Using a Pointer ..................................................................................... 690
Section 141.3: Compiling Unsafe Code ..................................................................................................................... 690
Section 141.4: Retrieving the Data Value Using a Pointer ..................................................................................... 691
Section 141.5: Passing Pointers as Parameters to Methods .................................................................................. 692
Chapter 142: How to use C# Structs to create a Union type (Similar to C Unions) ............... 693
Section 142.1: C-Style Unions in C# .......................................................................................................................... 693
Section 142.2: Union Types in C# can also contain Struct fields .......................................................................... 694
Chapter 143: Reactive Extensions (Rx) .......................................................................................................... 696
Section 143.1: Observing TextChanged event on a TextBox ................................................................................. 696
Section 143.2: Streaming Data from Database with Observable ......................................................................... 696
Chapter 144: [Link] Examples ..................................................................................................... 697
Section 144.1: Global and local AssemblyInfo ......................................................................................................... 697
Section 144.2: [AssemblyVersion] ............................................................................................................................ 697
Section 144.3: Automated versioning ...................................................................................................................... 698
Section 144.4: Common fields .................................................................................................................................. 698
Section 144.5: [AssemblyTitle] ................................................................................................................................. 698
Section 144.6: [AssemblyProduct] ........................................................................................................................... 698
Section 144.7: [InternalsVisibleTo] ........................................................................................................................... 698
Section 144.8: [AssemblyConfiguration] ................................................................................................................. 699
Section 144.9: [AssemblyKeyFile] ............................................................................................................................ 699
Section 144.10: Reading Assembly Attributes ......................................................................................................... 699
Chapter 145: Creating a Console Application using a Plain-Text Editor and the C#
Compiler ([Link]) .................................................................................................................................................... 701
Section 145.1: Creating a Console application using a Plain-Text Editor and the C# Compiler ........................ 701
Chapter 146: CLSCompliantAttribute ............................................................................................................. 703
Section 146.1: Access Modifier to which CLS rules apply ....................................................................................... 703
Section 146.2: Violation of CLS rule: Unsigned types / sbyte ............................................................................... 703
Section 146.3: Violation of CLS rule: Same naming ................................................................................................ 704
Section 146.4: Violation of CLS rule: Identifier _ ..................................................................................................... 705
Section 146.5: Violation of CLS rule: Inherit from non CLSComplaint class ......................................................... 705
Chapter 147: ObservableCollection<T> ......................................................................................................... 706
Section 147.1: Initialize ObservableCollection<T> ................................................................................................... 706
Chapter 148: Hash Functions .............................................................................................................................. 707
Section 148.1: PBKDF2 for Password Hashing ........................................................................................................ 707
Section 148.2: Complete Password Hashing Solution using Pbkdf2 .................................................................... 707
Section 148.3: MD5 ..................................................................................................................................................... 711
Section 148.4: SHA1 .................................................................................................................................................... 711
Section 148.5: SHA256 ............................................................................................................................................... 712
Section 148.6: SHA384 ............................................................................................................................................... 712
Section 148.7: SHA512 ................................................................................................................................................ 713
Chapter 149: Generating Random Numbers in C# .................................................................................. 714
Section 149.1: Generate a random int ...................................................................................................................... 714
Section 149.2: Generate a random int in a given range ........................................................................................ 714
Section 149.3: Generating the same sequence of random numbers over and over again ............................... 714
Section 149.4: Create multiple random class with dierent seeds simultaneously ............................................ 715
Section 149.5: Generate a Random double ............................................................................................................ 715
Section 149.6: Generate a random character ........................................................................................................ 715
Section 149.7: Generate a number that is a percentage of a max value ............................................................ 715
Chapter 150: Cryptography ([Link]) ......................................................... 717
Section 150.1: Modern Examples of Symmetric Authenticated Encryption of a string ...................................... 717
Section 150.2: Introduction to Symmetric and Asymmetric Encryption .............................................................. 728
Section 150.3: Simple Symmetric File Encryption ................................................................................................... 729
Section 150.4: Cryptographically Secure Random Data ....................................................................................... 730
Section 150.5: Password Hashing ............................................................................................................................ 731
Section 150.6: Fast Asymmetric File Encryption ..................................................................................................... 731
Chapter 151: [Link] Identity ............................................................................................................................ 736
Section 151.1: How to implement password reset token in [Link] identity using user manager ..................... 736
Chapter 152: Unsafe Code in .NET .................................................................................................................... 739
Section 152.1: Using unsafe with arrays .................................................................................................................. 739
Section 152.2: Using unsafe with strings ................................................................................................................. 739
Section 152.3: Unsafe Array Index ........................................................................................................................... 740
Chapter 153: C# Script ............................................................................................................................................ 741
Section 153.1: Simple code evaluation ..................................................................................................................... 741
Chapter 154: Runtime Compile .......................................................................................................................... 742
Section 154.1: RoslynScript ........................................................................................................................................ 742
Section 154.2: CSharpCodeProvider ........................................................................................................................ 742
Chapter 155: Interoperability ............................................................................................................................. 743
Section 155.1: Import function from unmanaged C++ DLL .................................................................................... 743
Section 155.2: Calling conventions ........................................................................................................................... 743
Section 155.3: C++ name mangling .......................................................................................................................... 744
Section 155.4: Dynamic loading and unloading of unmanaged DLLs ................................................................. 744
Section 155.5: Reading structures with Marshal ..................................................................................................... 745
Section 155.6: Dealing with Win32 Errors ................................................................................................................ 746
Section 155.7: Pinned Object .................................................................................................................................... 747
Section 155.8: Simple code to expose class for com ............................................................................................. 748
Chapter 156: .NET Compiler Platform (Roslyn) ......................................................................................... 750
Section 156.1: Semantic model ................................................................................................................................. 750
Section 156.2: Syntax tree ......................................................................................................................................... 750
Section 156.3: Create workspace from MSBuild project ........................................................................................ 751
Chapter 157: ILGenerator ..................................................................................................................................... 752
Section 157.1: Creates a DynamicAssembly that contains a UnixTimestamp helper method .......................... 752
Section 157.2: Create method override ................................................................................................................... 753
Chapter 158: T4 Code Generation .................................................................................................................... 755
Section 158.1: Runtime Code Generation ................................................................................................................. 755
Chapter 159: Creating Own MessageBox in Windows Form Application ...................................... 756
Section 159.1: How to use own created MessageBox control in another Windows Form application ............. 756
Section 159.2: Creating Own MessageBox Control ................................................................................................ 756
Chapter 160: Including Font Resources ......................................................................................................... 759
Section 160.1: Instantiate 'Fontfamily' from Resources ......................................................................................... 759
Section 160.2: Integration method ........................................................................................................................... 759
Section 160.3: Usage with a 'Button' ........................................................................................................................ 759
Chapter 161: Import Google Contacts ............................................................................................................ 761
Section 161.1: Requirements ...................................................................................................................................... 761
Section 161.2: Source code in the controller ............................................................................................................ 761
Section 161.3: Source code in the view ..................................................................................................................... 764
Chapter 162: Garbage Collector in .Net ......................................................................................................... 765
Section 162.1: Weak References ............................................................................................................................... 765
Section 162.2: Large Object Heap compaction ...................................................................................................... 766
Chapter 163: [Link] ......................................................................................... 767
Section 163.1: Retrieve Specified User's Out of Oce Settings ............................................................................. 767
Section 163.2: Update Specific User's Out of Oce Settings ................................................................................ 767
Chapter 164: Windows Communication Foundation ............................................................................... 769
Section 164.1: Getting started sample ...................................................................................................................... 769
Credits ............................................................................................................................................................................ 771
You may also like ...................................................................................................................................................... 785
About
Please feel free to share this PDF with anyone for free,
latest version of this book can be downloaded from:
[Link]
This is an unofficial free book created for educational purposes and is not
affiliated with official C# group(s) or company(s) nor Stack Overflow. All
trademarks and registered trademarks are the property of their respective
company owners
6. In the toolbar, click Debug -> Start Debugging or hit F5 or ctrl + F5 (running without debugger) to run the
program.
Explanation
class Program is a class declaration. The class Program contains the data and method definitions that your
program uses. Classes generally contain multiple methods. Methods define the behavior of the class.
However, the Program class has only one method: Main.
static void Main() defines the Main method, which is the entry point for all C# programs. The Main method
states what the class does when executed. Only one Main method is allowed per class.
[Link]("Hello, world!"); method prints a given data (in this example, Hello,
world!) as an output in the console window.
To compile via command line use either MSBuild or [Link] (the C# compiler), both part of the Microsoft Build Tools
package.
To compile this example, run the following command in the same directory where [Link] is located:
%WINDIR%\\[Link]\\Framework64\\v4.0.30319\\[Link] [Link]
It can also be possible that you have two main methods inside one application. In this case, you have to tell the
compiler which main method to execute by typing the following command in the console.(suppose Class ClassA
also has a main method in the same [Link] file in HelloWorld namespace)
Note: This is the path where .NET framework v4.0 is located in general. Change the path according to your .NET version.
In addition, the directory might be framework instead of framework64 if you're using the 32-bit .NET Framework. From
the Windows Command Prompt, you can list all the [Link] Framework paths by running the following commands (the first
for 32-bit Frameworks):
There should now be an executable file named [Link] in the same directory. To execute the program
from the command prompt, simply type the executable's name and hit Enter as follows:
[Link]
Hello, world!
6. Project created. The newly created project will look similar to:
(Always use descriptive names for projects so that they can easily be distinguished from other projects. It is
recommended not to use spaces in project or class name.)
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
}
}
}
Add the following two lines to the public static void Main(string[] args) object in [Link]: (make
sure it's inside the braces)
[Link]("Hello world!");
[Link]();
Why [Link]()? The first line prints out the text "Hello world!" to the console, and the second line
waits for a single character to be entered; in effect, this causes the program to pause execution so that you're
able to see the output while debugging. Without [Link]();, when you start debugging the application
it will just print "Hello world!" to the console and then immediately close. Your code window should now look
like the following:
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
[Link]("Hello world!");
[Link]();
}
}
}
8. Debug your program. Press the Start Button on the toolbar near the top of the window or press
F5 on your keyboard to run your application. If the button is not present, you can run the program from
the top menu: Debug → Start Debugging. The program will compile and then open a console window. It
should look similar to the following screenshot:
Windows
OSX
Linux
Docker
After the installation has completed, open a command prompt, or terminal window.
1. Create a new directory with mkdir hello_world and change into the newly created directory with cd
hello_world.
hello_world.csproj
<Project Sdk="[Link]">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
</Project>
[Link]
using System;
4. Optional Build the application with dotnet build for Debug or dotnet build -c Release for Release. dotnet
run will also run the compiler and throw build errors, if any are found.
5. Run the application with dotnet run for Debug or dotnet run
.\bin\Release\netcoreapp1.1\hello_world.dll for Release.
After installation is done, create a text file, name it [Link] and copy the following content into it:
If you are using Windows, run the Mono Command Prompt which is included in the Mono installation and ensures
that the necessary environment variables are set. If on Mac or Linux, open a new terminal.
To compile the newly created file, run the following command in the directory containing [Link]:
mono [Link]
Hello, world!
Press any key to exit..
1. Install LinqPad
6. Now that you have created your first .Net program, go and check out the samples included in LinqPad via the
"Samples" browser. There are many great examples that will show you many different features of the .Net
languages.
Notes:
1. If you click on "IL", you can inspect the IL code that your .net code generates. This is a great learning tool.
namespace FirstCsharp
{
public class MainClass
{
public static void Main(string[] args)
{
[Link]("Hello World!");
[Link]();
}
}
}
9. To run the code, press F5 or click the Play Button as shown below:
uint ui = 5U;
int i = 5;
sbyte sb = 127;
decimal m = 30.5M;
double d = 30.5D;
float f = 30.5F;
long l = 5L;
ulong ul = 5UL;
String literals may contain escape sequences. See String Escape Sequences
Additionally, C# supports verbatim string literals (See Verbatim Strings). These are defined by wrapping the value
with double-quotes ", and prepending it with @. Escape sequences are ignored in verbatim string literals, and all
whitespace characters are included:
char c = 'h';
Character literals may contain escape sequences. See String Escape Sequences
A character literal must be exactly one character long (after all escape sequences have been evaluated). Empty
character literals are not valid. The default character (returned by default(char) or new char()) is '\0', or the
NULL character (not to be confused with the null literal and null references).
byte b = 127;
short s = 127;
ushort us = 127;
bool b = true;
In C#, an operator is a program element that is applied to one or more operands in an expression or statement.
Operators that take one operand, such as the increment operator (++) or new, are referred to as unary operators.
Operators that take two operands, such as arithmetic operators (+,-,*,/), are referred to as binary operators. One
operator, the conditional operator (?:), takes three operands and is the sole ternary operator in C#.
And we want to add the option to use the + operator for this class. i.e.:
We will need to overload the + operator for the class. This is done using a static function and the operator keyword:
Operators such as +, -, *, / can all be overloaded. This also includes Operators that don't return the same type (for
example, == and != can be overloaded, despite returning booleans) The rule below relating to pairs is also enforced
here.
Comparison operators have to be overloaded in pairs (e.g. if < is overloaded, > also needs to be overloaded).
A full list of overloadable operators (as well as non-overloadable operators and the restrictions placed on some
overloadable operators) can be seen at MSDN - Overloadable Operators (C# Programming Guide).
overloading of operator is was introduced with the pattern matching mechanism of C# 7.0. For details see Pattern
Matching
When overriding Equals, GetHashCode must also be overridden. When implementing Equals, there are many
special cases: comparing to objects of a different type, comparing to self etc.
When NOT overridden Equals method and == operator behave differently for classes and structs. For classes just
references are compared, and for structs values of properties are compared via reflection what can negatively
affect performance. == can not be used for comparing structs unless it is overridden.
Unlike Java, the equality comparison operator works natively with strings.
The equality comparison operator will work with operands of differing types if an implicit cast exists from one to
the other. If no suitable implicit cast exists, you may call an explicit cast or use a method to convert to a compatible
type.
1 == 1.0 // Returns true because there is an implicit cast from int to double.
new Object() == 1.0 // Will not compile.
Unlike Visual [Link], the equality comparison operator is not the same as the equality assignment operator.
For value types, the operator returns true if both operands are equal in value.
For reference types, the operator returns true if both operands are equal in reference (not value). An exception is
that string objects will be compared with value equality.
Not Equals
This operator effectively returns the opposite result to that of the equals (==) operator
Greater Than
Checks whether the first operand is greater than the second operand.
var x = 10;
var y = 15;
x > y //Returns false.
y > x //Returns true.
Less Than
Checks whether the first operand is less than the second operand.
var x = 12;
var y = 22;
x < y //Returns true.
y < x //Returns false.
Checks whether the first operand is greater than equal to the second operand.
Checks whether the first operand is less than equal to the second operand.
The method cannot take any more arguments, nor can it be an instance method. It can, however, access any private
members of type it is defined within.
The cast operators can work both ways, going from your type and going to your type:
Finally, the as keyword, which can be involved in casting within a type hierarchy, is not valid in this situation. Even
after defining either an explicit or implicit cast, you cannot do:
It means that, if you are using && operator as firstCondition && secondCondition it will evaluate secondCondition only
when firstCondition is true and ofcource the overall result will be true only if both of firstOperand and secondOperand
are evaluated to true. This is useful in many scenarios, for example imagine that you want to check whereas your
list has more than three elements but you also have to check if list has been initialized to not run into
NullReferenceException. You can achieve this as below:
Logical AND
&& is the short-circuiting counterpart of the standard boolean AND (&) operator.
var x = true;
var y = false;
Logical OR
var x = true;
var y = false;
Example usage
Syntax:
Example:
The ternary operator is right-associative which allows for compound ternary expressions to be used. This is done by
adding additional ternary equations in either the true or false position of a parent ternary equation. Care should be
taken to ensure readability, but this can be useful shorthand in some circumstances.
In this example, a compound ternary operation evaluates a clamp function and returns the current value if it's
within the range, the min value if it's below the range, or the max value if it's above the range.
// This is evaluated from left to right and can be more easily seen with parenthesis:
a ? (b ? x : y) : z
When writing compound ternary statements, it's common to use parenthesis or indentation to improve readability.
The types of expression_if_true and expression_if_false must be identical or there must be an implicit conversion from
one to the other.
condition ? 3 : "Not three"; // Doesn't compile because `int` and `string` lack an implicit
conversion.
condition ? [Link]() : "Not three"; // OK because both possible outputs are strings.
condition ? 3 : 3.5; // OK because there is an implicit conversion from `int` to `double`. The
ternary operator will return a `double`.
condition ? 3.5 : 3; // OK because there is an implicit conversion from `int` to `double`. The
ternary operator will return a `double`.
condition ? new SportsCar() : new Car(); // OK because there is an implicit conversion from
`SportsCar` to `Car`. The ternary operator will return a reference of type `Car`.
condition ? new Car() : new SportsCar(); // OK because there is an implicit conversion from
`SportsCar` to `Car`. The ternary operator will return a reference of type `Car`.
condition ? new SportsCar() : new SUV(); // Doesn't compile because there is no implicit conversion
from `SportsCar` to SUV or `SUV` to `SportsCar`. The compiler is not smart enough to realize that
both of them have an implicit conversion to `Car`.
condition ? new SportsCar() as Car : new SUV() as Car; // OK because both expressions evaluate to a
reference of type `Car`. The ternary operator will return a reference of type `Car`.
Introduced in C# 6.0, the Null Conditional Operator ?. will immediately return null if the expression on its left-
hand side evaluates to null, instead of throwing a NullReferenceException. If its left-hand side evaluates to a non-
null value, it is treated just like a normal . operator. Note that because it might return null, its return type is
always a nullable type. That means that for a struct or primitive type, it is wrapped into a Nullable<T>.
This comes handy when firing events. Normally you would have to wrap the event call in an if statement checking
for null and raise the event afterwards, which introduces the possibility of a race condition. Using the Null
conditional operator this can be fixed in the following way:
This operator returns true when one, but only one, of the supplied bools are true.
The built-in primitive data types, such as char, int, and float, as well as user-defined types declared with struct,
or enum. Their default value is new T() :
default(int) // 0
default(DateTime) // 0001-01-01 12:00:00 AM
default(char) // '\0' This is the "null character", not a zero or a line break.
default(Guid) // 00000000-0000-0000-0000-000000000000
default(MyStruct) // new MyStruct()
// Note: default of an enum is 0, and not the first *key* in that enum
// so it could potentially fail the [Link] test
default(MyEnum) // (MyEnum)0
Any class, interface, array or delegate type. Their default value is null :
default(object) // null
default(string) // null
default(MyClass) // null
default(IDisposable) // null
default(dynamic) // null
sizeof(bool) // Returns 1.
sizeof(byte) // Returns 1.
sizeof(sbyte) // Returns 1.
sizeof(char) // Returns 2.
sizeof(short) // Returns 2.
sizeof(ushort) // Returns 2.
sizeof(int) // Returns 4.
sizeof(uint) // Returns 4.
sizeof(float) // Returns 4.
sizeof(long) // Returns 8.
sizeof(ulong) // Returns 8.
sizeof(double) // Returns 8.
sizeof(decimal) // Returns 16.
Left-Shift
Right-Shift
It is used to declare lambda expressions and also it is widely used with LINQ Queries:
When used in LINQ extensions or queries the type of the objects can usually be skipped as it is inferred by the
compiler:
int shortestWordLength = [Link](w => [Link]); //also compiles with the same result
The parameters of the lambda expression are specified before => operator, and the actual
expression/statement/block to be executed is to the right of the operator:
// expression
(int x, string s) => [Link] > x
// expression
(int x, int y) => x + y
// statement
(string x) => [Link](x)
// block
(string x) => {
x += " says Hello!";
[Link](x);
}
This operator can be used to easily define delegates, without writing an explicit method:
myDelegate("Hello");
instead of
void MyMethod(string s)
{
[Link](s + " World");
}
myDelegate("Hello");
var x = 42;
x++;
[Link](x); // 43
Postfix decrement
X--
++x is called prefix increment it increments the value of x and then returns x while x++ returns the value of x and
then increments
var x = 42;
[Link](++x); // 43
[Link](x); // 43
while
var x = 42;
[Link](x++); // 42
[Link](x); // 43
To get the run-time type, use GetType method to obtain the [Link] of the current instance.
Operator typeof takes a type name as parameter, which is specified at compile time.
Example:
x += y
is the same as
x = x + y
Assignment operators:
+=
-=
*=
/=
%=
&=
|=
^=
<<=
>>=
The nameof operator was introduced in C# 6.0. It is evaluated at compile-time and the returned string value is
inserted inline by the compiler, so it can be used in most cases where the constant string can be used (e.g., the case
labels in a switch statement, attributes, etc...). It can be useful in cases like raising & logging exceptions, attributes,
MVC Action links, etc...
Lets assume we have method (a.k.a. a function) which takes an int parameter which will represent a score up to
100, and the method will print out a message saying whether we pass or fail.
When looking at this method, you may notice this line of code (score >= 50) inside the If statement. This can be
seen as a boolean condition, where if the condition is evaluated to equal true, then the code that is in between the
if { } is ran.
For example, if this method was called like this: PrintPassOrFail(60);, the output of the method would be a
Console Print saying Pass! since the parameter value of 60 is greater or equal to 50.
However, if the method was called like: PrintPassOrFail(30);, the output of the method would print out saying
Fail!. This is because the value 30 is not greater or equal to 50, thus the code in between the else { } is ran instead
of the If statement.
In this example, we've said that score should go up to 100, which hasn't been accounted for at all. To account for
score not going past 100 or possibly dropping below 0, see the If-Else If-Else Statement example.
is exactly equivalent to
in other words, the conditions inside the "if" statement just form an ordinary Boolean expression.
A common mistake when writing conditional statements is to explicitly compare to true and false:
In the example from If-Else Statement, the example specified that the score goes up to 100; however there were
never any checks against this. To fix this, lets modify the method from If-Else Statement to look like this:
All these statements will run in order from the top all the way to the bottom until a condition has been met. In this
new update of the method, we've added two new branches to now accommodate for the score going out of bounds.
For example, if we now called the method in our code as PrintPassOFail(110);, the output would be a Console
Print saying Error: score is greater than 100!; and if we called the method in our code like PrintPassOrFail(-20);,
the output would say Error: score is less than 0!.
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false
otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For
the string type, == compares the values of the strings.
A good method to implement GetHashCode is to use one prime number as a starting value, and add the hashcodes
of the fields of the type multiplied by other prime numbers to that:
Only the fields which are used in the Equals-method should be used for the hash function.
If you have a need to treat the same type in different ways for Dictionary/HashTables, you can use
IEqualityComparer.
If the instance is a reference type, then Equals will return true only if the references are the same.
If the instance is a value type, then Equals will return true only if the type and value are the same.
//areFooIntEqual: True
int fooInt1 = 42;
int fooInt2 = 42;
bool areFooIntEqual = [Link](fooInt2);
[Link]("fooInt1 and fooInt2 are equal: {0}", areFooIntEqual);
//areFooStringEqual: True
string fooString1 = "42";
string fooString2 = "42";
bool areFooStringEqual = [Link](fooString2);
[Link]("fooString1 and fooString2 are equal: {0}", areFooStringEqual);
}
}
var person1 = new Person { Name = "Jon", Age = 20, Clothes = "some clothes" };
var person2 = new Person { Name = "Jon", Age = 20, Clothes = "some other clothes" };
var person1 = new Person { Name = "Jon", Age = 20, Clothes = "some clothes" };
var person2 = new Person { Name = "Jon", Age = 20, Clothes = "some other clothes" };
Also using LINQ to make different queries on persons will check both Equals and GetHashCode:
Note that for this query, two objects have been considered equal if both the Equals returned true and the
GetHashCode have returned the same hash code for the two persons.
Non-nullable
int? a = null;
int b = 3;
var output = a ?? b;
var type = [Link]();
Output:
Type :System.Int32
View Demo
Nullable
int? a = null;
int? b = null;
var output = a ?? b;
Multiple Coalescing
int? a = null;
int? b = null;
int c = 3;
var output = a ?? b ?? c;
Output:
Type :System.Int32
value :3
View Demo
The null coalescing operator can be used in tandem with the null propagation operator to provide safer access to
properties of objects.
object o = null;
var output = o?.ToString() ?? "Default Value";
Output:
Type :[Link]
value :Default Value
View Demo
if (name == null)
name = "Unknown!";
The first time the property .FooBars is accessed the _fooBars variable will evaluate as null, thus falling through to
the assignment statement assigns and evaluates to the resulting value.
Thread safety
This is not thread-safe way of implementing lazy properties. For thread-safe laziness, use the Lazy<T> class built
into the .NET Framework.
Note that since C# 6, this syntax can be simplified using expression body for the property:
Subsequent accesses to the property will yield the value stored in the _fooBars variable.
This is often used when implementing commands in the MVVM pattern. Instead of initializing the commands
eagerly with the construction of a viewmodel, commands are lazily initialized using this pattern as follows:
If an object is potentially null (such as a function that returns a reference type) the object must first be checked for
null to prevent a possible NullReferenceException. Without the null-conditional operator, this would look like:
var age = person?.Age; // 'age' will be of type 'int?', even if 'person' is not null
The null-conditional operator can be combined on the members and sub-members of an object.
The null-conditional operator can be combined with the null-coalescing operator to provide a default value:
Normally, the method will be triggered for null references, and return -1:
Using ?. the method will not be triggered for null references, and the type is int?:
This behavior is actually expected from the way in which the ?. operator works: it will avoid making instance
method calls for null instances, in order to avoid NullReferenceExceptions. However, the same logic applies to the
extension method, despite the difference on how the method is declared.
For more information on why the extension method is called in the first example, please see the Extension
methods - null checking documentation.
The operation is evaluated at compile-time, which means that you can rename a referenced identifier, using an
IDE's rename feature, and the name string will update with it.
Would output
myString
because the name of the variable is "myString". Refactoring the variable name would change the string.
If called on a reference type, the nameof operator returns the name of the current reference, not the name or type
name of the underlying object. For example:
_address = value;
OnPropertyChanged(nameof(Address));
}
}
}
...
Console Output
Address
Over
...
switch ([Link])
{
case nameof([Link]):
[Link]("{0} changed to {1}", [Link], [Link]);
break;
case nameof([Link]):
[Link]("{0} changed to {1}", [Link], [Link]);
break;
}
}
...
Console Output
[Link](nameof(SomeClass<int>));
Console Output
TItem
SomeClass
...
Console Output
paramValue
[Link](nameof([Link]));
[Link](nameof(MyClass));
[Link](nameof([Link]));
[Link](nameof([Link]));
Console Output
MyNamespace
MyClass
MyNestedClass
MyStaticProperty
[Link]($@"Testing \n 1 2 {5 - 2}
New line");
Output:
Testing \n 1 2 3
New line
As expected from a verbatim string, the backslashes are ignored as escape characters. And as expected from an
interpolated string, any expression inside curly braces is evaluated before being inserted into the string at that
position.
Output:
In verbatim strings, there are no character escapes (except for "" which is turned into a "). To use a verbatim string,
just prepend a @ before the starting quotes.
Output:
c: emp
[Link]
using character escaping. (The \t is replaced with a tab character and the \n is replace with a newline.)
multiline paragraph";
Output:
This is a
multiline paragraph
Multi-line strings that contain double-quotes can also be escaped just as they were on a single line, because they
are verbatim strings.
""San Diego""
It should be noted that the spaces/tabulations at the start of lines 2 and 3 here are actually present in the value of the
variable; check this question for possible solutions.
[Link]("Hello {0} Foo {1}", "World", "Bar") //Hello World Foo Bar
char[] a = [Link]();
[Link](a);
string r = new string(a);
However, what these people don't realize is that this is actually wrong.
And I don't mean because of the missing NULL check.
It is actually wrong because a Glyph/GraphemeCluster can consist out of several codepoints (aka. characters).
To see why this is so, we first have to be aware of the fact what the term "character" actually means.
Reference:
A code point is the atomic unit of information. Text is a sequence of code points. Each code point is a
number which is given meaning by the Unicode standard.
A grapheme is a sequence of one or more code points that are displayed as a single, graphical unit that a
reader recognizes as a single element of the writing system. For example, both a and ä are graphemes,
but they may consist of multiple code points (e.g. ä may be two code points, one for the base character a
followed by one for the diaresis; but there's also an alternative, legacy, single code point representing this
grapheme). Some code points are never part of any grapheme (e.g. the zero-width non-joiner, or
directional overrides).
A glyph is an image, usually stored in a font (which is a collection of glyphs), used to represent graphemes
or parts thereof. Fonts may compose multiple glyphs into a single representation, for example, if the
above ä is a single code point, a font may chose to render that as two separate, spatially overlaid glyphs.
For OTF, the font's GSUB and GPOS tables contain substitution and positioning information to make this
work. A font may contain multiple alternative glyphs for the same grapheme, too.
Which means, if you just reverse a valid string like Les Misérables, which can look like this
As you can see, the accent is on the R character, instead of the e character.
Although [Link] will yield the original string if you both times reverse the char array, this kind of
reversal is definitely NOT the reverse of the original string.
[Link] enumerator =
[Link](s);
while ([Link]())
{
[Link]((string)[Link]);
}
return ls;
}
// this
private static string ReverseGraphemeClusters(string s)
{
if([Link](s) || [Link] == 1)
return s;
[Link]<string> ls = GraphemeClusters(s);
[Link]();
[Link](r);
}
And - oh joy - you'll realize if you do it correctly like this, it will also work for Asian/South-Asian/East-Asian languages
(and French/Swedish/Norwegian, etc.)...
/// <summary>
/// VB Right function
/// </summary>
/// <param name="stringparam"></param>
/// <param name="numchars"></param>
/// <returns>Right-most numchars characters</returns>
public static string Right( this string stringparam, int numchars )
{
// Handle possible Null or numeric stringparam being passed
stringparam += [Link];
/// <summary>
/// VB Mid function - to end of string
/// </summary>
/// <param name="stringparam"></param>
/// <param name="startIndex">VB-Style startindex, 1st char startindex = 1</param>
/// <returns>Balance of string beginning at startindex character</returns>
public static string Mid( this string stringparam, int startindex )
/// <summary>
/// VB Mid function - for number of characters
/// </summary>
/// <param name="stringparam"></param>
/// <param name="startIndex">VB-Style startindex, 1st char startindex = 1</param>
/// <param name="numchars">number of characters to return</param>
/// <returns>Balance of string beginning at startindex character</returns>
public static string Mid( this string stringparam, int startindex, int numchars)
{
// Handle possible Null or numeric stringparam being passed
stringparam += [Link];
}
}
string q = "{(Hi!*";
string r = [Link]( '(', '*', '{' ); // "Hi!"
string delimiter=",";
char[] charArray = new[] { 'a', 'b', 'c' };
string inputString = [Link](delimiter, charArray);
Output : a,b,c if we change the delimiter as "" then the output will become abc.
Output : a|b|c
Output : Ram_is_a_boy
Result:
Substring returns the string up from a given index, or between two indexes (both inclusive).
Using the [Link] you can find out if a particular string exists within a string. The method returns
a boolean, true if the string exists else false.
string s = "hello";
char c = s[1]; //Returns 'e'
Notice that the return type is char, unlike the Substring method which returns a string type.
You can also use the indexer to iterate through the characters of the string:
string s = "hello";
foreach (char c in s)
[Link](c);
/********* This will print each character on a new line:
h
e
l
l
o
**********/
This method can also be used to remove part of a string, using the [Link] field:
Note: The reason to use the invariant versions of these methods is to prevent producing unexpected culture-
specific letters. This is explained here in detail.
Example:
Note that you can choose to specify a specific Culture when converting to lowercase and uppercase by using the
[Link](CultureInfo) and [Link](CultureInfo) methods accordingly.
The Format methods are a set of overloads in the [Link] class used to create strings that combine objects
into specific string representations. This information can be applied to [Link], various WriteLine methods
as well as other methods in the .NET framework.
More examples for this under the topic C# 6.0 features: String interpolation.
if (format == "Reverse")
{
return [Link]("", [Link]().Reverse());
}
return [Link]();
}
Usage:
Output:
[Link]([Link]("{0:dd}",date));
output :
06
Лхагва
06
Specifier Meaning Sample Result
d Date {0:d} 7/6/2016
dd Day, zero-padded {0:dd} 06
ddd Short day name {0:ddd} Wed
dddd Full day name {0:dddd} Wednesday
D Long date {0:D} Wednesday, July 6, 2016
f Full date and time, short {0:f} Wednesday, July 6, 2016 6:30 PM
ff Second fractions, 2 digits {0:ff} 20
fff Second fractions, 3 digits {0:fff} 201
ffff Second fractions, 4 digits {0:ffff} 2016
F Full date and time, long {0:F} Wednesday, July 6, 2016 6:30:14 PM
Precision
Currency Symbol
2. Use any string as currency symbol. Use NumberFormatInfo as to customize currency symbol.
Use CurrencyPositivePattern for positive values and CurrencyNegativePattern for negative values.
Negative pattern usage is the same as positive pattern. A lot more use cases please refer to original link.
// invariantResult is "1,234,567.89"
var invarianResult = [Link]([Link], "{0:#,###,##}", 1234567.89);
// customResult is "1_GS_234_GS_567_NS_89"
var customResult = [Link](customProvider, "{0:#,###.##}", 1234567.89);
Output:
// Decimals
[Link]("Decimal, fixed precision: {0:0.000}; as percents: {0:0.00%}", 0.12);
Output:
...
However, the class User can also override ToString() in order to alter the string it returns. The code fragment below
prints out "Id: 5, Name: User1" to the console.
...
An easier approach might be to simply use the ToString() method available on all objects within C#. It supports all
of the same standard and custom formatting strings, but doesn't require the necessary parameter mapping as
there will only be a single argument :
While this approach may be simpler in some scenarios, the ToString() approach is limited with regards to adding
left or right padding like you might do within the [Link]() method :
In order to accomplish this same behavior with the ToString() method, you would need to use another method
like PadLeft() or PadRight() respectively :
Calls to Append() can be daisy chained, because it returns a reference to the StringBuilder:
This example uses the [Link](String, String[], Int32, Int32) overload, which specifies the start index
and count on top of the separator and value.
If you do not wish to use the startIndex and count overloads, you can join all string given. Like this:
[Link] can also be used to remove part of a string, by specifying an empty string as the replacement value:
Using the [Link] method, you can locate the starting position of a substring within an existing
string.
Note the returned position is zero-based, a value of -1 is returned if the substring is not found.
To find the first location from the end of a string, use the [Link] method:
In addition:
Output:
One
Two
Three
Four
Note: The reason to use the invariant versions of these methods is to prevent producing unexpected culture-
specific letters. This is explained here in detail.
Example:
Note that you can choose to specify a specific Culture when converting to lowercase and uppercase by using the
[Link](CultureInfo) and [Link](CultureInfo) methods accordingly.
You can also use the [Link] method to format the DateTime object. This will produce the same output
as the code above.
Output:
Note: MM stands for months and mm for minutes. Be very careful when using these as mistakes can
introduce bugs that may be difficult to discover.
${value, padding}
NOTE: Positive padding values indicate left padding and negative padding values indicate right padding.
Left Padding
A left padding of 5 (adds 3 spaces before the value of number, so it takes up a total of 5 character positions in the
resulting string.)
Output:
Right Padding
Right padding, which uses a negative padding value, will add spaces to the end of the current value.
Output:
You can also use existing formatting specifiers in conjunction with padding.
Internally this
$"Hello, {name}!"
The second example uses a verbatim string literal, which doesn't treat the backslash as an escape character.
Quotes
Newlines
Backslash
and suppose the character Œ is not available in the character encoding you use for your C# source files. You are
lucky, it is permitted to use escapes of the type \u#### or \U######## in identifiers in the code. So it is legal to
write:
and the C# compiler will know Œ and \u0152 are the same character.
(However, it might be a good idea to switch to UTF-8 or a similar encoding that can handle all characters.)
string s = "\c";
char c = '\c';
Instead, they will produce the error Unrecognized escape sequence at compile time.
In the above example, myString initially only has the value "Apples". However, when we concatenate `" are my
favorite fruit"', what the string class does internally needs to do involves:
Creating a new array of characters equal to the length of myString and the new string we are appending.
Copying all of the characters of myString into the beginning of our new array and copying the new string into
the end of the array.
Create a new string object in memory and reassign it to myString.
For a single concatenation, this is relatively trivial. However, what if needed to perform many append operations,
say, in a loop?
Due to the repeated copying and object creation, this will bring significantly degrade the performance of our
program. We can avoid this by instead using a StringBuilder.
Now when the same loop is run, the performance and speed of the execution time of the program will be
significantly faster than using a normal string. To make the StringBuilder back into a normal string, we can simply
call the ToString() method of StringBuilder.
However, this isn't the only optimization StringBuilder has. In order to further optimize functions, we can take
advantage of other properties that help improve performance.
If we know in advance how long our StringBuilder needs to be, we can specify its size ahead of time, which will
prevent it from needing to resize the character array it has internally.
[Link]('k', 2000);
Though using StringBuilder for appending is much faster than a string, it can run even faster if you only need to
add a single character many times.
Once you have completed building your string, you may use the ToString() method on the StringBuilder to
convert it to a basic string. This is often necessary because the StringBuilder class does not inherit from string.
In conclusion, StringBuilder should be used in place of string when many modifications to a string need to be
made with performance in mind.
return [Link]();
}
Result:
Result:
found = new List<string>() { "text in here", "another one", "third one", "fourth" }
DateTimeFormatInfo specifies a set of specifiers for simple date and time formatting. Every specifier correspond to
a particular DateTimeFormatInfo format pattern.
//Create datetime
DateTime dt = new DateTime(2016,08,01,18,50,23,230);
y (year)
M (month)
d (day)
h (hour 12)
H (hour 24)
m (minute)
s (second)
f (second fraction)
F (second fraction, trailing zeroes are trimmed)
t (P.M or A.M)
z (time zone).
var year = [Link]("{0:y yy yyy yyyy}", dt); // "16 16 2016 2016" year
var month = [Link]("{0:M MM MMM MMMM}", dt); // "8 08 Aug August" month
var day = [Link]("{0:d dd ddd dddd}", dt); // "1 01 Mon Monday" day
var hour = [Link]("{0:h hh H HH}", dt); // "6 06 18 18" hour 12/24
var minute = [Link]("{0:m mm}", dt); // "50 50" minute
var secound = [Link]("{0:s ss}", dt); // "23 23" second
var fraction = [Link]("{0:f ff fff ffff}", dt); // "2 23 230 2300" [Link]
var fraction2 = [Link]("{0:F FF FFF FFFF}", dt); // "2 23 23 23" without zeroes
var period = [Link]("{0:t tt}", dt); // "P PM" A.M. or P.M.
var zone = [Link]("{0:z zz zzz}", dt); // "+0 +00 +00:00" time zone
You can use also date separator / (slash) and time sepatator : (colon).
[Link]([Link]());
DateTime dateTime;
[Link](result);
}
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else relationship = "is later than";
// daysInFeb gets 28 because the year 1998 was not a leap year.
int daysInFeb = [Link](1998, Feb);
[Link](daysInFeb);
[Link]();
1. The function always evaluates the same result value given the same argument value(s). The function result
value cannot depend on any hidden information or state that may change while program execution proceeds
or between different executions of the program, nor can it depend on any external input from I/O devices .
2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation
of mutable objects or output to I/O devices
As a developer you need to be aware of pure methods and you will stumble upon these a lot in many areas. One I
have seen that bites many junior developers is working with DateTime class methods. A lot of these are pure and if
you are unaware of these you can be in for a suprise. An example:
Given the example above one may expect the result printed to console to be '26/12/2016' but in reality you end up
with the same date. This is because AddDays is a pure method and does not affect the original date. To get the
expected output you would have to modify the AddDays call to the following:
sample = [Link](1);
Section 19.11:
[Link](String, String, IFormatProvider, D
ateTimeStyles, DateTime)
Converts the specified string representation of a date and time to its DateTime equivalent using the specified
format, culture-specific format information, and style. The format of the string representation must match the
specified format exactly. The method returns a value that indicates whether the conversion succeeded.
For Example
dateString = "2008-06-11T16:11:20.0904778Z";
if([Link](dateString, "o", [Link], [Link], out
dateValue))
{
[Link]("Converted '{0}' to {1} ({2}).", dateString, dateValue, [Link]);
}
else
{
[Link]("'{0}' is not in an acceptable format.", dateString);
}
Outputs
Example of Parse:
[Link](dutchDateString, dutchCulture)
// output {31/10/1999 04:20:00}
Section 19.15:
[Link](String, String, IFormatProvider)
Converts the specified string representation of a date and time to its DateTime equivalent using the specified
format and culture-specific format information. The format of the string representation must match the specified
format exactly.
Let's say we have a culture-specific DateTime string 08-07-2016 11:30:12 PM as MM-dd-yyyy hh:mm:ss tt format
and we want it to convert to equivalent DateTime object
Convert a date time string to equivalent DateTime object without any specific culture format
Let's say we have a DateTime string in dd-MM-yy hh:mm:ss tt format and we want it to convert to equivalent
DateTime object, without any specific culture information
Convert a date time string to equivalent DateTime object without any specific culture format with different
format
For example:
[Link]([Link]);
Indices in C# are zero-based. The indices of the array above will be 0-9. For example:
Which means the system starts counting the element index from 0. Moreover, accesses to elements of arrays are
done in constant time. That means accessing to the first element of the array has the same cost (in time) of
accessing the second element, the third element and so on.
You may also declare a bare reference to an array without instantiating an array.
An array can also be created and initialized with custom values using collection initialization syntax:
The new int[] portion can be omitted when declaring an array variable. This is not a self-contained expression, so
using it as part of a different call does not work (for that, use the version with new):
Alternatively, in combination with the var keyword, the specific type may be omitted so that the type of the array is
inferred:
// same as int[]
var arr = new [] { 1, 2, 3 };
// same as string[]
var arr = new [] { "one", "two", "three" };
// same as double[]
var arr = new [] { 1.0, 2.0, 3.0 };
To create an array initialized with a non-default value, we can use [Link] from the [Link]
Namespace:
Copying the whole array with the CopyTo() instance method, beginning at index 0 of the source and the specified
index in the destination:
Both CopyTo and Clone perform shallow copy which means the contents contains references to the same object as
the elements in the original array.
The SequenceEqual function will return true if the arrays have the same length and the values in corresponding
indices are equal, and false otherwise.
int[,] arr = new int[4, 2] { {1, 1}, {2, 2}, {3, 3}, {4, 4} };
// Get
[Link](arr[2]); // 20
// Set
arr[2] = 100;
using foreach:
unsafe
{
int length = [Link];
fixed (int* p = arr)
{
int* pInt = p;
while (length-- > 0)
{
[Link](*pInt);
pInt++;// move pointer to next element
}
}
}
Output:
1
6
3
3
9
The [Link] method allows us to create sequence of integer numbers from a specified start position and
a number of elements.
The method takes two arguments: the starting value and the number of elements to generate.
Usage:
int[] sequence = [Link](1, 100).ToArray();
This will generate an array containing the numbers 1 through 100 ([1, 2, 3, ..., 98, 99, 100]).
Because the Range method returns an IEnumerable<int>, we can use other LINQ methods on it:
This will generate an array that contains 10 integer squares starting at 4: [4, 9, 16, ..., 100, 121].
The second [] is initialized without a number. To initialize the sub arrays, you would need to do that separately:
Getting/Setting values
Now, getting one of the subarrays is easy. Let's print all the numbers of the 3rd column of a:
a[<row_number>][<column_number>]
a[<row_number>][<column_number>] = <value>
Remember: It's always recommended to use jagged arrays (arrays of arrays) rather than multidimensional arrays
(matrixes). It's faster and safer to use.
Consider a three-dimensional array of five-dimensional arrays of one-dimensional arrays of int. This is written in
C# as:
In the CLR type system, the convention for the ordering of the brackets is reversed, so with the above arr instance
we have:
[Link]().ToString() == "System.Int32[][,,,,][,,]"
and likewise:
typeof(int[,,][,,,,][]).ToString() == "System.Int32[][,,,,][,,]"
This conversion is not type-safe. The following code will raise a runtime exception:
More importantly, one-dimensional arrays implement the IList<> and IReadOnlyList<> generic interfaces (and
their base interfaces) for the type of data that they contain. This means that they can be treated as generic
enumerable types and passed in to a variety of methods without needing to first convert them to a non-array form.
int[] arr1 = { 3, 5, 7 };
IEnumerable<int> enumerableIntegers = arr1; //Allowed because arrays implement IEnumerable<T>
List<int> listOfIntegers = new List<int>();
[Link](arr1); //You can pass in a reference to an array to populate a List.
After running this code, the list listOfIntegers will contain a List<int> containing the values 3, 5, and 7.
The IEnumerable<> support means arrays can be queried with LINQ, for example [Link](i => 10 * i).
/// Sample
array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
shiftCount = -1;
Rotate(ref array, shiftCount);
[Link]([Link](", ", array));
// Output: [2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
shiftCount = -35;
Rotate(ref array, shiftCount);
[Link]([Link](", ", array));
// Output: [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
}
array = backupArray;
}
The thing that is important in this code is the formula with which we find the new index value after the rotation.
(shiftCount % [Link]) -> we normalize the shifting value to be in the length of the array (since in an array
with length 10, shifting 1 or 11 is the same thing, the same goes for -1 and -11).
[Link] + (shiftCount % [Link]) -> this is done due to left rotations to make sure we do not go into a
negative index, but rotate it to the end of the array. Without it for an array with length 10 for index 0 and a rotation
-1 we would go into a negative number (-1) and not get the real rotation index value, which is 9. (10 + (-1 % 10) = 9)
index + [Link] + (shiftCount % [Link]) -> not much to say here as we apply the rotation to the index
to get the new index. (0 + 10 + (-1 % 10) = 9)
index + [Link] + (shiftCount % [Link]) % [Link] -> the second normalization is making sure
that the new index value does not go outside of the array, but rotates the value in the beginning of the array. It is
for right rotations, since in an array with length 10 without it for index 9 and a rotation 1 we would go into index 10,
which is outside of the array, and not get the real rotation index value is 0. ((9 + 10 + (1 % 10)) % 10 = 0)
This is useful when P/Invoking to native code, mapping to data sources, and similar circumstances. In general, the
default int should be used, because most developers expect an enum to be an int.
An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set
of named integral constants that may be assigned to a variable.
Essentially, an enum is a type that only allows a set of finite options, and each option corresponds to a number. By
default, those numbers are increasing in the order the values are declared, starting from zero. For example, one
could declare an enum for the days of the week:
By default the underlying type of each element in the enum is int, but byte, sbyte, short, ushort, uint, long and
ulong can be used as well. If you use a type other than int, you must specify the type using a colon after the enum
name:
[Link](typeof(Days)));
Output:
[Link]
[Flags]
enum MyEnum
{
//None = 0, can be used but not combined in bitwise operations
FlagA = 1,
FlagB = 2,
FlagC = 4,
FlagD = 8
//you must use powers of two or combinations of powers of two
//for bitwise operations to work
}
// This will enumerate all the flags in the variable: "FlagA, FlagB".
[Link](twoFlags);
Because FlagsAttribute relies on the enumeration constants to be powers of two (or their combinations) and
enum values are ultimately numeric values, you are limited by the size of the underlying numeric type. The largest
available numeric type that you can use is UInt64, which allows you to specify 64 distinct (non-combined) flag enum
constants. The enum keyword defaults to the underlying type int, which is Int32. The compiler will allow the
declaration of values wider than 32 bit. Those will wrap around without a warning and result in two or more enum
members of the same value. Therefore, if an enum is meant to accomodate a bitset of more than 32 flags, you need
to specify a bigger type explicitely:
Although flags are often only a single bit, they can be combined into named "sets" for easier use.
[Flags]
enum FlagsEnum
{
None = 0,
Option1 = 1,
Option2 = 2,
Option3 = 4,
To avoid spelling out the decimal values of powers of two, the left-shift operator (<<) can also be used to declare the
same enum
[Flags]
enum FlagsEnum
{
None = 0,
Option1 = 1 << 0,
Option2 = 1 << 1,
Option3 = 1 << 2,
To check if the value of enum variable has a certain flag set, the HasFlag method can be used. Let's say we have
[Flags]
enum MyEnum
{
One = 1,
Two = 2,
Three = 4
}
And a value
if([Link]([Link]))
[Link]("Enum has One");
if([Link]([Link]))
[Link]("Enum has Two");
if([Link]([Link]))
[Link]("Enum has Three");
Also we can iterate through all values of enum to get all flags that are set
if ([Link](item))
[Link]("Enum has " + name);
}
This also helps to improve readability of large enums with plenty of flags in them.
[Flags]
public enum MyEnum
{
None = 0,
Flag1 = 1 << 0,
Flag2 = 1 << 1,
Flag3 = 1 << 2,
Flag4 = 1 << 3,
Flag5 = 1 << 4,
...
Flag31 = 1 << 30
}
It is obvious now that MyEnum contains proper flags only and not any messy stuff like Flag30 = 1073741822 (or
111111111111111111111111111110 in binary) which is inappropriate.
[Flags]
enum FlagsEnum
{
Option1 = 1,
Option2 = 2,
Option3 = 4,
Option2And3 = Option2 | Option3;
The Default value is actually a combination of two others merged with a bitwise OR. Therefore to test for the
presence of a flag we need to use a bitwise AND.
[Link](isOption2And3Set);
[Flags]
public enum MyEnum
{
Flag1 = 1 << 0,
Flag2 = 1 << 1,
Flag3 = 1 << 2
}
// remove flag
value &= ~MyEnum.Flag2; //value is now Flag1, Flag3
// Enum to string
string thursday = [Link](); // "Thursday"
// String to enum (.NET 4.0+ only - see below for alternative syntax for earlier .NET versions)
DayOfWeek tuesday;
[Link]("Tuesday", out tuesday); // [Link]
DayOfWeek sunday;
bool matchFound1 = [Link]("SUNDAY", out sunday); // Returns false (case-sensitive match)
DayOfWeek wednesday;
bool matchFound2 = [Link]("WEDNESDAY", true, out wednesday); // Returns true;
[Link] (case-insensitive match)
Although the below enum type DaysOfWeek only has 7 defined values, it can still hold any int value.
DaysOfWeek d = (DaysOfWeek)31;
[Link](d); // prints 31
DaysOFWeek s = [Link];
s++; // No error
There is currently no way to define an enum which does not have this behavior.
However, undefined enum values can be detected by using the method [Link]. For example,
DaysOfWeek d = (DaysOfWeek)31;
[Link]([Link](typeof(DaysOfWeek),d)); // prints False
if (e == [Link])
[Link]("defaults to one");
Example: [Link]
For example:
Now, if you would like to return the description of a specific enum value you can do the following:
This can also be easily transformed to an extension method for all enums:
foreach(MyEnum e in [Link](typeof(MyEnum)))
[Link](e);
One
Two
Three
[Flags]
enum Colors
{
Red=1,
Blue=2,
Green=4,
Yellow=8
}
prints Red,Blue
enum Colors
{
Red=1,
Blue=2,
Green=4,
Yellow=8
}
var color = [Link] | [Link];
[Link]([Link]());
prints 3
var tuple = new Tuple<string, int, bool, MyClass>("foo", 123, true, new MyClass());
var item1 = tuple.Item1; // "foo"
var item2 = tuple.Item2; // 123
var item3 = tuple.Item3; // true
var item4 = tuple.Item4; // new My Class()
Tuples can also be created using static [Link] methods. In this case, the types of the elements are inferred
by the C# Compiler.
(int number, bool flag, MyClass instance) tuple = (123, true, new MyClass());
As an example, an enumerable whose elements are of type Tuple can be sorted based on comparisons operators
defined on a specified element:
// Output:
void Write()
{
var result = AddMultiply(25, 28);
[Link](result.Item1);
[Link](result.Item2);
}
Output:
53
700
Now C# 7.0 offers an alternative way to return multiple values from methods using value tuples More info about
ValueTuple struct.
Depending on your needs you can also format the Guid, by adding a format type argument to the ToString call.
// None "7febf16f651b43b0a5e30da8da49e90d"
[Link]([Link]("N"));
// Hyphens "7febf16f-651b-43b0-a5e3-0da8da49e90d"
[Link]([Link]("D"));
// Braces "{7febf16f-651b-43b0-a5e3-0da8da49e90d}"
[Link]([Link]("B"));
// Parentheses "(7febf16f-651b-43b0-a5e3-0da8da49e90d)"
[Link]([Link]("P"));
// Hex "{0x7febf16f,0x651b,0x43b0{0xa5,0xe3,0x0d,0xa8,0xda,0x49,0xe9,0x0d}}"
[Link]([Link]("X"));
Guid g = [Link];
Guid g2 = new Guid();
Guid g = [Link]();
Declaration :
using System;
using [Link];
namespace Euler_25
{
class Program
{
static void Main(string[] args)
{
BigInteger l1 = 1;
BigInteger l2 = 1;
BigInteger current = l1 + l2;
while ([Link]().Length < 1000)
{
l2 = l1;
l1 = current;
current = l1 + l2;
}
[Link](current);
}
}
}
This simple algorithm iterates through Fibonacci numbers until it reaches one at least 1000 decimal digits in length,
then prints it out. This value is significantly larger than even a ulong could hold.
Theoretically, the only limit on the BigInteger class is the amount of RAM your application can consume.
Collection initializers are syntactic sugar for Add() calls. Above code is equivalent to:
Note that the intialization is done atomically using a temporary variable, to avoid race conditions.
For types that offer multiple parameters in their Add() method, enclose the comma-separated arguments in curly
braces:
Dictionary Initialization
So there is a significant difference in functionality, as the new syntax uses the indexer of the initialized object to
assign values instead of using its Add() method. This means the new syntax only requires a publicly available
indexer, and works for any object that has one.
class Program
{
static void Main()
{
var col = new MyCollection {
"foo",
{ "bar", 3 },
Synonyms is a collection-type property. When the Tag object is created using object initializer syntax, Synonyms can
also be initialized with collection initializer syntax:
The collection property can be readonly and still support collection initializer syntax. Consider this modified
example (Synonyms property now has a private setter):
This works because collection initializers are just syntatic sugar over calls to Add(). There's no new list being created
here, the compiler is just generating calls to Add() on the exiting object.
[Link] uses a hash table, so that lookups are extremely fast, regardless of the number of items in the
collection.
using [Link];
// Reading data
[Link](people["John"]); // 30
[Link](people["George"]); // throws KeyNotFoundException
int age;
if ([Link]("Mary", out age))
{
[Link](age); // 35
}
// add something
// note that we add 2 before we add 1
[Link](2);
[Link](1);
// output:
// 1
// 2
// output:
// one
// two
// output:
// something else
// two
using [Link];
List<T> can be thought of as an array that you can resize. Enumerating over the collection in order is quick, as is
access to individual elements via their index. To access elements based on some aspect of their value, or some
other key, a Dictionary<T> will provide faster lookup.
// Pop removes the top element of the stack and returns it.
[Link]([Link]()); // prints 8
[Link]([Link]()); // prints 5
[Link]([Link]()); // prints 3
[Link](2);
// the list now is 2, 3, 5, 8
[Link]();
// the list is now 3, 5, 8
[Link]();
// the list is now 3, 5
Initialization - Makes a new local variable that can only be used in the loop.
Condition - The loop only runs when the condition is true.
Increment - How the variable changes every time the loop runs.
An example:
Output:
0
1
2
3
4
You can also leave out spaces in the For Loop, but you have to have all semicolons for it to function.
Output for 3:
3
5
7
9
11
// Sum values from the array until we get a total that's greater than 10,
// or until we run out of values.
int sum = 0;
int i = 0;
do
{
sum += numbers[i];
i++;
} while (sum <= 10 && i < [Link]);
[Link](sum); // 13
syntax
remarks
1. The type ItemType does not need to match the precise type of the items, it just needs to be assignable from
the type of the items
2. Instead of ItemType, alternatively var can be used which will infer the items type from the enumerableObject
by inspecting the generic argument of the IEnumerable implementation
3. The statement can be a block, a single statement or even an empty statement (;)
4. If enumerableObject is not implementing IEnumerable, the code will not compile
5. During each iteration the current item is cast to ItemType (even if this is not specified but compiler-inferred
via var) and if the item cannot be cast an InvalidCastException will be thrown.
is equivalent to:
The most trivial loop type. Only drawback is there is no intrinsic clue to know where you are in the loop.
Do
Similar to while, but the condition is evaluated at the end of the loop instead of the beginning. This results in
executing the loops at least once.
do
{
/// do something
} while(condition) /// loop while the condition satisfies
For
Another trivial loop style. While looping an index (i) gets increased and you can use it. It is usually used for handling
arrays.
Foreach
Modernized way of looping through IEnumarable objects. Good thing that you don't have to think about the index
of the item or the item count of the list.
Foreach Method
While the other styles are used for selecting or updating the elements in collections, this style is usually used for
calling a method straight away for all elements in a collection.
// or
[Link](item => DoSomething(item));
// using an array
[Link](myArray, [Link]);
It is important to note that this method in only available on List<T> instances and as a static method on Array - it is
not part of Linq.
Just like Linq Foreach, except this one does the job in a parallel manner. Meaning that all the items in the collection
will run the given action at the same time, simultaneously.
/// or
[Link]().ForAll(item => DoSomething(item));
[Link](i);
9
10
Note: Continue is often most useful in while or do-while loops. For-loops, with well-defined exit conditions, may not
benefit as much.
Output:
0
1
2
3
4
// Call a custom method that takes a count, and returns an IEnumerator for a list
// of strings with the names of theh largest city metro areas.
IEnumerator<string> largestMetroAreas = GetLargestMetroAreas(4);
while ([Link]())
{
[Link]([Link]);
}
Sample output:
Tokyo/Yokohama
New York Metro
Sao Paulo
Seoul/Incheon
for (;;)
{
if (should_end_loop)
break;
// do something
}
Alternative:
if (!endLoop)
{
// do something
}
}
Note: In nested loops and/or switch must use more than just a simple break.
Most methods will return control to their caller through normal return statements, which disposes all state local to
that method. In contrast, methods that use yield statements allow them to return multiple values to the caller on
request while preserving local state in-between returning those values. These returned values constitute a
sequence. There are two types of yield statements used within iterators:
yield return, which returns control to the caller but preserves state. The callee will continue execution from
this line when control is passed back to it.
yield break, which functions similarly to a normal return statement - this signifies the end of the sequence.
Normal return statements themselves are illegal within an iterator block.
This example below demonstrates an iterator method that can be used to generate the Fibonacci sequence:
This iterator can then be used to produce an enumerator of the Fibonacci sequence that can be consumed by a
calling method. The code below demonstrates how the first ten terms within the Fibonacci sequence can be
enumerated:
void Main()
{
foreach (int term in Fibonacci(10))
{
[Link](term);
}
}
Output
1
1
2
3
5
8
13
21
This is possible because arrays implement the IEnumerable interface, allowing clients to obtain an iterator for the
array using the GetEnumerator() method. This method returns an enumerator, which is a read-only, forward-only
cursor over each number in the array.
int[] numbers = { 1, 2, 3, 4, 5 };
while ([Link]())
{
[Link]([Link]);
}
Output
1
2
3
4
5
It's also possible to achieve the same results using a foreach statement:
IEnumerable is an interface which implements the method GetEnumerator. The GetEnumerator method returns an
IEnumerator which provides options to iterate through the collection like foreach.
public CoffeeCollection() {
enumerator = new CoffeeEnumerator();
}
return false;
}
In the example below, the object sequenceOfNumbers implements IEnumerable. It represents a series of integers.
The foreach loop iterates through each in turn.
In C#, arguments can be passed to parameters either by value or by reference. Passing by reference
enables function members, methods, properties, indexers, operators, and constructors to change the
value of the parameters and have that change persist in the calling environment. To pass a parameter by
reference, use the ref or out keyword.
The difference between ref and out is that out means that the passed parameter has to be assigned before the
function [Link] contrast parameters passed with ref can be changed or left unchanged.
using System;
class Program
{
static void Main(string[] args)
{
int a = 20;
[Link]("Inside Main - Before Callee: a = {0}", a);
Callee(a);
[Link]("Inside Main - After Callee: a = {0}", a);
[Link]();
}
Output :
You'll notice that even though the printingList list was made before the corrections to student names after the
typos, the PrintPrintingList method still prints out the corrected names:
Scott Duke
Vincent Kong
Craig Brett
This is because both lists hold a list of references to the same students. SO changing the underlying student object
propogates to usages by either list.
class Program
{
static void Main(string[] args)
{
int a = 20;
[Link]("Inside Main - Before Callee: a = {0}", a);
Callee(a);
[Link]("Inside Main - After Callee: a = {0}", a);
[Link]();
Output
Assigning to a variable of a List<int> does not create a copy of the List<int>. Instead, it copies the reference to
the List<int>. We call types that behave this way reference types.
value = 4;
[Link](nameof(ByOut) + value);
}
int outValue2 = 10; // does not make any sense for out
ByOut(out outValue2); // prints 4
}
int refValue2 = 0;
ByRef(ref refValue2); // prints 0 and 4
The catch is that by using out the parameter must be initialized before leaving the method, therefore the following
Making these changes would make the number update as expected, meaning the console output for number would
be 8.
This can be avoided by first unboxing into the original Type, e.g.:
This can be avoided by using the overloaded Equals method, which will give the expected result.
Alternatively, the same could be done by unboxing the left and right variables so that the int values are
compared:
Section 32.5: Value type - short, int, long (signed 16 bit, 32 bit,
64 bit integers)
// assigning a signed short to its minimum value
short s = -32768;
// assigning a signed long to its minimum value (note the long postfix)
long l = -9223372036854775808L;
// assigning a signed long to its maximum value (note the long postfix)
long l = 9223372036854775807L;
It is also possible to make these types nullable, meaning that additionally to the usual values, null can be assigned,
too. If a variable of a nullable type is not initialized, it will be null instead of 0. Nullable types are marked by adding a
question mark (?) after the type.
// assigning an unsigned long to its maximum value (note the unsigned long postfix)
ulong l = 18446744073709551615UL;
It is also possible to make these types nullable, meaning that additionally to the usual values, null can be assigned,
too. If a variable of a nullable type is not initialized, it will be null instead of 0. Nullable types are marked by adding a
question mark (?) after the type.
The bool keyword is an alias of [Link]. It is used to declare variables to store the Boolean values, true and
false.
The C# type keywords and their aliases are interchangeable. For example, you can declare an integer variable by
using either of the following declarations:
Conversely, dynamic has dynamic type checking, opting for runtime errors, instead of compile-time errors.
If the member names are not specified, they are set to the name of the property/variable used to initialize the
object.
int foo = 1;
int bar = 2;
var anon2 = new { foo, bar };
// [Link] == 1
// [Link] == 2
Note that names can only be omitted when the expression in the anonymous type declaration is a simple property
access; for method calls or more complex expressions, a property name must be specified.
Two anonymous types are considered the same if and only if their properties have the same name and type and
appear in the same order.
In the case of List<T>, implicitly typed arrays may be converted to a List<T> through the ToList LINQ method:
[Link]([Link]);
// 456
[Link]([Link]);
// Throws RuntimeBinderException
foo = "123";
[Link](foo + 234);
// 123234
[Link]([Link]()):
// NOW A STRING
class IfElseExample
{
public string DebugToString(object a)
{
if (a is StringBuilder)
class DynamicExample
{
public string DebugToString(object a)
{
return DebugToStringInternal((dynamic)a);
}
The advantage to the dynamic, is adding a new Type to handle just requires adding an overload of
DebugToStringInternal of the new type. Also eliminates the need to manually cast it to the type as well.
class Program
{
static void Main(string[] args)
{
Digit dig = new Digit(7);
//This call invokes the implicit "double" operator
double num = dig;
//This call invokes the implicit "Digit" operator
Digit dig2 = 12;
[Link]("num = {0} dig2 = {1}", num, [Link]);
[Link]();
}
}
Output:
if(value is int)
{
[Link](value + "is an int");
}
string [Link]()
{
return "IMyInterface2";
}
}
// Outputs :
// I am : IMyInterface1
// I am : IMyInterface2
If we wanted to create a JsExpression representing a comparison of two JavaScript values, we could do something
like this:
But we can add some explicit conversion operators to JsExpression, to allow a simple conversion when using explicit
casting.
// Usage:
JsExpression intExpression = (JsExpression)(-1);
JsExpression doubleExpression = (JsExpression)(-1.0);
[Link]([Link](doubleExpression)); // (-1 == -1.0)
Or, we could change these operators to implicit to make the syntax much simpler.
// Usage:
JsExpression intExpression = -1;
[Link]([Link](-1.0)); // (-1 == -1.0)
interface IThing { }
class Thing : IThing { }
LINQ allows you to create a projection that changes the compile-time generic type of an IEnumerable<> via the
[Link]<>() and [Link]<>() extension methods.
When things2 is evaluated, the Cast<>() method will try to cast all of the values in things into Things. If it
encounters a value that cannot be cast, an InvalidCastException will be thrown.
When things3 is evaluated, the OfType<>() method will do the same, except that if it encounters a value that
cannot be cast, it will simply omit that value rather than throw an exception.
Due to the generic type of these methods, they cannot invoke Conversion Operators or perform numeric
conversions.
If we tried passing value directly to [Link](), we would get a compile-time exception because [Link]()
doesn't have an overload that takes an object as a parameter.
If value could not be cast to an int, then the second line in this example would throw an InvalidCastException
In this example, we didn't need to use the typical explicit casting syntax because the compiler knows all ints can be
cast to objects. In fact, we could avoid creating variables and pass -1 directly as the argument of
[Link]() that expects an object.
[Link](-1);
Note that in cases where the destination type has less precision than the original type, precision will be lost. For
example, -1.1 as a double value in the above example becomes -1 as an integer value.
Also, numeric conversions rely on compile-time types, so they won't work if the numeric types have been "boxed"
into objects.
Nullable<int> i = null;
Or:
int? i = null;
Or:
var i = (int?)null;
Nullable<int> i = 0;
Or:
int? i = 0;
if (i != null)
{
[Link]("i is not null");
}
else
{
[Link]("i is null");
}
if ([Link])
{
[Link]("i is not null");
}
else
{
[Link]("i is null");
}
int? i = 10;
int j = i ?? 0;
int j = [Link](0);
int j = [Link] ? [Link] : 0;
The following usage is always unsafe. If i is null at runtime, a [Link] will be thrown.
At design time, if a value is not set, you'll get a Use of unassigned local variable 'i' error.
int j = [Link];
class Program
{
static void Main()
{
int? nullableExample = null;
int result = [Link]();
[Link](result); // will output the default value for int - 0
int secondResult = [Link](1);
[Link](secondResult) // will output our specified default - 1
int thirdResult = nullableExample ?? 1;
[Link](secondResult) // same as the GetValueOrDefault but a bit shorter
}
}
Output:
0
1
Output:
null
There are some tricks which allow to effectively use the result of the [Link] method when
creating code related to reflection/code-generation purposes:
The usage:
if([Link]())
[Link]("Type is nullable.");
Type underlyingType;
if([Link](out underlyingType))
[Link]("The underlying type is " + [Link] + ".");
if([Link]<int>())
[Link]("Type is either exact or nullable Int32.");
if()
[Link]("Type is neither exact nor nullable enum.");
Output:
[Link]`1[System.Int32]
Type is nullable.
The underlying type is Int32.
Type is either exact or nullable Int32.
Type is neither exact nor nullable enum.
Destructors/Finalizers are methods in a class that are invoked when an instance of that is destroyed. In C# they are
rarely explicitely written/used.
Initialize static state, that is state which is shared across different instances of the same class.
Create a singleton
Example:
class Animal
{
// * A static constructor is executed only once,
// when a class is first accessed.
// * A static constructor cannot have any access modifiers
// * A static constructor cannot have any parameters
static Animal()
{
[Link]("Animal initialized");
}
Output:
Animal initialized
Animal created
Animal created
View Demo
If the first call is to a static method, the static constructor is invoked without the instance constructor. This is OK,
because the static method can't access instance state anyways.
Animal initialized
Yawn!
Singleton example:
static SessionManager()
{
Instance = new SessionManager();
}
}
private SingletonClass()
{
// Put custom constructor code here
}
}
Because the constructor is private, no new instances of SingletonClass can be made by consuming code. The only
way to access the single instance of SingletonClass is by using the static property [Link].
The Instance property is assigned by a static constructor that the C# compiler generates. The .NET runtime
guarantees that the static constructor is run at most once and is run before Instance is first read. Therefore, all
synchronization and initialization concerns are carried out by the runtime.
Note, that if the static constructor fails the Singleton class becomes permanently unusable for the life of the
AppDomain.
Also, the static constructor is not guaranteed to run at the time of the first access of Instance. Rather, it will run at
some point before that. This makes the time at which initialization happens non-deterministic. In practical cases the
JIT often calls the static constructor during compilation (not execution) of a method referencing Instance. This is a
performance optimization.
See the Singleton Implementations page for other ways to implement the singleton pattern.
The definition of any constructor for the type will suppress the default constructor generation. If the type were
defined as follows:
// This is valid
var myAnimal = new Animal("Fluffy");
// This fails to compile
var unnamedAnimal = new Animal();
For the second example, the compiler will display an error message:
If you want a class to have both a parameterless constructor and a constructor that takes a parameter, you can do
it by explicitly implementing both constructors.
The compiler will not be able to generate a default constructor if the class extends another class which doesn't have
a parameterless constructor. For example, if we had a class Creature:
Remark: All static initialization (fields initializers for example) will run, not only the constructor itself.
Potential usages: Forcing initialization during the splash screen in an UI application or ensuring that a static
constructor doesn't fail in an unit test.
If a derived class doesn't explicitly specify which constructor of the base class should be called, the compiler
assumes the parameterless constructor.
In this case, instantiating a Mammal by calling new Mammal("George the Cat") will print
Calling a different constructor of the base class is done by placing : base(args) between the constructor's
signature and its body:
View Demo
class TheBaseClass
{
~TheBaseClass()
{
[Link]("Base class finalized!");
}
}
try
{
[Link]();
}
catch (Exception e)
{
[Link]([Link]());
}
try
{
[Link]();
}
catch (Exception e)
{
[Link]([Link]());
}
Static ctor
[...]
where you can see that the actual constructor is only executed once, and the exception is re-used.
public TestClass()
{
if (TestProperty == 1)
if (TestProperty == 2)
{
[Link]("Or shall this be executed");
}
}
}
In the example above, shall the TestProperty value be 1 in the class' constructor or after the class constructor?
Will be executed after the constructor is run. However, initializing the property value in the class' property in C# 6.0
like this:
public TestClass()
{
}
}
public TestClass()
{
if (TestProperty == 1)
{
[Link]("Shall this be executed?");
}
if (TestProperty == 2)
{
[Link]("Or shall this be executed");
}
}
}
Explanation:
The TestProperty value will first be assigned as 2, then the TestClass constructor will be run, resulting in printing
of
And then the TestProperty will be assigned as 1 due to new TestClass() { TestProperty = 1 }, making the final
value for the TestProperty printed by [Link]([Link]) to be
"1"
class Animal<T>
{
static Animal()
{
[Link](typeof(T).FullName);
}
Animal<Object>.Yawn();
Animal<String>.Yawn();
[Link]
[Link]
If you come from a C++ background this is surprising, base class constructor already sees derived class virtual
method table!
Be careful: derived class may not been fully initialized yet (its constructor will be executed after base class
constructor) and this technique is dangerous (there is also a StyleCop warning for this). Usually this is regarded as
bad practice.
private
internal
protected
protected internal
Assembly 1
Assembly 2
void MyMethod2()
// Compile Error
var myProtectedInternalProperty = [Link];
// Compile Error
var myProtectedInternalNestedInstance =
new [Link]();
}
// Compile Error
var myProtectedInternalProperty = [Link];
// Compile Error
var myProtectedInternalNestedInstance =
new [Link]();
}
void MyMethod2()
{
Foo foo = new Foo();
var myPublicProperty = [Link];
//Compile Error
var myProtectedInternalProperty = [Link];
// Compile Error
var myProtectedInternalNestedInstance =
new [Link]();
}
}
This can be broken to allow a testing assembly to access the code via adding code to [Link] file:
using [Link];
[assembly:InternalsVisibleTo("MyTests")]
Because they implement INoiseMaker, both cat and dog are required to include the string MakeNoise() method
and will fail to compile without it.
interface IChauffeur
{
string Drive();
}
interface IGolfPlayer
{
string Drive();
}
[Link]([Link]()); // Vroom!
[Link]([Link]()); // Vroom!
[Link]([Link]()); // Took a swing...
The implementation cannot be called from anywhere else except by using the interface:
Due to this, it may be advantageous to put complex implementation code of an explicitly implemented interface in
a separate, private method.
An explicit interface implementation can of course only be used for methods that actually exist for that interface:
Similarly, using an explicit interface implementation without declaring that interface on the class causes an error,
too.
Hint:
Implementing interfaces explicitly can also be used to avoid dead code. When a method is no longer needed and
gets removed from the interface, the compiler will complain about each still existing implementation.
Note:
Programmers expect the contract to be the same regardless of the context of the type and explicit implementation
should not expose different behavior when called. So unlike the example above, [Link] and Drive
should do the same thing when possible.
Can't be instantiated
Can't have any functionality
Can only contain methods * (Properties and Events are methods internally)
Inheriting an interface is called "Implementing"
You can inherit from 1 class, but you can "Implement" multiple Interfaces
Things to notice:
// ok
[Link]();
// ok
[Link] = 5;
// in order to access the property in the class you must "down cast" it
((MyClass)obj).SomeValueNotImplemtingAnything = 5; // ok
This is especially useful when you're working with UI frameworks such as WinForms or WPF because it's mandatory
to inherit from a base class to create user control and you loose the ability to create abstraction over different
control types. An example? Coming up:
The problem proposed is that both contain some concept of "Text" but the property names differ. And you can't
create create a abstract base class because they have a mandatory inheritance to 2 different classes. An interface
can alleviate that
// Runtime Error because 1 class is in fact not a button which makes this cast invalid
((MyButton)ctrl).Clicks = 0;
Let's say that in a program for a online store, we have a variety of items you can buy. Each item has a name, an ID
number, and a price.
public string name; // though public variables are generally bad practice,
public int idNumber; // to keep this example simple we will use them instead
public decimal price; // of a property.
We have our Items stored inside of a List<Item>, and in our program somewhere, we want to sort our list by ID
number from smallest to largest. Instead of writing our own sorting algorithm, we can instead use the Sort()
method that List<T> already has. However, as our Item class is right now, there is no way for the List<T> to
understand what order to sort the list. Here is where the IComparable interface comes in.
To correctly implement the CompareTo method, CompareTo should return a positive number if the parameter is "less
than" the current one, zero if they are equal, and a negative number if the parameter is "greater than".
[Link]([Link](banana)); // 11
[Link]([Link](cow)); // 0
[Link]([Link](diamond)); // -3
Now, when we call Sort() on a List<Item> object, the List will automatically call the Item's CompareTo method
when it needs to determine what order to put objects in. Furthermore, besides List<T>, any other objects that
need the ability to compare two objects will work with the Item because we have defined the ability for two
different Items to be compared with one another.
Let's say that we define an interface IShape to represent different type of shapes, we expect a shape to have an
area, so we will define a method to force the interface implementations to return their area :
Let's that we have the following two shapes : a Rectangle and a Circle
Each one of them have its own definition of its area, but both of them are shapes. So it's only logical to see them as
IShape in our program :
[Link]();
}
// Output:
// Area : 50.00
// Area : 78.54
Answer: I don't. So neither should it be declared public but simply declaring the members as private will make the
compiler throw an error
void [Link]() {
So now you have implemented the members as required and they won't expose any members in as public.
If you seriously still want to access the member even though is explicitly implement all you have to do is cast the
object to the interface and you good to go.
((IMessageService)obj).OnMessageRecieve();
1. You cannot create an instance of a static class (this even removes the default constructor)
2. All properties and methods in the class must be static as well.
3. A static class is a sealed class, meaning it cannot be inherited.
void Main()
{
[Link]("Static classes are lazily initialized");
[Link]("The static constructor is only invoked when the class is first accessed");
[Link]();
1. This value does not change from object to object but rather changes on a class as a whole
2. Static properties and methods don't require an instance.
//Notice this next call doesn't access the instance but calls by the class name.
[Link]([Link]); //this will also print "1"
}
}
This implementation is thread-safe because in this case instance object is initialized in the static constructor. The
CLR already ensures that all static constructors are executed thread-safe.
Mutating instance is not a thread-safe operation, therefore the readonly attribute guarantees immutability after
initialization.
private LazySingleton() { }
}
Using Lazy<T> will make sure that the object is only instantiated when it is used somewhere in the calling code.
using System;
private ThreadSafeSingleton()
{
}
return instance;
}
}
}
Notice that the if (instance == null) check is done twice: once before the lock is acquired, and once afterwards.
This implementation would still be thread-safe even without the first null check. However, that would mean that a
lock would be acquired every time the instance is requested, and that would cause performance to suffer. The first
null check is added so that the lock is not acquired unless it's necessary. The second null check makes sure that
only the first thread to acquire the lock then creates the instance. The other threads will find the instance to be
populated and skip ahead.
Because the Nested class is nested and private the instantiation of the singleton instance will not be triggered by
accessing other members of the Sigleton class (such as a public readonly property, for example).
public AnimalController(){
[Link]("");
}
}
We look at this code and we think everything is ok but now our AnimalController is reliant on object
_SantaAndHisReindeer. Automatically my Controller is bad to testing and reusability of my code will be very hard.
Very good explanation why we should use Depedency Injection and interfaces here.
If we want Unity to handle DI, the road to achieve this is very simple :) With NuGet( package manager) we can easily
import unity to our code.
in Visual Studio Tools -> NuGet Package Manager -> Manage Packages for Solution -> in search input write
unity -> choose our project-> click install
UnityConfig - in RegisterTypes method, we can see type that will be injection in our constructors.
namespace [Link].App_Start
{
/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return [Link];
[Link]<ISanta, SantaAndHisReindeer>();
}
}
}
UnityMvcActivator - > also with nice comments which say that this class integrates Unity with [Link]
MVC
using [Link];
using [Link];
using [Link];
[assembly:
[Link](typeof([Link].App_Start.UnityWebActivator), "Start")]
[assembly:
[Link](typeof([Link].App_Start.UnityWebActivator),
"Shutdown")]
namespace [Link].App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with [Link] MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = [Link]();
[Link]([Link]<FilterAttributeFilterProvider>().
First());
[Link](new UnityFilterAttributeFilterProvider(container));
[Link](new UnityDependencyResolver(container));
_SantAndHisReindeer = SantaAndHisReindeer;
}
}
In [Link] we must add new line: [Link]() which will start, configure Unity and register our
types.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link].App_Start;
namespace [Link]
{
public class MvcApplication : [Link]
{
protected void Application_Start()
{
[Link]();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]();
}
}
}
[Export(typeof(ILogger))]
[Export(typeof(ILogger))]
[ExportMetadata("Name", "File")]
public class FileLogger:ILogger
{
public void Log(string message)
{
//Write the message to file
}
}
[ImportMany]
private IEnumerable<Lazy<ILogger, ILoggerMetaData>> _loggers;
using System;
namespace PartialClassAndMethods
{
public partial class PartialClass
{
public void ExampleMethod() {
[Link]("Method call from the first declaration.");
}
}
class Program
{
static void Main(string[] args)
{
PartialClass partial = new PartialClass();
[Link](); // outputs "Method call from the first declaration."
[Link](); // outputs "Method call from the second declaration."
}
}
}
// [Link]
public partial class PartialClass : BaseClass {}
// [Link]
public partial class PartialClass {}
You can specify the same base class in more than one partial class. It will get flagged as redundant by some IDE
tools, but it does compile correctly.
// [Link]
public partial class PartialClass : BaseClass {}
You cannot specify different base classes in multiple partial classes, it will result in a compiler error.
// [Link]
public partial class PartialClass : BaseClass {} // compiler error
// [Link]
public partial class PartialClass : OtherBaseClass {} // compiler error
using System;
namespace PartialClassAndMethods
{
public partial class PartialClass // Auto-generated
{
partial void PartialMethod();
}
class Program
{
static void Main(string[] args)
{
PartialClass partial = new PartialClass();
[Link](); // outputs "Partial method called."
}
}
}
Book theBook = new Book { Title = "Don Quixote", Author = "Miguel de Cervantes" };
This is equivalent to
var someBook = new Book(16) { Title = "Don Quixote", Author = "Miguel de Cervantes" }
This will first instantiate a Book with the Book(int) constructor, then set each property in the initializer. It is
equivalent to:
// Single argument
[Link]("Hello World");
// Multiple arguments
string name = "User";
[Link]("Hello, {0}!", name);
int x = 42;
// The instance method called here is [Link]()
string xAsString = [Link]();
class Program
{
static void Main(string[] args)
{
// C# 2.0 definition
IntOp add = delegate(int lhs, int rhs)
{
return lhs + rhs;
};
// C# 3.0 definition
IntOp mul = (lhs, rhs) =>
{
return lhs * rhs;
};
ReturnType can be void for no return or can be any type from the basic ones, as int to complex classes.
a Method may have some or no input parameters. to set parameters for a method, you should declare each one
like normal variable declarations (like int a), and for more than one parameter you should use comma between
them (like int a, int b).
Parameters may have default values. for this you should set a value for the parameter (like int a = 0). if a
parameter has a default value, setting the input value is optional.
Parameters can be used to pass values into a method, so that the method can work with them. This can be every
kind of work like printing the values, or making modifications to the object referenced by a parameter, or storing
the values.
When you call the method, you need to pass an actual value for every parameter. At this point, the values that you
actually pass to the method call are called Arguments:
If your method specifies a return value, the method must return a value. You do this using the return statement.
Once a return statement has been reached, it returns the specified value and any code after it will not be run
anymore (exceptions are finally blocks, which will still be executed before the method returns).
If your method returns nothing (void), you can still use the return statement without a value if you want to return
from the method immediately. At the end of such a method, a return statement would be unnecessary though.
return;
return 0;
return x * 2;
return [Link]();
Throwing an exception can end method execution without returning a value. Also, there are iterator blocks, where
return values are generated by using the yield keyword, but those are special cases that will not be explained at this
point.
When you call such a method and omit a parameter for which a default value is provided, the compiler inserts that
default value for you.
Keep in mind that parameters with default values need to be written after parameters without default values.
Factors affecting
Number of Arguments
Type of arguments
Return Type**
Consider a method named Area that will perform calculation functions, which will accepts various arguments and
return the result.
Example
This method will accepts one argument and return a string, if we call the method with an integer(say 5) the output
will be "Area of Square is 25".
Similarly if we pass two double values to this method the output will be the product of the two values and are of
type double. This can be used of multiplication as well as finding the Area of rectangles
This can be used specially for finding the area of circle, which will accepts a double value(radius) and return
another double value as its Area.
Each of these methods can be called normally without conflict - the compiler will examine the parameters of each
method call to determine which version of Area needs to be used.
**Note that return type alone cannot differentiate between two methods. For instance, if we had two definitions for
Area that had the same parameters, like so:
If we need to have our class use the same method names that return different values, we can remove the issues of
ambiguity by implementing an interface and explicitly defining its usage.
//protected internal: access is limited to the current assembly or types derived from the containing
class.
protected internal void MyMethod()
An extension method is created by adding a static method to a static class which is distinct from the original type
being extended. The static class holding the extension method is often created for the sole purpose of holding
extension methods.
Extension methods take a special first parameter that designates the original type being extended. This first
parameter is decorated with the keyword this (which constitutes a special and distinct use of this in C#—it should
be understood as different from the use of this which allows referring to members of the current object instance).
In the following example, the original type being extended is the class string. String has been extended by a
method Shorten(), which provides the additional functionality of shortening. The static class StringExtensions
has been created to hold the extension method. The extension method Shorten() shows that it is an extension of
string via the specially marked first parameter. To show that the Shorten() method extends string, the first
parameter is marked with this. Therefore, the full signature of the first parameter is this string text, where
string is the original type being extended and text is the chosen parameter name.
class Program
{
static void Main()
{
// This calls method [Link]()
var myString = "Hello World!".ToUpper();
"some string".Shorten(5);
Note that extension methods are only usable if they are in the same namespace as their definition, if the namespace is
imported explicitly by the code using the extension method, or if the extension class is namespace-less. The .NET
framework guidelines recommend putting extension classes in their own namespace. However, this may lead to
discovery issues.
This results in no conflicts between the extension methods and the libraries being used, unless namespaces which
might conflict are explicitly pulled in. For example LINQ Extensions:
using [Link]; // Allows use of extension methods from the [Link] namespace
class Program
{
static void Main()
{
var ints = new int[] {1, 2, 3, 4};
Since C# 6.0, it is also possible to put a using static directive to the class containing the extension methods. For
example, using static [Link];. This makes extension methods from that particular class
available without bringing other types from the same namespace into scope.
When a class method with the same signature is available, the compiler prioritizes it over the extension method
call. For example:
class Test
{
public void Hello()
{
[Link]("From Test");
}
}
Note that if there are two extension functions with the same signature, and one of them is in the same namespace,
then that one will be prioritized. On the other hand, if both of them are accessed by using, then a compile time
error will ensue with the message:
Note that the syntactic convenience of calling an extension method via [Link]()
is an optional convenience. The method can also be called in the traditional manner, so that the special first
parameter is used as a parameter to the method.
Usage:
There are still scenarios where you would need to use an extension method as a static method:
Resolving conflict with a member method. This can happen if a new version of a library introduces a new
member method with the same signature. In this case, the member method will be preferred by the
compiler.
Resolving conflicts with another extension method with the same signature. This can happen if two libraries
include similar extension methods and namespaces of both classes with extension methods are used in the
same file.
Passing extension method as a method group into delegate parameter.
Doing your own binding through Reflection.
Using the extension method in the Immediate window in Visual Studio.
Using static
If a using static directive is used to bring static members of a static class into global scope, extension methods
are skipped. Example:
If you remove the this modifier from the first argument of the Shorten method, the last line will compile.
View Demo
View Demo
You can also create extension methods for partially bound types in multi generic types:
View Demo
Calling code:
int number = 5;
var IsDefault = [Link]();
}
}
Extension methods are just a syntactic sugar, and are not actually members of the class they extend. This means
that they cannot break encapsulation—they only have access to public (or when implemented in the same
assembly, internal) fields, properties and methods.
void Main()
{
int result = [Link]().Decrement().Increment();
// result is now 6
}
void Main()
{
int[] ints = new[] { 1, 2, 3, 4, 5, 6};
int[] a = [Link]();
//a is { 2, 4, 6 };
int[] b = [Link]().WhereGreaterThan(2);
//b is { 4, 6 };
}
Now you can quickly convert your enum value to a different type. In this case a bool.
Also the dispatch based on static type does not allow an extension method to be called on a dynamic object:
use like:
// Prints True
[Link]([Link]());
[Link] = awesomeString;
// Prints True
[Link]([Link]([Link]));
The reason [calling extension methods from dynamic code] doesn't work is because in regular, non-
dynamic code extension methods work by doing a full search of all the classes known to the compiler for
a static class that has an extension method that matches. The search goes in order based on the
namespace nesting and available using directives in each namespace.
That means that in order to get a dynamic extension method invocation resolved correctly, somehow the
DLR has to know at runtime what all the namespace nestings and using directives were in your source
code. We do not have a mechanism handy for encoding all that information into the call site. We
considered inventing such a mechanism, but decided that it was too high cost and produced too much
schedule risk to be worth it.
Source
The following is a very simple interface with convenience overloads provided as extensions.
class Program
{
static void Main(string[] args)
{
var formatter = new SecondsTimeFormatter();
// Callers get two method overloads!
[Link]($"4500ms is rougly {[Link](4500)}");
var span = [Link](5);
[Link]($"{span} is formatted as {[Link](span)}");
}
}
By default the items are compared based on their order within the list and the items themselves, passing false to
the isOrdered parameter will compare only the items themselves regardless of their order.
For this method to work, the generic type (T) must override both Equals and GetHashCode methods.
Usage:
Method:
public static bool Compare<T>(this IList<T> list1, IList<T> list2, bool isOrdered = true)
{
if (list1 == null && list2 == null)
return true;
if (list1 == null || list2 == null || [Link] != [Link])
return false;
if (isOrdered)
{
for (int i = 0; i < [Link]; i++)
{
var l1 = list1[i];
var l2 = list2[i];
if (
(l1 == null && l2 != null) ||
(l1 != null && l2 == null) ||
())
{
This approach removes the need of using string literals as keys all over the codebase as well as the need of casting
to the required type during the read operation. Overall it creates a more secure, strongly typed way of interacting
with such loosely typed objects as Dictionaries.
The beauty here is all the mapping method have a common name (ToViewModel) and we can reuse it several ways
[Link](value);
}
[Link]("example", 5);
[Link]("example", 10);
[Link]("example", 15);
[Link]("example", 5);
[Link]("example", 10);
[Link]("example", 15);
[Link]([Link]("example")); // False
View Demo
using System;
using [Link];
namespace Samples
{
/// <summary>
/// Wraps a try catch statement as a static helper which uses
/// Extension methods for the exception
/// </summary>
public static class Bullet
{
/// <summary>
/// Wrapper for Try Catch Statement
/// </summary>
/// <param name="code">Call back for code</param>
/// <param name="error">Already handled and logged exception</param>
public static void Proof(Action code, Action<Exception> error)
{
try
{
code();
}
catch (Exception iox)
{
//extension method used here
[Link]("BP2200-ERR-Unexpected Error");
//callback, exception already handled and logged
error(iox);
}
}
/// <summary>
/// Example of a logging method helper, this is the extension method
/// </summary>
/// <param name="error">The Exception to log</param>
/// <param name="messageID">A unique error ID header</param>
public static void Log(this Exception error, string messageID)
{
[Link](messageID);
[Link]([Link]);
[Link]([Link]);
[Link]("");
}
}
/// <summary>
/// Shows how to use both the wrapper and extension methods.
/// </summary>
public class UseBulletProofing
/// <summary>
/// How to use Bullet Proofing in your code.
/// </summary>
/// <returns>A string</returns>
public string DoSomething()
{
string result = [Link];
//Note that the [Link] method forces this construct.
[Link](() =>
{
//this is the code callback
result = "DST5900-INF-No Exceptions in this code";
}, error =>
{
//error is the already logged and handled exception
//determine the base result
result = "DTS6200-ERR-An exception happened look at console log";
if ([Link]("SomeMarker"))
{
//filter the result for Something within the exception message
result = "DST6500-ERR-Some marker was found in the exception";
}
});
return result;
}
/// <summary>
/// Next step in workflow
/// </summary>
public void DoSomethingElse()
{
//Only called if no exception was thrown before
}
}
}
Sample Method:
Call Sample:
[Link] (Sample(left:"A",right:"B"));
[Link] (Sample(right:"A",left:"B"));
Results:
A-B
B-A
When you want to call this method without set retryCount argument :
class SmsUtil
{
public bool SendMessage(string from, string to, string message, int retryCount, object
attachment)
you can make this method call even more clear with named arguments:
Here we have set the value for width as optional and gave value as 56. If you note, the IntelliSense itself shows you
the optional argument as shown in the below image.
Note that we did not get any error while compiling and it will give you an output as follows.
Another way of implementing the optional argument is by using the [Optional] keyword. If you do not pass the
using [Link];
private static double FindAreaWithOptional(int length, [Optional]int width)
{
try
{
return (length * width);
}
catch (Exception)
{
throw new NotImplementedException();
}
}
And when we call the function, we get 0 because the second argument is not passed and the default value of int is 0
and so the product is 0.
FindArea(120, 56);
In this our first argument is length (ie 120) and second argument is width (ie 56). And we are calculating the area by
that function. And following is the function definition.
So in the first function call, we just passed the arguments by its position. Right?
double area;
[Link]("Area with positioned argument is: ");
area = FindArea(120, 56);
[Link](area);
[Link]();
Now if you run this program, you will get the same result. We can give the names vice versa in the method call if we
are using the named arguments.
One of the important use of a named argument is, when you use this in your program it improves the readability of
your code. It simply says what your argument is meant to be, or what it is?.
You can give the positional arguments too. That means, a combination of both positional argument and named
argument.
In the above example we passed 120 as the length and 56 as a named argument for the parameter width.
There are some limitations too. We will discuss the limitation of a named arguments now.
Named argument specification must appear after all fixed arguments have been specified.
Named argument specification must appear after all fixed arguments have been specified
Usage
Here is an example where two ValidationAttribute and one DisplayAttribute are used:
class Kid
{
[Range(0, 18)] // The age cannot be over 18 and cannot be negative
public int Age { get; set; }
[StringLength(MaximumLength = 50, MinimumLength = 3)] // The name cannot be under 3 chars or
more than 50 chars
public string Name { get; set; }
[DataType([Link])] // The birthday will be displayed as a date only (without the time)
public DateTime Birthday { get; set; }
}
Data annotations are mostly used in frameworks such as [Link]. For example, in [Link] MVC, when a model is
received by a controller method, [Link]() can be used to tell if the received model respects all its
ValidationAttribute. DisplayAttribute is also used in [Link] MVC to determine how to display values on a web
page.
if ()
{
isValid = [Link]() != "BANANA";
}
return isValid;
}
}
Validation Context
Any validation needs a context to give some information about what is being validated. This can include various
information such as the object to be validated, some properties, the name to display in the error message, etc.
Once the context is created, there are multiple ways of doing validation.
And More
Example: RequiredAttribute
When validated through the [Link] method, this attribute will return an error if the Name
property is null or contains only whitespace.
Example: StringLengthAttribute
The StringLengthAttribute validates if a string is less than the maximum length of a string. It can optionally
specify a minimum length. Both values are inclusive.
Example: RangeAttribute
The RangeAttribute gives the maximum and minimum value for a numeric field.
Example: CustomValidationAttribute
The CustomValidationAttribute class allows a custom static method to be invoked for validation. The custom
method must be static ValidationResult [MethodName] (object input).
Method declaration:
if (input?.ToString()?.ToUpperInvariant() == "APPLE")
{
result = new ValidationResult("Apples are not allowed.");
}
return result;
}
}
<Window x:Class="[Link]"
xmlns="[Link]
xmlns:x="[Link]
xmlns:wpfApplication="clr-namespace:WpfApplication"
Height="70" Width="360" Title="Display name example">
<[Link]>
<wpfApplication:EditableConverter x:Key="EditableConverter"/>
</[Link]>
<StackPanel Margin="5">
<!-- TextBox Text (FirstName property value) -->
<!-- TextBox IsEnabled (Editable attribute) -->
<TextBox Text="{Binding [Link], Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding Employee, Converter={StaticResource EditableConverter},
ConverterParameter=FirstName}"/>
</StackPanel>
</Window>
namespace WpfApplication
{
/// <summary>
/// Interaction logic for [Link]
/// </summary>
public partial class MainWindow : Window
{
private Employee _employee = new Employee() { FirstName = "This is not editable"};
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
namespace WpfApplication
{
public class EditableConverter : IValueConverter
Section 52.1: as
The as keyword is an operator similar to a cast. If a cast is not possible, using as produces null rather than
resulting in an InvalidCastException.
expression as type is equivalent to expression is type ? (type)expression : (type)null with the caveat that
as is only valid on reference conversions, nullable conversions, and boxing conversions. User-defined conversions
are not supported; a regular cast must be used instead.
For the expansion above, the compiler generates code such that expression will only be evaluated once and use
single dynamic type check (unlike the two in the sample above).
as can be useful when expecting an argument to facilitate several types. Specifically it grants the user multiple
options - rather than checking every possibility with is before casting, or just casting and catching exceptions. It is
best practice to use 'as' when casting/checking an object which will cause only one unboxing penalty. Using is to
check, then casting will cause two unboxing penalties.
If an argument is expected to be an instance of a specific type, a regular cast is preferred as its purpose is more
clear to the reader.
Because a call to as may produce null, always check the result to avoid a NullReferenceException.
Example usage
class MyCustomClass
{
goto as a:
Label:
void InfiniteHello()
{
sayHello:
[Link]("Hello!");
goto sayHello;
}
Case statement:
enum Permissions { Read, Write };
switch (GetRequestedPermission())
{
case [Link]:
GrantReadAccess();
break;
case [Link]:
GrantWriteAccess();
goto case [Link]; //People with write access also get read
}
This is particularly useful in executing multiple behaviors in a switch statement, as C# does not support fall-through
case blocks.
Exception Retry
var exCount = 0;
retry:
try
{
//Do work
}
catch (IOException)
{
exCount++;
if (exCount < 3)
{
[Link](100);
Similar to many languages, use of goto keyword is discouraged except the cases below.
Multi-level break. LINQ can often be used instead, but it usually has worse performance.
Resource deallocation when working with unwrapped low-level objects. In C#, low-level objects should
usually be wrapped in separate classes.
Finite state machines, for example, parsers; used internally by compiler generated async/await state
machines.
It is good practice to mark every variable that may be used by multiple threads as volatile to prevent unexpected
behavior due to behind-the-scenes optimizations. Consider the following code block:
/* the value of x will always be the current value, but y will always be "15" */
[Link]("x = " + x + ", y = " + y);
}
}
In the above code-block, the compiler reads the statements x = 5 and y = x + 10 and determines that the value of
y will always end up as 15. Thus, it will optimize the last statement as y = 15. However, the variable x is in fact a
public field and the value of x may be modified at runtime through a different thread acting on this field
separately. Now consider this modified code-block. Do note that the field x is now declared as volatile.
Now, the compiler looks for read usages of the field x and ensures that the current value of the field is always
retrieved. This ensures that even if multiple threads are reading and writing to this field, the current value of x is
always retrieved.
volatile can only be used on fields within classes or structs. The following is not valid:
Remarks:
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock
statement to serialize access.
The volatile keyword can be applied to fields of reference types
The volatile keyword will not make operating on 64-bit primitives on a 32-bit platform atomic. Interlocked
operations such as [Link] and [Link] must still be used for safe multi-threaded
access on these platforms.
When overflow occurs within a checked block (or when the compiler is set to globally use checked arithmetic), an
exception is thrown to warn of undesired behavior. Meanwhile, in an unchecked block, overflow is silent: no
exceptions are thrown, and the value will simply wrap around to the opposite boundary. This can lead to subtle,
hard to find bugs.
Since most arithmetic operations are done on values that are not large or small enough to overflow, most of the
time, there is no need to explicitly define a block as checked. Care needs to be taken when doing arithmetic on
unbounded input that may cause overflow, for example when doing arithmetic in recursive functions or while
taking user input.
When a block or expression is declared as unchecked, any arithmetic operations inside it are allowed to overflow
without causing an error. An example where this behavior is desired would be the calculation of a checksum, where
One of the most common uses for unchecked is implementing a custom override for [Link](), a type
of checksum. You can see the keyword's use in the answers to this question: What is the best algorithm for an
overridden [Link]?.
When a block or expression is declared as checked, any arithmetic operation that causes an overflow results in an
OverflowException being thrown.
Checked and unchecked blocks do not affect called methods, only operators called directly in the current method.
For example, [Link](), Convert.ToInt32(), and user-defined operators are not affected by custom
checked/unchecked contexts.
Note: The default overflow default behavior (checked vs. unchecked) may be changed in the Project Properties or
through the /checked[+|-] command line switch. It is common to default to checked operations for debug builds and
unchecked for release builds. The checked and unchecked keywords would then be used only where a default approach
does not apply and you need an explicit behavior to ensure correctness.
The virtual keyword allows a method, property, indexer or event to be overridden by derived classes and present
polymorphic behavior. (Members are non-virtual by default in C#)
In order to override a member, the override keyword is used in the derived classes. (Note the signature of the
members must be identical)
The polymorphic behavior of virtual members means that when invoked, the actual member being executed is
determined at runtime instead of at compile time. The overriding member in the most derived class the particular
object is an instance of will be the one executed.
In short, object can be declared of type BaseClass at compile time but if at runtime it is an instance of
DerivedClass then the overridden member will be executed:
new
Since only members defined as virtual are overridable and polymorphic, a derived class redefining a non virtual
member might lead to unexpected results.
When this happens, the member executed is always determined at compile time based on the type of the object.
If the object is declared of type BaseClass (even if at runtime is of a derived class) then the method of
BaseClass is executed
If the object is declared of type DerivedClass then the method of DerivedClass is executed.
This is usually an accident (When a member is added to the base type after an identical one was added to the