English
Summary
Conclusions
A class describes the data and behavior of objects of the same kind, and objects created with new have their own state. Classes are reference types: variables store references, assignment does not copy an object, and null requires checking. Store object state in private fields and expose it through properties that validate values; auto-properties, init, required, computed properties, and the field keyword make the code shorter. Constructors put an object into a valid initial state, can be overloaded, and call one another through this(…); primary constructors and object initializers make object creation more concise. The garbage collector frees memory occupied by unreachable objects.
Self-check questions
- What are a class and an object? What makes up an object's state and behavior?
- How do you declare a class and create an instance? What does target-typed
new()mean? - What happens when class-type variables are assigned? How does
==compare objects? - What is
null? How does the?.operator work? - How does a field differ from a property? Why make fields private?
- What does the
thiskeyword mean? - How do you write a property that validates its value? What is
value? - How do
{ get; },{ get; init; }, and{ get; private set; }properties differ? - What is the
requiredmodifier for? - What is a computed property? What does the
fieldkeyword refer to? - When does the compiler create a default constructor?
- How does a
: this(…)constructor chain work? - How do primary constructor parameters differ from fields?
- In what order do the constructor and object initializer run?
- Who frees memory occupied by objects, and when?
Useful links
- Classes: https://learn.microsoft.com/dotnet/csharp/fundamentals/types/classes
- Objects: https://learn.microsoft.com/dotnet/csharp/fundamentals/object-oriented/objects
- Properties: https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/properties
- Constructors: https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/constructors
- What's new in C# 14: https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-14
- Nullable reference types: https://learn.microsoft.com/dotnet/csharp/nullable-references