c# notes for professionals - bandido.ch · 2018-04-19 · c# c# notes for professionals notes for...

807
C# Notes for Professionals C# Notes for Professionals GoalKicker.com Free Programming Books Disclaimer This is an unocial free book created for educational purposes and is not aliated with ocial C# group(s) or company(s). All trademarks and registered trademarks are the property of their respective owners 700+ pages of professional hints and tricks

Upload: others

Post on 01-Aug-2020

10 views

Category:

Documents


1 download

TRANSCRIPT

  • C#Notes for ProfessionalsC#

    Notes for Professionals

    GoalKicker.comFree Programming Books

    DisclaimerThis is an unocial free book created for educational purposes and is

    not aliated with ocial C# group(s) or company(s).All trademarks and registered trademarks are

    the property of their respective owners

    700+ pagesof professional hints and tricks

    http://goalkicker.comhttp://goalkicker.com

  • ContentsAbout 1 ................................................................................................................................................................................... Chapter 1: Getting started with C# Language 2 ...............................................................................................

    Section 1.1: Creating a new console application (Visual Studio) 2 ............................................................................... Section 1.2: Creating a new project in Visual Studio (console application) and Running it in Debug mode

    4 .................................................................................................................................................................................. Section 1.3: Creating a new program using .NET Core 7 .............................................................................................. Section 1.4: Creating a new program using Mono 9 ..................................................................................................... Section 1.5: Creating a new query using LinqPad 9 ...................................................................................................... Section 1.6: Creating a new project using Xamarin Studio 12 ......................................................................................

    Chapter 2: Verbatim Strings 18 ................................................................................................................................. Section 2.1: Interpolated Verbatim Strings 18 ............................................................................................................... Section 2.2: Escaping Double Quotes 18 ....................................................................................................................... Section 2.3: Verbatim strings instruct the compiler to not use character escapes 18 .............................................. Section 2.4: Multiline Strings 19 .......................................................................................................................................

    Chapter 3: Operators 20 ................................................................................................................................................ Section 3.1: Overloadable Operators 20 ......................................................................................................................... Section 3.2: Overloading equality operators 21 ............................................................................................................ Section 3.3: Relational Operators 22 .............................................................................................................................. Section 3.4: Implicit Cast and Explicit Cast Operators 24 ............................................................................................. Section 3.5: Short-circuiting Operators 25 ..................................................................................................................... Section 3.6: ? : Ternary Operator 26 ............................................................................................................................... Section 3.7: ?. (Null Conditional Operator) 27 ................................................................................................................ Section 3.8: "Exclusive or" Operator 27 .......................................................................................................................... Section 3.9: default Operator 27 ..................................................................................................................................... Section 3.10: Assignment operator '=' 28 ........................................................................................................................ Section 3.11: sizeof 28 ........................................................................................................................................................ Section 3.12: ?? Null-Coalescing Operator 29 ................................................................................................................ Section 3.13: Bit-Shifting Operators 29 ........................................................................................................................... Section 3.14: => Lambda operator 29 ............................................................................................................................. Section 3.15: Class Member Operators: Null Conditional Member Access 30 ............................................................ Section 3.16: Class Member Operators: Null Conditional Indexing 31 ......................................................................... Section 3.17: Postfix and Prefix increment and decrement 31 ..................................................................................... Section 3.18: typeof 31 ..................................................................................................................................................... Section 3.19: Binary operators with assignment 32 ...................................................................................................... Section 3.20: nameof Operator 32 ................................................................................................................................. Section 3.21: Class Member Operators: Member Access 33 ........................................................................................ Section 3.22: Class Member Operators: Function Invocation 33 ................................................................................. Section 3.23: Class Member Operators: Aggregate Object Indexing 33 ....................................................................

    Chapter 4: Extension Methods 34 ............................................................................................................................. Section 4.1: Extension methods - overview 34 ............................................................................................................... Section 4.2: Null checking 36 ........................................................................................................................................... Section 4.3: Explicitly using an extension method 37 .................................................................................................... Section 4.4: Generic Extension Methods 37 ................................................................................................................... Section 4.5: Extension methods can only see public (or internal) members of the extended class 39 .................. Section 4.6: Extension methods for chaining 39 ............................................................................................................ Section 4.7: Extension methods with Enumeration 40 .................................................................................................. Section 4.8: Extension methods dispatch based on static type 41 .............................................................................

  • Section 4.9: Extension methods on Interfaces 42 ......................................................................................................... Section 4.10: Extension methods in combination with interfaces 43 ........................................................................... Section 4.11: Extension methods aren't supported by dynamic code 43 .................................................................... Section 4.12: Extensions and interfaces together enable DRY code and mixin-like functionality 44 ...................... Section 4.13: IList Extension Method Example: Comparing 2 Lists 45 ................................................................... Section 4.14: Extension methods as strongly typed wrappers 46 ............................................................................... Section 4.15: Using Extension methods to create beautiful mapper classes 46 ........................................................ Section 4.16: Using Extension methods to build new collection types (e.g. DictList) 47 ............................................ Section 4.17: Extension methods for handling special cases 48 .................................................................................. Section 4.18: Using Extension methods with Static methods and Callbacks 49 .........................................................

    Chapter 5: Collection Initializers 51 ......................................................................................................................... Section 5.1: Collection initializers 51 ................................................................................................................................ Section 5.2: C# 6 Index Initializers 51 .............................................................................................................................. Section 5.3: Collection initializers in custom classes 52 ................................................................................................ Section 5.4: Using collection initializer inside object initializer 53 ................................................................................ Section 5.5: Collection Initializers with Parameter Arrays 54 .......................................................................................

    Chapter 6: String Interpolation 55 ........................................................................................................................... Section 6.1: Format dates in strings 55 ........................................................................................................................... Section 6.2: Padding the output 55 ................................................................................................................................. Section 6.3: Expressions 56 .............................................................................................................................................. Section 6.4: Formatting numbers in strings 56 .............................................................................................................. Section 6.5: Simple Usage 57 ..........................................................................................................................................

    Chapter 7: An overview of C# collections 58 ...................................................................................................... Section 7.1: HashSet 58 .............................................................................................................................................. Section 7.2: Dictionary 58 ..................................................................................................................... Section 7.3: SortedSet 59 ........................................................................................................................................... Section 7.4: T[ ] (Array of T) 59 ...................................................................................................................................... Section 7.5: List 60 ...................................................................................................................................................... Section 7.6: Stack 60 .................................................................................................................................................. Section 7.7: LinkedList 60 ........................................................................................................................................... Section 7.8: Queue 61 .......................................................................................................................................................

    Chapter 8: Constructors and Finalizers 62 ........................................................................................................... Section 8.1: Static constructor 62 .................................................................................................................................... Section 8.2: Singleton constructor pattern 63 ............................................................................................................... Section 8.3: Default Constructor 63 ................................................................................................................................ Section 8.4: Forcing a static constructor to be called 64 ............................................................................................. Section 8.5: Calling a constructor from another constructor 65 ................................................................................. Section 8.6: Calling the base class constructor 65 ........................................................................................................ Section 8.7: Finalizers on derived classes 66 ................................................................................................................. Section 8.8: Exceptions in static constructors 66 .......................................................................................................... Section 8.9: Constructor and Property Initialization 67 ................................................................................................ Section 8.10: Generic Static Constructors 69 ................................................................................................................. Section 8.11: Calling virtual methods in constructor 69 .................................................................................................

    Chapter 9: Static Classes 71 ........................................................................................................................................ Section 9.1: Static Classes 71 ........................................................................................................................................... Section 9.2: Static class lifetime 71 ................................................................................................................................. Section 9.3: Static keyword 72 ........................................................................................................................................

    Chapter 10: Keywords 73 ............................................................................................................................................... Section 10.1: as 73 ............................................................................................................................................................. Section 10.2: goto 74 ........................................................................................................................................................

  • Section 10.3: volatile 75 .................................................................................................................................................... Section 10.4: checked, unchecked 76 .............................................................................................................................. Section 10.5: virtual, override, new 77 ............................................................................................................................. Section 10.6: stackalloc 80 ............................................................................................................................................... Section 10.7: break 81 ....................................................................................................................................................... Section 10.8: const 83 ....................................................................................................................................................... Section 10.9: async, await 84 ........................................................................................................................................... Section 10.10: for 85 .......................................................................................................................................................... Section 10.11: abstract 86 .................................................................................................................................................. Section 10.12: fixed 87 ....................................................................................................................................................... Section 10.13: default 88 ................................................................................................................................................... Section 10.14: sealed 89 .................................................................................................................................................... Section 10.15: is 89 ............................................................................................................................................................. Section 10.16: this 90 ......................................................................................................................................................... Section 10.17: readonly 91 ................................................................................................................................................ Section 10.18: typeof 92 .................................................................................................................................................... Section 10.19: foreach 92 .................................................................................................................................................. Section 10.20: dynamic 93 ............................................................................................................................................... Section 10.21: try, catch, finally, throw 94 ....................................................................................................................... Section 10.22: void 95 ....................................................................................................................................................... Section 10.23: namespace 95 .......................................................................................................................................... Section 10.24: ref, out 96 .................................................................................................................................................. Section 10.25: base 97 ...................................................................................................................................................... Section 10.26: float, double, decimal 99 ......................................................................................................................... Section 10.27: operator 100 ............................................................................................................................................. Section 10.28: char 101 .................................................................................................................................................... Section 10.29: params 101 ............................................................................................................................................... Section 10.30: while 102 .................................................................................................................................................... Section 10.31: null 104 ....................................................................................................................................................... Section 10.32: continue 105 .............................................................................................................................................. Section 10.33: string 105 ................................................................................................................................................... Section 10.34: return 106 .................................................................................................................................................. Section 10.35: unsafe 106 ................................................................................................................................................. Section 10.36: switch 108 .................................................................................................................................................. Section 10.37: var 109 ....................................................................................................................................................... Section 10.38: when 110 ................................................................................................................................................... Section 10.39: lock 111 ..................................................................................................................................................... Section 10.40: uint 112 ...................................................................................................................................................... Section 10.41: if, if...else, if... else if 112 ............................................................................................................................. Section 10.42: static 113 ................................................................................................................................................... Section 10.43: internal 115 ............................................................................................................................................... Section 10.44: using 116 ................................................................................................................................................... Section 10.45: where 116 .................................................................................................................................................. Section 10.46: int 118 ........................................................................................................................................................ Section 10.47: ulong 119 ................................................................................................................................................... Section 10.48: true, false 119 ........................................................................................................................................... Section 10.49: struct 119 .................................................................................................................................................. Section 10.50: extern 120 ................................................................................................................................................. Section 10.51: bool 121 ...................................................................................................................................................... Section 10.52: interface 121 ............................................................................................................................................. Section 10.53: delegate 122 .............................................................................................................................................

  • Section 10.54: unchecked 122 ......................................................................................................................................... Section 10.55: ushort 123 ................................................................................................................................................. Section 10.56: sizeof 123 .................................................................................................................................................. Section 10.57: in 123 ......................................................................................................................................................... Section 10.58: implicit 124 ................................................................................................................................................ Section 10.59: do 124 ........................................................................................................................................................ Section 10.60: long 125 ..................................................................................................................................................... Section 10.61: enum 125 ................................................................................................................................................... Section 10.62: partial 126 ................................................................................................................................................. Section 10.63: event 127 ................................................................................................................................................... Section 10.64: sbyte 128 ...................................................................................................................................................

    Chapter 11: Object Oriented Programming In C# 129 ..................................................................................... Section 11.1: Classes: 129 ..................................................................................................................................................

    Chapter 12: Inheritance 130 .......................................................................................................................................... Section 12.1: Inheritance. Constructors' calls sequence 130 ......................................................................................... Section 12.2: Inheriting from a base class 132 .............................................................................................................. Section 12.3: Inheriting from a class and implementing an interface 133 .................................................................. Section 12.4: Inheriting from a class and implementing multiple interfaces 133 ...................................................... Section 12.5: Constructors In A Subclass 134 ................................................................................................................. Section 12.6: Inheritance Anti-patterns 134 ................................................................................................................... Section 12.7: Extending an abstract base class 135 ...................................................................................................... Section 12.8: Testing and navigating inheritance 136 .................................................................................................. Section 12.9: Inheriting methods 136 .............................................................................................................................. Section 12.10: Base class with recursive type specification 137 ...................................................................................

    Chapter 13: Generics 140 ................................................................................................................................................ Section 13.1: Implicit type inference (methods) 140 ...................................................................................................... Section 13.2: Type inference (classes) 141 .................................................................................................................... Section 13.3: Using generic method with an interface as a constraint type 141 ....................................................... Section 13.4: Type constraints (new-keyword) 142 ...................................................................................................... Section 13.5: Type constraints (classes and interfaces) 143 ........................................................................................ Section 13.6: Checking equality of generic values 144 ................................................................................................. Section 13.7: Reflecting on type parameters 145 .......................................................................................................... Section 13.8: Covariance 145 ........................................................................................................................................... Section 13.9: Contravariance 146 .................................................................................................................................... Section 13.10: Invariance 147 ........................................................................................................................................... Section 13.11: Variant interfaces 147 ............................................................................................................................... Section 13.12: Variant delegates 148 ............................................................................................................................... Section 13.13: Variant types as parameters and return values 149 ............................................................................ Section 13.14: Type Parameters (Interfaces) 149 .......................................................................................................... Section 13.15: Type constraints (class and struct) 150 ................................................................................................. Section 13.16: Explicit type parameters 150 ................................................................................................................... Section 13.17: Type Parameters (Classes) 150 ............................................................................................................... Section 13.18: Type Parameters (Methods) 151 ............................................................................................................. Section 13.19: Generic type casting 151 .......................................................................................................................... Section 13.20: Configuration reader with generic type casting 152 ............................................................................

    Chapter 14: Using Statement 154 ............................................................................................................................. Section 14.1: Using Statement Basics 154 ....................................................................................................................... Section 14.2: Gotcha: returning the resource which you are disposing 155 ............................................................... Section 14.3: Multiple using statements with one block 156 ......................................................................................... Section 14.4: Gotcha: Exception in Dispose method masking other errors in Using blocks 157 ..............................

  • Section 14.5: Using statements are null-safe 157 .......................................................................................................... Section 14.6: Using Dispose Syntax to define custom scope 157 ................................................................................ Section 14.7: Using Statements and Database Connections 158 ................................................................................ Section 14.8: Executing code in constraint context 160 ................................................................................................

    Chapter 15: Using Directive 161 ................................................................................................................................. Section 15.1: Associate an Alias to Resolve Conflicts 161 ............................................................................................. Section 15.2: Using alias directives 161 .......................................................................................................................... Section 15.3: Access Static Members of a Class 161 ..................................................................................................... Section 15.4: Basic Usage 162 ......................................................................................................................................... Section 15.5: Reference a Namespace 162 .................................................................................................................... Section 15.6: Associate an Alias with a Namespace 162 ..............................................................................................

    Chapter 16: Reflection 164 ............................................................................................................................................ Section 16.1: Get the members of a type 164 ................................................................................................................. Section 16.2: Get a method and invoke it 164 ................................................................................................................ Section 16.3: Creating an instance of a Type 165 ......................................................................................................... Section 16.4: Get a Strongly-Typed Delegate to a Method or Property via Reflection 168 ..................................... Section 16.5: Get a generic method and invoke it 169 .................................................................................................. Section 16.6: Get a System.Type 170 .............................................................................................................................. Section 16.7: Getting and setting properties 170 ........................................................................................................... Section 16.8: Create an instance of a Generic Type and invoke it's method 170 ...................................................... Section 16.9: Custom Attributes 170 ............................................................................................................................... Section 16.10: Instantiating classes that implement an interface (e.g. plugin activation) 172 .................................. Section 16.11: Get a Type by name with namespace 172 ............................................................................................. Section 16.12: Determining generic arguments of instances of generic types 173 ................................................... Section 16.13: Looping through all the properties of a class 174 .................................................................................

    Chapter 17: Null-Coalescing Operator 175 ........................................................................................................... Section 17.1: Basic usage 175 ........................................................................................................................................... Section 17.2: Null fall-through and chaining 175 ........................................................................................................... Section 17.3: Null coalescing operator with method calls 176 ..................................................................................... Section 17.4: Use existing or create new 177 ................................................................................................................. Section 17.5: Lazy properties initialization with null coalescing operator 177 ............................................................

    Chapter 18: Getting Started: Json with C# 178 .................................................................................................. Section 18.1: Simple Json Example 178 .......................................................................................................................... Section 18.2: First things First: Library to work with Json 178 ..................................................................................... Section 18.3: C# Implementation 178 ............................................................................................................................. Section 18.4: Serialization 179 .......................................................................................................................................... Section 18.5: Deserialization 179 ..................................................................................................................................... Section 18.6: Serialization & De-Serialization Common Utilities function 179 ............................................................

    Chapter 19: LINQ Queries 181 ...................................................................................................................................... Section 19.1: Chaining methods 181 ................................................................................................................................ Section 19.2: First, FirstOrDefault, Last, LastOrDefault, Single, and SingleOrDefault 182 ......................................... Section 19.3: Except 185 ................................................................................................................................................... Section 19.4: SelectMany 187 .......................................................................................................................................... Section 19.5: Any 188 ........................................................................................................................................................ Section 19.6: JOINS 188 .................................................................................................................................................... Section 19.7: Skip and Take 191 ...................................................................................................................................... Section 19.8: Defining a variable inside a Linq query (let keyword) 191 .................................................................... Section 19.9: Zip 192 ......................................................................................................................................................... Section 19.10: Range and Repeat 192 ............................................................................................................................ Section 19.11: Basics 193 ...................................................................................................................................................

  • Section 19.12: All 193 ......................................................................................................................................................... Section 19.13: Aggregate 194 ........................................................................................................................................... Section 19.14: Distinct 195 ................................................................................................................................................. Section 19.15: SelectMany: Flattening a sequence of sequences 195 ......................................................................... Section 19.16: GroupBy 197 .............................................................................................................................................. Section 19.17: Query collection by type / cast elements to type 198 .......................................................................... Section 19.18: Enumerating the Enumerable 198 .......................................................................................................... Section 19.19: Using Range with various Linq methods 200 ......................................................................................... Section 19.20: Where 201 ................................................................................................................................................. Section 19.21: Using SelectMany instead of nested loops 201 ..................................................................................... Section 19.22: Contains 201 ............................................................................................................................................. Section 19.23: Joining multiple sequences 202 .............................................................................................................. Section 19.24: Joining on multiple keys 205 .................................................................................................................. Section 19.25: ToLookup 205 ........................................................................................................................................... Section 19.26: SkipWhile 205 ............................................................................................................................................ Section 19.27: Query Ordering - OrderBy() ThenBy() OrderByDescending() ThenByDescending() 206 ............... Section 19.28: Sum 207 ..................................................................................................................................................... Section 19.29: GroupBy one or multiple fields 207 ........................................................................................................ Section 19.30: OrderBy 208 .............................................................................................................................................. Section 19.31: Any and First(OrDefault) - best practice 208 ......................................................................................... Section 19.32: GroupBy Sum and Count 209 ................................................................................................................. Section 19.33: SequenceEqual 210 .................................................................................................................................. Section 19.34: ElementAt and ElementAtOrDefault 210 ............................................................................................... Section 19.35: DefaultIfEmpty 211 .................................................................................................................................. Section 19.36: ToDictionary 212 ...................................................................................................................................... Section 19.37: Concat 213 ................................................................................................................................................ Section 19.38: Build your own Linq operators for IEnumerable 213 ..................................................................... Section 19.39: Select - Transforming elements 214 ...................................................................................................... Section 19.40: OrderByDescending 214 ......................................................................................................................... Section 19.41: Union 215 ................................................................................................................................................... Section 19.42: GroupJoin with outer range variable 215 .............................................................................................. Section 19.43: Linq Quantifiers 216 ................................................................................................................................. Section 19.44: TakeWhile 216 .......................................................................................................................................... Section 19.45: Reverse 216 .............................................................................................................................................. Section 19.46: Count and LongCount 217 ...................................................................................................................... Section 19.47: Incrementally building a query 218 ........................................................................................................ Section 19.48: Select with Func selector - Use to get ranking of elements 219 ..................

    Chapter 20: C# 7.0 Features 221 ............................................................................................................................... Section 20.1: Language support for Tuples 221 ............................................................................................................ Section 20.2: Local functions 225 ................................................................................................................................... Section 20.3: out var declaration 226 ............................................................................................................................. Section 20.4: Pattern Matching 227 ................................................................................................................................ Section 20.5: Digit separators 229 .................................................................................................................................. Section 20.6: Binary literals 229 ...................................................................................................................................... Section 20.7: throw expressions 230 .............................................................................................................................. Section 20.8: Extended expression bodied members list 231 ...................................................................................... Section 20.9: ref return and ref local 232 ...................................................................................................................... Section 20.10: ValueTask 233 ....................................................................................................................................

    Chapter 21: C# 6.0 Features 235 ................................................................................................................................ Section 21.1: Exception filters 235 ....................................................................................................................................

  • Section 21.2: String interpolation 239 ............................................................................................................................. Section 21.3: Auto-property initializers 245 .................................................................................................................... Section 21.4: Null propagation 247 ................................................................................................................................. Section 21.5: Expression-bodied function members 250 .............................................................................................. Section 21.6: Operator nameof 252 ................................................................................................................................ Section 21.7: Using static type 254 .................................................................................................................................. Section 21.8: Index initializers 254 ................................................................................................................................... Section 21.9: Improved overload resolution 256 ........................................................................................................... Section 21.10: Await in catch and finally 257 .................................................................................................................. Section 21.11: Minor changes and bugfixes 258 ............................................................................................................. Section 21.12: Using an extension method for collection initialization 259 ................................................................. Section 21.13: Disable Warnings Enhancements 260 ....................................................................................................

    Chapter 22: C# 5.0 Features 261 ............................................................................................................................... Section 22.1: Async & Await 261 ...................................................................................................................................... Section 22.2: Caller Information Attributes 262 ............................................................................................................

    Chapter 23: C# 4.0 Features 263 ............................................................................................................................... Section 23.1: Optional parameters and named arguments 263 .................................................................................. Section 23.2: Variance 264 .............................................................................................................................................. Section 23.3: Dynamic member lookup 264 .................................................................................................................. Section 23.4: Optional ref keyword when using COM 265 ...........................................................................................

    Chapter 24: C# 3.0 Features 266 ............................................................................................................................... Section 24.1: Implicitly typed variables (var) 266 .......................................................................................................... Section 24.2: Language Integrated Queries (LINQ) 266 .............................................................................................. Section 24.3: Lambda expresions 267 ............................................................................................................................ Section 24.4: Anonymous types 267 ..............................................................................................................................

    Chapter 25: String Escape Sequences 269 ........................................................................................................... Section 25.1: Escaping special symbols in string literals 269 ....................................................................................... Section 25.2: Unicode character escape sequences 269 ............................................................................................. Section 25.3: Escaping special symbols in character literals 269 ............................................................................... Section 25.4: Using escape sequences in identifiers 269 ............................................................................................. Section 25.5: Unrecognized escape sequences produce compile-time errors 270 ..................................................

    Chapter 26: Exception Handling 271 ........................................................................................................................ Section 26.1: Creating Custom Exceptions 271 .............................................................................................................. Section 26.2: Finally block 273 ........................................................................................................................................ Section 26.3: Best Practices 274 ..................................................................................................................................... Section 26.4: Exception Anti-patterns 276 ..................................................................................................................... Section 26.5: Basic Exception Handling 278 .................................................................................................................. Section 26.6: Handling specific exception types 278 .................................................................................................... Section 26.7: Aggregate exceptions / multiple exceptions from one method 279 ................................................... Section 26.8: Throwing an exception 280 ...................................................................................................................... Section 26.9: Unhandled and Thread Exception 280 .................................................................................................... Section 26.10: Implementing IErrorHandler for WCF Services 281 ............................................................................. Section 26.11: Using the exception object 283 ................................................................................................................ Section 26.12: Nesting of Exceptions & try catch blocks 285 .......................................................................................

    Chapter 27: Null-conditional Operators 286 ....................................................................................................... Section 27.1: Null-Conditional Operator 286 .................................................................................................................. Section 27.2: The Null-Conditional Index 286 ................................................................................................................ Section 27.3: Avoiding NullReferenceExceptions 287 ................................................................................................... Section 27.4: Null-conditional Operator can be used with Extension Method 287 ....................................................

  • Chapter 28: Built-in Types 289 .................................................................................................................................... Section 28.1: Conversion of boxed value types 289 ...................................................................................................... Section 28.2: Comparisons with boxed value types 289 .............................................................................................. Section 28.3: Immutable reference type - string 289 ................................................................................................... Section 28.4: Value type - char 290 ................................................................................................................................ Section 28.5: Value type - short, int, long (signed 16 bit, 32 bit, 64 bit integers) 290 ................................................. Section 28.6: Value type - ushort, uint, ulong (unsigned 16 bit, 32 bit, 64 bit integers) 290 ...................................... Section 28.7: Value type - bool 291 ................................................................................................................................

    Chapter 29: Lambda expressions 292 ..................................................................................................................... Section 29.1: Lambda Expressions as Shorthand for Delegate Initialization 292 ...................................................... Section 29.2: Lambda Expression as an Event Handler 292 ........................................................................................ Section 29.3: Lambda Expressions with Multiple Parameters or No Parameters 293 .............................................. Section 29.4: Lambdas can be emitted both as `Func` and `Expression` 293 ............................................................ Section 29.5: Put Multiple Statements in a Statement Lambda 293 ........................................................................... Section 29.6: Lambdas for both `Func` and `Action` 294 .............................................................................................. Section 29.7: Passing a Lambda Expression as a Parameter to a Method 294 ........................................................

    Chapter 30: Async-Await 295 ....................................................................................................................................... Section 30.1: Await operator and async keyword 295 .................................................................................................. Section 30.2: Concurrent calls 296 .................................................................................................................................. Section 30.3: Try/Catch/Finally 297 ............................................................................................................................... Section 30.4: Returning a Task without await 298 ........................................................................................................ Section 30.5: Async/await will only improve performance if it allows the machine to do additional work

    298 ............................................................................................................................................................................. Section 30.6: Web.config setup to target 4.5 for correct async behaviour 299 ........................................................ Section 30.7: Simple consecutive calls 299 .................................................................................................................... Section 30.8: Blocking on async code can cause deadlocks 300 ...............................................................................

    Chapter 31: Properties 302 ............................................................................................................................................ Section 31.1: Auto-implemented properties 302 ............................................................................................................ Section 31.2: Default Values for Properties 302 ............................................................................................................. Section 31.3: Public Get 303 ............................................................................................................................................. Section 31.4: Public Set 303 .............................................................................................................................................. Section 31.5: Accessing Properties 303 ........................................................................................................................... Section 31.6: Read-only properties 305 .......................................................................................................................... Section 31.7: Various Properties in Context 305 .............................................................................................................

    Chapter 32: Threading 307 ........................................................................................................................................... Section 32.1: Avoiding Reading and Writing Data Simultaneously 307 ...................................................................... Section 32.2: Creating and Starting a Second Thread 308 .......................................................................................... Section 32.3: Parallel.ForEach Loop 308 ........................................................................................................................ Section 32.4: Deadlocks (hold resource and wait) 309 ................................................................................................ Section 32.5: Simple Complete Threading Demo 311 .................................................................................................. Section 32.6: Creating One Thread Per Processor 312 ................................................................................................ Section 32.7: Simple Complete Threading Demo using Tasks 312 ............................................................................. Section 32.8: Deadlocks (two threads waiting on each other) 313 ............................................................................. Section 32.9: Explicit Task Parallism 314 ........................................................................................................................ Section 32.10: Implicit Task Parallelism 314 ................................................................................................................... Section 32.11: Starting a thread with parameters 315 ..................................................................................................

    Chapter 33: Methods 316 ............................................................................................................................................... Section 33.1: Calling a Method 316 .................................................................................................................................. Section 33.2: Anonymous method 316 ........................................................................................................................... Section 33.3: Declaring a Method 317 ............................................................................................................................

  • Section 33.4: Parameters and Arguments 317 .............................................................................................................. Section 33.5: Return Types 317 ....................................................................................................................................... Section 33.6: Default Parameters 318 ............................................................................................................................ Section 33.7: Method overloading 319 ........................................................................................................................... Section 33.8: Access rights 320 .......................................................................................................................................

    Chapter 34: Yield Keyword 321 .................................................................................................................................. Section 34.1: Simple Usage 321 ....................................................................................................................................... Section 34.2: Correctly checking arguments 321 ......................................................................................................... Section 34.3: Early Termination 322 ............................................................................................................................... Section 34.4: More Pertinent Usage 323 ........................................................................................................................ Section 34.5: Lazy Evaluation 324 .................................................................................................................................. Section 34.6: Try...finally 325 ............................................................................................................................................ Section 34.7: Eager evaluation 326 ................................................................................................................................. Section 34.8: Using yield to create an IEnumerator when implementing IEnumerable 326 ........................ Section 34.9: Lazy Evaluation Example: Fibonacci Numbers 327 ............................................................................... Section 34.10: The dierence between break and yield break 328 ............................................................................ Section 34.11: Return another Enumerable within a method returning Enumerable 329 ..........................................

    Chapter 35: Events 330 ................................................................................................................................................... Section 35.1: Declaring and Raising Events 330 ............................................................................................................ Section 35.2: Event Properties 331 ................................................................................................................................. Section 35.3: Creating cancelable event 332 ................................................................................................................. Section 35.4: Standard Event Declaration 333 .............................................................................................................. Section 35.5: Anonymous Event Handler Declaration 334 ........................................................................................... Section 35.6: Non-Standard Event Declaration 334 ..................................................................................................... Section 35.7: Creating custom EventArgs containing additional data 335 ................................................................

    Chapter 36: Common String Operations 337 ...................................................................................................... Section 36.1: Formatting a string 337 ............................................................................................................................. Section 36.2: Correctly reversing a string 337 ............................................................................................................... Section 36.3: Padding a string to a fixed length 338 .................................................................................................... Section 36.4: Getting x characters from the right side of a string 339 ....................................................................... Section 36.5: Checking for empty String using String.IsNullOrEmpty() and String.IsNullOrWhiteSpace()

    340 ............................................................................................................................................................................. Section 36.6: Trimming Unwanted Characters O the Start and/or End of Strings 341 ......................................... Section 36.7: Convert Decimal Number to Binary,Octal and Hexadecimal Format 341 .......................................... Section 36.8: Construct a string from Array 341 ........................................................................................................... Section 36.9: Formatting using ToString 342 ................................................................................................................. Section 36.10: Splitting a String by another string 343 ................................................................................................. Section 36.11: Splitting a String by specific character 343 ............................................................................................ Section 36.12: Getting Substrings of a given string 343 ................................................................................................ Section 36.13: Determine whether a string begins with a given sequence 343 .......................................................... Section 36.14: Getting a char at specific index and enumerating the string 343 ....................................................... Section 36.15: Joining an array of strings into a new one 344 .................................................................................... Section 36.16: Replacing a string within a string 344 .................................................................................................... Section 36.17: Changing the case of characters within a String 344 ........................................................................... Section 36.18: Concatenate an array of strings into a single string 345 .................................................................... Section 36.19: String Concatenation 345 ........................................................................................................................

    Chapter 37: Expression Trees 346 ............................................................................................................................. Section 37.1: Create Expression Trees with a lambda expression 346 ........................................................................ Section 37.2: Creating Expression Trees by Using the API 346 ................................................................................... Section 37.3: Compiling Expression Trees 346 ..............................................................................................................

  • Section 37.4: Parsing Expression Trees 347 ................................................................................................................... Section 37.5: Expression Tree Basic 347 ........................................................................................................................ Section 37.6: Examining the Structure of an Expression using Visitor 348 ................................................................. Section 37.7: Understanding the expressions API 348 ..................................................................................................

    Chapter 38: Overload Resolution 350 ..................................................................................................................... Section 38.1: Basic Overloading Example 350 ............................................................................................................... Section 38.2: "params" is not expanded, unless necessary 350 .................................................................................. Section 38.3: Passing null as one of the arguments 351 ..............................................................................................

    Chapter 39: String.Format 352 ................................................................................................................................... Section 39.1: Since C# 6.0 352 .......................................................................................................................................... Section 39.2: Places where String.Format is 'embedded' in the framework 352 ....................................................... Section 39.3: Create a custom format provider 352 ..................................................................................................... Section 39.4: Date Formatting 353 ................................................................................................................................. Section 39.5: Currency Formatting 354 .......................................................................................................................... Section 39.6: Using custom number format 355 ........................................................................................................... Section 39.7: Align left/ right, pad with spaces 355 ...................................................................................................... Section 39.8: Numeric formats 355 ................................................................................................................................ Section 39.9: ToString() 356 ............................................................................................................................................ Section 39.10: Escaping curly brackets inside a String.Format() expression 356 ...................................................... Section 39.11: Relationship with ToString() 357 ..............................................................................................................

    Chapter 40: nameof Operator 358 .......................................................................................................................... Section 40.1: Basic usage: Printing a variable name 358 ............................................................................................. Section 40.2: Raising PropertyChanged event 358 ...................................................................................................... Section 40.3: Argument Checking and Guard Clauses 359 .......................................................................................... Section 40.4: Strongly typed MVC action links 359 ...................................................................................................... Section 40.5: Handling PropertyChanged events 360 .................................................................................................. Section 40.6: Applied to a generic type parameter 360 .............................................................................................. Section 40.7: Printing a parameter name 361 ............................................................................................................... Section 40.8: Applied to qualified identifiers 361 ..........................................................................................................

    Chapter 41: Unsafe Code in .NET 362 ...................................................................................................................... Section 41.1: Using unsafe with arrays 362 .................................................................................................................... Section 41.2: Using unsafe with strings 362 ................................................................................................................... Section 41.3: Unsafe Array Index 363 .............................................................................................................................

    Chapter 42: Initializing Properties 364 ................................................................................................................... Section 42.1: C# 6.0: Initialize an Auto-Implemented Property 364 ............................................................................. Section 42.2: Initializing Property with a Backing Field 364 ......................................................................................... Section 42.3: Property Initialization during object instantiation 364 ........................................................................... Section 42.4: Initializing Property in Constructor 364 ...................................................................................................

    Chapter 43: BindingList 365 ................................................................................................................................. Section 43.1: Add item to list 365 ..................................................................................................................................... Section 43.2: Avoiding N*2 iteration 365 ........................................................................................................................

    Chapter 44: ILGenerator 366 ...................................................................................................................................... Section 44.1: Creates a DynamicAssembly that contains a UnixTimestamp helper method 366 ........................... Section 44.2: Create method override 367 ....................................................................................................................

    Chapter 45: Object initializers 369 ........................................................................................................................... Section 45.1: Simple usage 369 ....................................................................................................................................... Section 45.2: Usage with non-default constructors 369 .............................................................................................. Section 45.3: Usage with anonymous types 369 ..........................................................................................................

    Chapter 46: XML Documentation Comments 371 ............................................................................................

  • Section 46.1: Simple method annotation 371 ................................................................................................................. Section 46.2: Generating XML from documentation comments 371 .......................................................................... Section 46.3: Method documentation comment with param and returns elements 373 ......................................... Section 46.4: Interface and class documentation comments 373 .............................................................................. Section 46.5: Referencing another class in documentation 374 .................................................................................

    Chapter 47: Preprocessor directives 375 ............................................................................................................. Section 47.1: Conditional Expressions 375 ...................................................................................................................... Section 47.2: Other Compiler Instructions 375 .............................................................................................................. Section 47.3: Defining and Undefining Symbols 376 .................................................................................................... Section 47.4: Region Blocks 377 ..................................................................................................................................... Section 47.5: Disabling and Restoring Compiler Warnings 377 .................................................................................. Section 47.6: Generating Compiler Warnings and Errors 377 ..................................................................................... Section 47.7: Custom Preprocessors at project level 378 ............................................................................................ Section 47.8: Using the Conditional attribute 378 .........................................................................................................

    Chapter 48: Dynamic type 380 .................................................................................................................................. Section 48.1: Creating a dynamic object with properties 380 ...................................................................................... Section 48.2: Creating a dynamic variable 380 ............................................................................................................ Section 48.3: Returning dynamic 380 ............................................................................................................................. Section 48.4: Handling Specific Types Unknown at Compile Time 380 ......................................................................

    Chapter 49: Anonymous types 382 .......................................................................................................................... Section 49.1: Anonymous vs dynamic 382 ..................................................................................................................... Section 49.2: Creating an anonymous type 382 ........................................................................................................... Section 49.3: Anonymous type equality 382 ................................................................................................................. Section 49.4: Generic methods with anonymous types 383 ........................................................................................ Section 49.5: Instantiating generic types with anonymous types 383 ........................................................................ Section 49.6: Implicitly typed arrays 383 .......................................................................................................................

    Chapter 50: Structs 385 ................................................................................................................................................. Section 50.1: Declaring a struct 385 ................................................................................................................................ Section 50.2: Struct usage 386 ........................................................................................................................................ Section 50.3: Structs are copied on assignment 387 .................................................................................................... Section 50.4: Struct implementing interface 387 ..........................................................................................................

    Chapter 51: Tuples 388 .................................................................................................................................................... Section 51.1: Accessing tuple elements 388 .................................................................................................................... Section 51.2: Creating tuples 388 .................................................................................................................................... Section 51.3: Comparing and sorting Tuples 388 .......................................................................................................... Section 51.4: Return multiple values from a method 389 .............................................................................................

    Chapter 52: Enum 390 ..................................................................................................................................................... Section 52.1: Enum basics 390 ......................................................................................................................................... Section 52.2: Enum as flags 391 ..................................................................................................................................... Section 52.3: Using

  • Section 53.1: Access Modifiers Diagrams 398 ................................................................................................................ Section 53.2: public 399 ................................................................................................................................................... Section 53.3: private 399 .................................................................................................................................................. Section 53.4: protected internal 400 ............................................................................................................................... Section 53.5: internal 401 ................................................................................................................................................. Section 53.6: protected 402 .............................................................................................................................................

    Chapter 54: Task Parallel Library 403 ................................................................................................................... Section 54.1: Parallel.ForEach 403 ................................................................................................................................... Section 54.2: Parallel.For 403 .......................................................................................................................................... Section 54.3: Parallel.Invoke 404 .....................................................................................................................................

    Chapter 55: Attributes 405 ........................................................................................................................................... Section 55.1: Creating a custom attribute 405 ............................................................................................................... Section 55.2: Reading an attribute 405 .......................................................................................................................... Section 55.3: Using an attribute 406 ............................................................................................................................... Section 55.4: DebuggerDisplay Attribute 406 ............................................................................................................... Section 55.5: Caller info attributes 407 ........................................................................................................................... Section 55.6: Obsolete Attribute 408 .............................................................................................................................. Section 55.7: Reading an attribute from interface 408 ................................................................................................

    Chapter 56: Guid 410 ....................................................................................................................................................... Section 56.1: Getting the string representation of a Guid 410 ...................................................................................... Section 56.2: Creating a Guid 410 ................................................................................................................................... Section 56.3: Declaring a nullable GUID 410 .................................................................................................................

    Chapter 57: Singleton Implementation 412 ......................................................................................................... Section 57.1: Statically Initialized Singleton 412 ............................................................................................................. Section 57.2: Lazy, thread-safe Singleton (using Lazy) 412 .................................................................................. Section 57.3: Lazy, thread-safe Singleton (using Double Checked Locking) 412 ...................................................... Section 57.4: Lazy, thread safe singleton (for .NET 3.5 or older, alternate implementation) 413 ...........................

    Chapter 58: Delegates 415 ........................................................................................................................................... Section 58.1: Declaring a delegate type 415 ................................................................................................................. Section 58.2: The Func, Action and Predicate delegate types 416 ............................................ Section 58.3: Combine Delegates (Multicast Delegates) 417 ...................................................................................... Section 58.4: Safe invoke multicast delegate 418 ........................................................