What if you could write less code and still get perfect data comparisons and copies automatically?
Why Record structs in C Sharp (C#)? - Purpose & Use Cases
Imagine you want to store simple data like a point with X and Y coordinates. You write a struct with fields, constructors, and methods to compare points. But every time you want to copy or compare points, you write extra code. It feels like repeating the same work over and over.
Manually writing structs for simple data is slow and error-prone. You might forget to write equality checks or GetHashCode methods. This leads to bugs and messy code.
Record structs provide a neat way to define simple data containers with built-in value equality and copying. They combine the speed of structs with the convenience of records, so you write less code and get safer, clearer data types.
public struct Point {
public int X, Y;
public Point(int x, int y) { X = x; Y = y; }
public override bool Equals(object obj) { /* manual equality code */ return base.Equals(obj); }
public override int GetHashCode() { /* manual hash code */ return base.GetHashCode(); }
}public record struct Point(int X, int Y);
You can create simple, efficient data types that automatically support comparison, copying, and immutability with minimal code.
In a game, you can represent positions or colors as record structs. This makes it easy to compare locations or colors without writing extra code, improving performance and reducing bugs.
Manual data types require extra code for equality and hashing.
Record structs simplify this by combining struct speed with record features.
This leads to cleaner, safer, and more efficient code.
Practice
record struct in C#?Solution
Step 1: Understand record struct purpose
Record structs combine struct's lightweight nature with record's value-based equality and immutability.Step 2: Analyze options
It provides value-based equality and immutability by default. correctly states value-based equality and immutability. Options B, C, and D are incorrect because structs cannot inherit, record structs are value types, and they support properties.Final Answer:
It provides value-based equality and immutability by default. -> Option AQuick Check:
record struct = value equality + immutability [OK]
- Thinking record structs support inheritance
- Confusing record structs with classes
- Assuming record structs are reference types
Point with two properties X and Y of type int?Solution
Step 1: Recall record struct declaration syntax
The concise syntax for record structs uses positional parameters:record struct Name(type prop1, type prop2);Step 2: Compare options
record struct Point(int X, int Y); matches the correct syntax. record struct Point { int X; int Y; } is close but missing parentheses for positional parameters. Options A and C have incorrect order or keywords.Final Answer:
record struct Point(int X, int Y); -> Option CQuick Check:
record struct with positional params = record struct Point(int X, int Y); [OK]
- Swapping 'record' and 'struct' keywords
- Omitting parentheses for positional parameters
- Using braces instead of parentheses for positional syntax
record struct Point(int X, int Y);
var p1 = new Point(3, 4);
var p2 = new Point(3, 4);
Console.WriteLine(p1 == p2);
Solution
Step 1: Understand equality in record structs
Record structs provide value-based equality by default, so two instances with same values are equal.Step 2: Analyze the code output
Bothp1andp2have X=3 and Y=4, sop1 == p2evaluates to true.Final Answer:
True -> Option AQuick Check:
record struct equality = value equality [OK]
- Assuming == compares references for structs
- Expecting compilation error for == operator
- Confusing record structs with classes for equality
record struct Rectangle(int Width, int Height);
var rect = new Rectangle(5, 10);
rect.Width = 7;
Console.WriteLine(rect);
Solution
Step 1: Understand immutability of record structs
Record structs with positional parameters generate readonly properties by default.Step 2: Analyze the assignment line
Trying to assignrect.Width = 7;causes a compile-time error becauseWidthis readonly.Final Answer:
Cannot assign to 'Width' because it is readonly. -> Option DQuick Check:
Positional record struct properties are readonly [OK]
- Trying to modify readonly properties
- Confusing record structs with mutable classes
- Ignoring compiler error messages
X, Y, and Z. You want it to be immutable, support value equality, and print nicely. Which is the best way to declare it using record structs?Solution
Step 1: Identify requirements
Needs lightweight, immutable, value equality, and nice printing.Step 2: Match requirements to options
record struct Point3D(int X, int Y, int Z); uses record struct with positional parameters, providing all requirements. struct Point3D { public int X; public int Y; public int Z; } is mutable struct without value equality or nice printing. class Point3D { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } } is mutable class, heavier and reference type. record class Point3D(int X, int Y, int Z); is record class, reference type, not lightweight.Final Answer:
record struct Point3D(int X, int Y, int Z); -> Option BQuick Check:
record struct = lightweight + immutable + value equality [OK]
- Choosing class instead of struct for lightweight data
- Using mutable fields instead of readonly properties
- Confusing record class with record struct
Ready to prove it?
Pass the test and this pattern moves unknown
