Notes for Professionals C#

Programming · notes

Browse all programmation documents

C#

Notes for ProfessionalsC#

Notes for Professionals

700+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an unocial free book created for educational purposes and is

not aliated with ocial C# group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with C# Language

...............................................................................................

2

Section 1.1: Creating a new console application (Visual Studio)

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

...............................................................................

2

..................................................................................................................................................................................

4

Section 1.3: Creating a new program using .NET Core

Section 1.4: Creating a new program using Mono

Section 1.5: Creating a new query using LinqPad

Section 1.6: Creating a new project using Xamarin Studio

..............................................................................................

.....................................................................................................

......................................................................................................

......................................................................................

12

Chapter 2: Literals

......................................................................................................................................................

18

Section 2.1: uint literals

Section 2.2: int literals

Section 2.3: sbyte literals

Section 2.4: decimal literals

Section 2.5: double literals

Section 2.6: float literals

Section 2.7: long literals

Section 2.8: ulong literal

Section 2.9: string literals

Section 2.10: char literals

Section 2.11: byte literals

Section 2.12: short literal

Section 2.13: ushort literal

Section 2.14: bool literals

................................................................................................................................................

.................................................................................................................................................

............................................................................................................................................

........................................................................................................................................

18

..........................................................................................................................................

..............................................................................................................................................

..............................................................................................................................................

.............................................................................................................................................

............................................................................................................................................

............................................................................................................................................

.............................................................................................................................................

.............................................................................................................................................

...........................................................................................................................................

.............................................................................................................................................

Chapter 3: Operators

................................................................................................................................................

20

.........................................................................................................................

............................................................................................................

..............................................................................................................................

.............................................................................................

.....................................................................................................................

...............................................................................................................................

................................................................................................................

..........................................................................................................................

.....................................................................................................................................

........................................................................................................................

........................................................................................................................................................

................................................................................................................

...........................................................................................................................

.............................................................................................................................

Section 3.1: Overloadable Operators

Section 3.2: Overloading equality operators

Section 3.3: Relational Operators

Section 3.4: Implicit Cast and Explicit Cast Operators

Section 3.5: Short-circuiting Operators

Section 3.6: ? : Ternary Operator

Section 3.7: ?. (Null Conditional Operator)

Section 3.8: "Exclusive or" Operator

Section 3.9: default Operator

Section 3.10: Assignment operator '='

Section 3.11: sizeof

Section 3.12: ?? Null-Coalescing Operator

Section 3.13: Bit-Shifting Operators

Section 3.14: => Lambda operator

Section 3.15: Class Member Operators: Null Conditional Member Access

Section 3.16: Class Member Operators: Null Conditional Indexing

Section 3.17: Postfix and Prefix increment and decrement

Section 3.18: typeof

Section 3.19: Binary operators with assignment

Section 3.20: nameof Operator

Section 3.21: Class Member Operators: Member Access

Section 3.22: Class Member Operators: Function Invocation

.....................................................................................................................................................

......................................................................................................

.................................................................................................................................

........................................................................................

.................................................................................

............................................................

31

.........................................................................

.....................................................................................

7

9

9

18

18

18

18

18

18

18

19

19

19

19

19

19

20

21

22

Advertisement

24

25

26

27

27

28

28

28

29

29

29

31

31

32

32

32

33

33

Section 3.23: Class Member Operators: Aggregate Object Indexing

....................................................................

33

Chapter 4: Conditional Statements

..................................................................................................................

34

Section 4.1: If-Else Statement

Section 4.2: If statement conditions are standard boolean expressions and values

Section 4.3: If-Else If-Else Statement

.....................................................................................................................................

34

.........................................................................................................................

35

..........................................

34

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

Section 6.2: Default Equals behavior

Section 6.3: Override Equals and GetHashCode on custom types

Section 6.4: Equals and GetHashCode in IEqualityComparator

.................................................................................................

.........................................................................................................................

........................................................................

38

.............................................................................

39

Chapter 7: Null-Coalescing Operator

...............................................................................................................

41

..............................................................................................................................................

Section 7.1: Basic usage

Section 7.2: Null fall-through and chaining

Section 7.3: Null coalescing operator with method calls

Section 7.4: Use existing or create new

Section 7.5: Lazy properties initialization with null coalescing operator

..............................................................................................................

.........................................................................................

....................................................................................................................

...............................................................

43

Chapter 8: Null-conditional Operators

Section 8.1: Null-Conditional Operator

Section 8.2: The Null-Conditional Index

Section 8.3: Avoiding NullReferenceExceptions

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

Section 9.2: Raising PropertyChanged event

Section 9.3: Argument Checking and Guard Clauses

Section 9.4: Strongly typed MVC action links

Section 9.5: Handling PropertyChanged events

Section 9.6: Applied to a generic type parameter

Section 9.7: Printing a parameter name

Section 9.8: Applied to qualified identifiers

..................................................................................................

...........................................................................................................

..............................................................................................

48

...........................................................................................................

......................................................................................................

...................................................................................................

...................................................................................................................

