Examples from Chapter 2 (Basic Coding in C#) of Programming C# 10.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 versus char
- Example 41. Expressions in strings
- Example 42. More complex expressions in strings
- Example 43. Using string.Format
- Example 44. A potentially unused interpolated string
- Example 45. Format specifiers
- Example 46. Format specifiers with invariant culture
- Example 47. Creating and using a tuple
- Example 48. Naming tuple members in the initializer
- Example 49. Inferring tuple member names from variables
- Example 50. Default tuple member names
- Example 51. Structural equivalence of tuples
- Example 52. Constructing then deconstructing tuples
- Example 53. Mixing declarations and existing variables in tuple deconstruction
- Example 54. Tuple deconstruction with discard
Operators
- Example 55. The conditional AND operator
- Example 56. The conditional operator
- Example 57. Exploiting conditional evaluation
- Example 58. The null coalescing operator
- Example 59. Null-conditional and null coalescing operators
- Example 60. Conditional expression as method argument
- Example 61. Life without the conditional operator
- Example 62. Assignment and addition
- Example 63. Compound assignment (addition)
Flow control
- Example 64. Simple if statement
- Example 65. Probably not what was intended
- Example 66. If and else
- Example 67. Picking one of several possibilities
- Example 68. Overdoing the blocks
- Example 69. A switch statement with strings
- Example 70. C-style fall-through, illegal in C#
- Example 71. Fall-through in C#
- Example 72. A while loop
- Example 73. A do loop
- Example 74. Modifying array elements with a for loop
- Example 75. Multiple initializers and iterators
- Example 76. Nested for loops
- Example 77. Iterating over a collection with foreach
- Example 78. General-purpose collection iteration
Patterns
- Example 79. Declaration patterns
- Example 80. Type patterns
- Example 81. Positional pattern
- Example 82. Positional patterns with constant values
- Example 83. Positional pattern with constant and declaration patterns
- Example 84. Positional pattern with var
- Example 85. Positional pattern with discard pattern
- Example 86. Property pattern
- Example 87. Property pattern with output
- Example 88. Property pattern with nested pattern with output
- Example 89. Property pattern with nested property pattern
- Example 90. Extended property pattern
- Example 91. Detecting non-nullness with pattern negation
- Example 92. Using pattern conjunction (
and
) and negation (not
) - Example 93. Relational patterns
- Example 94. Using relational patterns in a conjunction
- Example 95. Pattern with when clause
- Example 96. Patterns, but not in expressions
- Example 97. A switch expression
- Example 98. An 'is' expression
- Example 99. Testing for non-nullness with 'is'
- Example 100. Using the values from an is expression’s pattern
- Example 100. No need for when in an is expression’s pattern