Each type in the CTS is defined as either a value type or a reference type. These types include all custom types in the .NET class library and also your own user-defined types. Types that you define by using the struct
keyword are value types; all the built-in numeric types are structs
. Types that you define by using the class
or record
keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior.
Classes and structs are two of the basic constructs of the common type system in .NET. Each is essentially a data structure that encapsulates a set of data and behaviors that belong together as a logical unit. The data and behaviors are the members of the class, struct, or record. The members include its methods, properties, events, and so on.
A class, struct, or record declaration is like a blueprint that is used to create instances or objects at run time. If you define a class, struct, or record named Person
, Person
is the name of the type. If you declare and initialize a variable p
of type Person
, p
is said to be an object or instance of Person
. Multiple instances of the same Person
type can be created, and each instance can have different values in its properties and fields.
A class is a reference type. When an object of the type is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.
A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it's copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy don't affect the other copy.
Record types may be either reference types (record class
) or value types (record struct
). Record types contain methods that support value-equality.
In general, classes are used to model more complex behavior. Classes typically store data that is intended to be modified after a class object is created. Structs are best suited for small data structures. Structs typically store data that isn't intended to be modified after the struct is created. Record types are data structures with additional compiler synthesized members. Records typically store data that isn't intended to be modified after the object is created.
Note: Structs do not support inheritance, but they can implement interfaces.
Source: https://learn.microsoft.com/ru-ru/dotnet/csharp/fundamentals/types/