...............................................................................................................

Chapter 10: Verbatim Strings

...............................................................................................................................

51

Section 10.1: Interpolated Verbatim Strings

Section 10.2: Escaping Double Quotes

Section 10.3: Verbatim strings instruct the compiler to not use character escapes

Section 10.4: Multiline Strings

..............................................................................................................

......................................................................................................................

.....................................................................................................................................

52

............................................

51

Chapter 11: Common String Operations

..........................................................................................................

53

.................................................................................................................................

Section 11.1: Formatting a string

Section 11.2: Correctly reversing a string

Section 11.3: Padding a string to a fixed length

Section 11.4: Getting x characters from the right side of a string

Section 11.5: Checking for empty String using String.IsNullOrEmpty() and String.IsNullOrWhiteSpace()

..................................................................................................................

........................................................................................................

...........................................................................

53

53

54

55

................................................................................................................................................................................

56

Section 11.6: Trimming Unwanted Characters O the Start and/or End of Strings

Section 11.7: Convert Decimal Number to Binary,Octal and Hexadecimal Format

Section 11.8: Construct a string from Array

Section 11.9: Formatting using ToString

Section 11.10: Splitting a String by another string

Advertisement

..............................................................................................................

....................................................................................................................

.....................................................................................................

.............................................

57

..............................................

57

57

58

59

37

37

41

41

42

43

44

44

44

45

47

47

48

49

49

50

50

51

51

Section 11.11: Splitting a String by specific character

Section 11.12: Getting Substrings of a given string

Section 11.13: Determine whether a string begins with a given sequence

Section 11.14: Getting a char at specific index and enumerating the string

Section 11.15: Joining an array of strings into a new one

Section 11.16: Replacing a string within a string

Section 11.17: Changing the case of characters within a String

Section 11.18: Concatenate an array of strings into a single string

Section 11.19: String Concatenation

................................................................................................

...................................................................................................

.............................................................

...........................................................

59

........................................................................................

........................................................................................................

..............................................................................

........................................................................

............................................................................................................................

......................................................................................................................................

............................................................................................................................................

Chapter 12: String.Format

Section 12.1: Since C# 6.0

Section 12.2: Places where String.Format is 'embedded' in the framework

Section 12.3: Create a custom format provider

Section 12.4: Date Formatting

Section 12.5: Currency Formatting

Section 12.6: Using custom number format

Section 12.7: Align left/ right, pad with spaces

Section 12.8: Numeric formats

Section 12.9: ToString()

Section 12.10: Escaping curly brackets inside a String.Format() expression

Section 12.11: Relationship with ToString()

.......................................................................................................

....................................................................................................................................

............................................................................................................................

..............................................................................................................

.........................................................................................................

...................................................................................................................................

...............................................................................................................................................

................................................................................................................

67

.........................................................

67

..........................................................

Chapter 13: String Concatenate

..........................................................................................................................

68

...............................................................................................................................................

68

Section 13.1: + Operator

Section 13.2: Concatenate strings using System.Text.StringBuilder

Section 13.3: Concat string array elements using String.Join

Section 13.4: Concatenation of two strings using $

.......................................................................

68

.................................................................................

.................................................................................................

Chapter 14: String Manipulation

.........................................................................................................................

70

Section 14.1: Replacing a string within a string

Section 14.2: Finding a string within a string

Section 14.3: Removing (Trimming) white-space from a string

Section 14.4: Splitting a string using a delimiter

Section 14.5: Concatenate an array of strings into a single string

Section 14.6: String Concatenation

Section 14.7: Changing the case of characters within a String

.........................................................................................................

.............................................................................................................

.............................................................................

.......................................................................................................

.........................................................................

71

............................................................................................................................

...............................................................................

.........................................................................................................................

.........................................................................................................................

...............................................................................................................................

Chapter 15: String Interpolation

Section 15.1: Format dates in strings

Section 15.2: Padding the output

Section 15.3: Expressions

Section 15.4: Formatting numbers in strings

Section 15.5: Simple Usage

.............................................................................................................................................

............................................................................................................

74

.........................................................................................................................................

75

Chapter 16: String Escape Sequences

..............................................................................................................

76

Section 16.1: Escaping special symbols in string literals

Section 16.2: Unicode character escape sequences

Section 16.3: Escaping special symbols in character literals

Section 16.4: Using escape sequences in identifiers

Section 16.5: Unrecognized escape sequences produce compile-time errors

..........................................................................................

................................................................................................

..................................................................................

................................................................................................

.....................................................

77

Advertisement

Chapter 17: StringBuilder

........................................................................................................................................

78

Section 17.1: What a StringBuilder is and when to use one

Section 17.2: Use StringBuilder to create string from a large number of records

.....................................................................................

78

...............................................

79

59

59

59

60

60

60

61

61

62

62

62

62

63

64

65

65

66

66

68

69

70

70

70

71

71

71

73

73

73

74

76

76

76

76

...........................................................................................................................

............................................................................................................................

...................................................................................................................................

.....................................................................................................................................

