Records, structs, enums and week review
Not all types are classes. C# offers alternatives designed for specific cases: records for data compared by their content, structs for small lightweight values, and enums for closed lists of options. Knowing them saves you from writing a lot of repetitive code. To understand their differences you need a key idea: the difference between reference types and value types.
Reference versus value
Variables of a reference type (class, string, arrays, List<T>) store an address that points to the object; when you assign b = a, both variables point to the same object and a change is seen in both. Variables of a value type (int, bool, struct, enum) store the value itself; when you assign b = a the content is copied and the variables are independent. That is why, when comparing two objects of a class with ==, by default C# checks whether they are the same object, not whether they have the same data.
var c1 = new Box { Size = 5 };
var c2 = c1; // same object
c2.Size = 99;
Console.WriteLine(c1.Size); // 99
var s1 = new Dot { X = 5 };
var s2 = s1; // independent copy
s2.X = 99;
Console.WriteLine(s1.X); // 5
Console.WriteLine(new Box { Size = 1 } == new Box { Size = 1 }); // False (different objects)
class Box { public int Size { get; set; } }
struct Dot { public int X { get; set; } }Records
A record is a class designed to hold data. With a single line, record Person(string Name, int Age), you get init-only properties, a constructor, comparison by content (two records with the same data are equal), a readable ToString and the with operator, which creates a copy changing only some fields without modifying the original. Records are immutable by design, which makes them very safe to pass around. Use them for data such as addresses, coordinates, messages or the records that are later saved as JSON.
var p1 = new Person("Ana", 29);
var p2 = new Person("Ana", 29);
var p3 = p1 with { Age = 30 };
Console.WriteLine(p1 == p2); // True: same content
Console.WriteLine(p1); // Person { Name = Ana, Age = 29 }
Console.WriteLine(p3); // Person { Name = Ana, Age = 30 }
Console.WriteLine(p1.Age); // 29: the original did not change
record Person(string Name, int Age);Structs and enums
A struct is a value type that groups a few small pieces of data, like a point or a color. It is copied on assignment and does not need separate heap memory, but if it grows a lot it is expensive to copy; that is why it is reserved for small values. An enum defines a fixed set of names, each associated with a number: enum Status { Pending, Paid, Shipped }. Using an enum instead of loose numbers or texts makes the code more readable, because you write Status.Paid instead of 1 or "paid", and the compiler prevents invented values. Enums combine wonderfully with switch.
Status current = Status.Paid;
Console.WriteLine(current); // Paid
Console.WriteLine((int)current); // 1
string message = current switch
{
Status.Pending => "Waiting for payment",
Status.Paid => "Preparing the order",
Status.Shipped => "On the way",
_ => "Unknown"
};
Console.WriteLine(message); // Preparing the order
Console.WriteLine(Enum.Parse<Status>("Shipped")); // Shipped
var origin = new Coord(0, 0);
var moved = origin with { Y = 5 };
Console.WriteLine($"{origin} {moved}"); // Coord { X = 0, Y = 0 } Coord { X = 0, Y = 5 }
enum Status { Pending, Paid, Shipped }
readonly record struct Coord(int X, int Y);اختبر ما تعلمته
السؤال 1 من 6What happens with b = a if a is an object of a class?
✎ ملاحظاتي
محفوظ في هذا المتصفح فقط. سجّل الدخول لحفظه في حسابك.0/5000 حرف