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

    https://goalkicker.comhttps://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: 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 String.IsNullOrEmpty() and String.IsNullOrWhiteSpace()

    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: String.Format 62 ...................................................................................................................................... Section 12.1: Since C# 6.0 62 ............................................................................................................................................ Section 12.2: Places where String.Format 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 String.Format() 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 System.Text.StringBuilder 68 ....................................................................... Section 13.3: Concat string array elements using String.Join 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: DateTime.AddDays(Double) 82 ................................................................................................................ Section 19.3: DateTime.AddHours(Double) 82 ............................................................................................................... Section 19.4: DateTime.Parse(String) 82 ........................................................................................................................ Section 19.5: DateTime.TryParse(String, DateTime) 82 ................................................................................................ Section 19.6: DateTime.AddMilliseconds(Double) 83 .................................................................................................... Section 19.7: DateTime.Compare(DateTime t1, DateTime t2 ) 83 ................................................................................ Section 19.8: DateTime.DaysInMonth(Int32,Int32) 83 ................................................................................................. Section 19.9: DateTime.AddYears(Int32) 84 ................................................................................................................... Section 19.10: Pure functions warning when dealing with DateTime 84 ..................................................................... Section 19.11: DateTime.TryParseExact(String,String,IFormatProvider,DateTimeStyles,DateTime)

    84 ................................................................................................................................................................................ Section 19.12: DateTime.Add(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: DateTime.ParseExact(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

  • 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 115 .......................................................................................................................................... Section 27.2: Dictionary 115 ................................................................................................................. Section 27.3: SortedSet 116 ....................................................................................................................................... Section 27.4: T[ ] (Array of T) 116 .................................................................................................................................. Section 27.5: List 117 .................................................................................................................................................. Section 27.6: Stack 117 .............................................................................................................................................. Section 27.7: LinkedList 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 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 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 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) 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 ASP.NET 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 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 System.Type 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 380 ..................................................................... Section 66.39: Select - Transforming elements 381 ..................................................................................................... Section 66.40: OrderByDescending 382 ......................................................................................................................... Sec


Top Related