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