...........................................................................................................................................

Chapter 18: Regex Parsing

Section 18.1: Single match

Section 18.2: Multiple matches

Chapter 19: DateTime Methods

Section 19.1: DateTime Formatting

Section 19.2: DateTime.AddDays(Double)

Section 19.3: DateTime.AddHours(Double)

Section 19.4: DateTime.Parse(String)

Section 19.5: DateTime.TryParse(String, DateTime)

Section 19.6: DateTime.AddMilliseconds(Double)

Section 19.7: DateTime.Compare(DateTime t1, DateTime t2 )

Section 19.8: DateTime.DaysInMonth(Int32, Int32)

Section 19.9: DateTime.AddYears(Int32)

Section 19.10: Pure functions warning when dealing with DateTime

Section 19.11: DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)

................................................................................................

....................................................................................................

........................................................................................................................

...................................................................................................................

................................................................................................................

...............................................................................................................

.................................................................................................

................................................................................

.....................................................................

................................................................................................................................................................................

84

...................................................................................................................

Section 19.12: DateTime.Add(TimeSpan)

Section 19.13: Parse and TryParse with culture info

Section 19.14: DateTime as initializer in for-loop

Section 19.15: DateTime.ParseExact(String, String, IFormatProvider)

Section 19.16: DateTime ToString, ToShortDateString, ToLongDateString and ToString formatted

Section 19.17: Current Date

.........................................................................................................................................

88

......................................................................................................

.................................................................................................

.................................................................

.................

88

Chapter 20: Arrays

.....................................................................................................................................................

89

.........................................................

89

................................................................................................................................

89

Section 20.1: Declaring an array

Section 20.2: Initializing an array filled with a repeated non-default value

Section 20.3: Copying arrays

Section 20.4: Comparing arrays for equality

Section 20.5: Multi-dimensional arrays

Section 20.6: Getting and setting array values

Section 20.7: Iterate over an array

Section 20.8: Creating an array of sequential numbers

Section 20.9: Jagged arrays

Section 20.10: Array covariance

Section 20.11: Arrays as IEnumerable<> instances

Section 20.12: Checking if one array contains another array

.....................................................................................................................................

...........................................................................................................

.....................................................................................................................

........................................................................................................

............................................................................................................................

.........................................................................................

......................................................................................................................................

................................................................................................................................

...................................................................................................

................................................................................

80

80

80

81

81

82

82

82

82

83

83

83

84

84

86

86

Advertisement

87

87

90

90

91

91

91

92

92

94

94

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

Section 22.2: Enum as flags

Section 22.3: Using << notation for flags

Section 22.4: Test flags-style enum values with bitwise logic

Section 22.5: Add and remove values from flagged enum

Section 22.6: Enum to string and back

Section 22.7: Enums can have unexpected values

Section 22.8: Default value for enum == ZERO

Section 22.9: Adding additional description information to an enum value

Section 22.10: Get all the members values of an enum

Section 22.11: Bitwise Manipulation using enums

..............................................................................

..................................................................................

...................................................................................................................

................................................................................................

......................................................................................................

.......................................................

104

........................................................................................

...................................................................................................

98

99

101

101

102

102

103

103

105

105

Chapter 23: Tuples

...................................................................................................................................................

106

Section 23.1: Accessing tuple elements

Section 23.2: Creating tuples

Section 23.3: Comparing and sorting Tuples

Section 23.4: Return multiple values from a method

...................................................................................................................

...................................................................................................................................

.........................................................................................................

............................................................................................

107

Chapter 24: Guid

.......................................................................................................................................................

108

Section 24.1: Getting the string representation of a Guid

Section 24.2: Creating a Guid

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

Section 26.1: Collection initializers

Section 26.2: C# 6 Index Initializers

Section 26.3: Collection initializers in custom classes

Section 26.4: Using collection initializer inside object initializer

Section 26.5: Collection Initializers with Parameter Arrays

.....................................................................................................................

............................................................................................................................

.........................................................................................................................

............................................................................................

............................................................................

113

...................................................................................

114

Chapter 27: An overview of C# collections

.................................................................................................

115

Section 27.1: HashSet<T>

Section 27.2: Dictionary<TKey, TValue>

Section 27.3: SortedSet<T>

Section 27.4: T[ ] (Array of T)

Section 27.5: List<T>

Section 27.6: Stack<T>

Section 27.7: LinkedList<T>

Section 27.8: Queue

..........................................................................................................................................

115

.................................................................................................................

115

.......................................................................................................................................

..................................................................................................................................

..................................................................................................................................................

..............................................................................................................................................

......................................................................................................................................

..................................................................................................................................................

................................................................................................................................................

...............................................................................................................................................

Chapter 28: Looping

Section 28.1: For Loop

Section 28.2: Do - While Loop

Section 28.3: Foreach Loop

Section 28.4: Looping styles

Section 28.5: Nested loops

Section 28.6: continue

Section 28.7: While loop

Section 28.8: break

Chapter 29: Iterators

..................................................................................................................................

120

................................................................................