Examples from Chapter 3 (Types) of Programming C# 12.0 (O'Reilly).
- Example 1. A simple class
- Example 2. Using a custom class
- Example 3. A class with a primary constructor
- Example 4. Using multiple instances of a class with a primary constructor
- Example 7. Using static members normally
- Example 8. Using static members without explicit qualification
- Example 9. Plain old data, using public fields
- Example 10. Enforcing initialization of fields with a constructor
- Example 11. A record type with positional syntax
- Example 12. Using a record type
- Example 13. Making a modified copy of an immutable record
- Example 14. A record type with modifiable properties
- Example 15. Comparing two instances of a type
- Example 16. Nested record types
- Example 17. Using nested record types
- Example 18. Copying references
- Example 19. A Count property for the Counter class
- Example 20. Comparing references
- Example 21. Comparing values
- Example 22. Specifying nullability
- Example 23. Specifying enabled nullable warning and annotation contexts as the project-wide default
- Example 24. Dereferencing a nullable reference
- Example 25. The null forgiving operator
- Example 26. Nullability and the Try pattern—before nullable reference types
- Example 27. Nullable-aware use of the Try pattern
- Example 28. A nullable attribute
- Example 29. Arrays and nullability
- Example 30. A simple struct
- Example 31. Support custom comparison
- Example 32. Comparing struct instances
- Example 33. A read-only struct in a read-only property
- Example 34. A read-only
record
, a mutablerecord struct
and areadonly record struct
- Example 36. Error: failing to provide a Uri with its URI
- Example 37. A class with one constructor
- Example 38. A class with a primary constructor and no other constructors
- Example 39. Double storage of a primary constructor argument
- Example 40. Using a constructor
- Example 41. Using the target-typed new syntax
- Example 42. A nonempty zero-argument constructor
- Example 43. Record type with customized copy constructor
- Example 44. Optional chaining of constructors
- Example 45. Chained constructor arguments
- Example 46. Class with static constructor
- Example 47. Significant ordering of static fields
- Example 48. Initialization order
- Example 49. Circular static dependencies
- Example 50. Enabling deconstruction
- Example 51. Using a custom deconstructor
- Example 52. Positional pattern using a custom deconstructor
- Example 53. Positional pattern with explicit type
- Example 54. Record struct using positional syntax
- Example 55. Passing arguments by reference
- Example 56. Putting an out parameter's result into a new variable
- Example 57. Using out parameter's result in the same expression
- Example 58. Putting an out parameter's result into an existing variable
- Example 59. Discarding an out parameter's result
- Example 60. Calling a method with a ref argument
- Example 61. A large value type
- Example 62. A method with an in parameter
- Example 63. Calling a method with an in parameter
- Example 64. A local ref variable
- Example 65. Valid and invalid uses of ref returns
- Example 66. A method with optional arguments
- Example 67. Omitting one argument
- Example 68. Specifying an argument name
- Example 69. Unclear arguments
- Example 70. Improving clarity by naming arguments
- Example 71. Overloaded method
- Example 72. String formatting
- Example 73. Explicitly passing multiple arguments as an array
- Example 74. The params keyword
- Example 75. Local functions
- Example 76. An expression-bodied method
- Example 77. An extension method
- Example 78. Extension method available due to namespace declaration
- Example 79. Extension method available due to using directive
- Example 80. Class with simple property
- Example 81. Expression-bodied get and set
- Example 82. An auto-implemented property
- Example 83. Using a property
- Example 84. Auto-property with private setter
- Example 85. Auto-property with no setter
- Example 86. Initializing an auto-property with no setter
- Example 87. Record requiring initial values, but allowing later modification
- Example 88. Using an object initializer
- Example 89. Using a constructor and an object initializer
- Example 90. Class with auto-property with init-only setter
- Example 91. Setting an init-only property
- Example 92. A read-only struct with init-only properties
- Example 93. Using the with syntax on a non-record read-only struct
- Example 94. Required properties
- Example 95. A calculated property
- Example 96. An expression-bodied read-only property
- Example 97. A property using a mutable value type
- Example 98. Error: cannot modify a property of a value type property
- Example 99. Replacing a property with methods
- Example 100. A property returning a reference
- Example 101. Making the copy explicit
- Example 102. Making the reference explicit
- Example 103. Using an indexer
- Example 104. Class with indexer
- Example 105. Null conditional index access
- Example 106. Code equivalent to null-conditional index access
- Example 107. Using an indexer in an object initializer
- Example 108. Implementing the + operator
- Example 109. Supporting other operand types
- Example 110. Checked
+
operator - Example 111. Conversion operators
- Example 112. Using explicit conversion operators
- Example 114. An interface
- Example 115. Implementing an interface
- Example 116. Explicit implementation of an interface member
- Example 117. An interface with a default implementation of a property
- Example 118. An interface with a const and a nested type
- Example 119. An interface with a static abstract property
- Example 120. A static virtual property
- Example 121. Using a static virtual member