Examples from Chapter 2 (Basic Coding in C#) of Programming C# 8.0 (O'Reilly).
Variables
- Example 3. Variable declarations
- Example 4. Assigning values to previously declared variables
- Example 5. An error: the wrong type
- Example 6. Implicit variable types with the var keyword
- Example 7. An error: the wrong type (again)
- Example 8. Multiple variables in a single declaration
- Example 9. Using variables
- Example 10. Error: using an unassigned variable
- Example 11. Error: out of scope
- Example 12. Variable declared outside block, used within block
- Example 13. Error: trying to use a variable not in scope
- Example 14. Error: surprising name collision
- Example 15. Error: hiding a variable
Statements and Expressions
- Example 16. Some statements
- Example 17. A block
- Example 18. Expressions within expressions
- Example 19. Method invocation expressions as statements
- Example 20. Errors: some expressions that don’t work as statements
- Example 21. Assignments are expressions
- Example 22. Operand evaluation order
- Example 23. Operand evaluation order with nested expressions
Comments and whitespace
- Example 24. Single line comments
- Example 25. Delimited comments
- Example 26. Multiline comments
- Example 27. Insignificant whitespace
Preprocessing directives:
- Example 28. Conditional compilation
- Example 29. Conditional method
- Example 30. Generating a compiler error
- Example 31. The #line directive and a deliberate mistake
- Example 32. Disabling a compiler warning
Built-in types
- Example 33. Implicit conversions
- Example 34. Errors: implicit conversions not available
- Example 35. Explicit conversions with casts
- Example 36. Exploiting unchecked integer overflow
- Example 37. Checked expression
- Example 38. Checked statement
- Example 39. Using BigInteger
- Example 40. Characters vs char
- Example 41. Expressions in strings
- Example 42. More complex expressions in strings
- Example 43. The effect of string interpolation
- Example 44. Format specifiers
- Example 45. Format specifiers with invariant culture
- Example 46. Creating and using a tuple
- Example 47. Naming tuple members in the initializer
- Example 48. Inferring tuple member names from variables
- Example 49. Default tuple member names
- Example 50. Structural equivalence of tuples
- Example 51. Constructing then deconstructing tuples
Operators
- Example 52. The conditional AND operator
- Example 53. The conditional operator
- Example 54. Exploiting conditional evaluation
- Example 55. The null coalescing operator
- Example 56. Null-conditional and null coalescing operators
- Example 57. Conditional expression as method argument
- Example 58. Life without the conditional operator
- Example 59. Assignment and addition
- Example 60. Compound assignment (addition)
Flow control
- Example 61. Simple if statement
- Example 62. Probably not what was intended
- Example 63. If and else
- Example 64. Picking one of several possibilities
- Example 65. Overdoing the blocks
- Example 66. A switch statement with strings
- Example 67. C-style fall-through, illegal in C#
- Example 68. Fall-through in C#
- Example 69. A while loop
- Example 70. A do loop
- Example 71. Modifying array elements with a for loop
- Example 72. Multiple initializers and iterators
- Example 73. Nested for loops
- Example 74. Iterating over a collection with foreach
- Example 75. General-purpose collection iteration
Patterns
- Example 76. Tuple patterns
- Example 77. Type patterns
- Example 78. Positional pattern
- Example 79. Positional pattern with constant and type patterns
- Example 80. Positional pattern with var
- Example 81. Positional pattern with discard pattern
- Example 82. Property pattern
- Example 83. Property pattern with output
- Example 84. Property pattern with nested pattern with output
- Example 85. Pattern with when clause
- Example 86. Patterns, but not in expressions
- Example 87. A switch expression
- Example 88. An 'is' expression
- Example 89. Using the values from an is expression’s pattern
- Example 90. No need for when in an is expression’s pattern