Community Question
What are Records and Structs in C#?
Share knowledge. Learn from experts. Build together.
Question
Records and structs are C# types that provide different approaches to representing data. Records are designed primarily for data-centric models and provide value-based equality semantics, while structs are value types that can be useful for small data structures where value semantics and efficient allocation are important.
Records can be declared as record or record struct, providing developers with features such as concise syntax and value-based equality. Structs, on the other hand, are value types and should generally be designed to be small and immutable when practical.
The choice between classes, records, and structs affects equality, mutability, copying behavior, memory usage, and application design.
Answers
Records and structs are both useful for representing data in C#, but they have fundamentally different semantics.
The most important distinction is:
Records are primarily designed around value-based equality and data modeling, while structs are value types with value-type memory semantics.
However, modern C# makes this distinction more nuanced because C# supports both record classes and record structs.
Structs
A struct is a value type.
Example:
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
When you assign a struct:
Point p1 = new Point { X = 10, Y = 20 };
Point p2 = p1;
p2 receives a copy of the value.
Conceptually:
p1 → X=10, Y=20
p2 → X=10, Y=20
Changing p2 doesn't normally modify p1.
Where structs are useful
Structs are appropriate for small, lightweight values, such as:
Coordinates
Measurements
Numeric abstractions
Date/time-related values
Small immutable value objects
The .NET ecosystem itself contains important value types such as:
Int32
Double
Decimal
DateTime
Guid
Struct performance consideration
A struct isn't automatically "faster" simply because it's a value type.
Large structs can be expensive to copy.
For example:
LargeStruct value2 = value1;
may copy a substantial amount of data.
Therefore, structs are generally best kept relatively small and preferably immutable.
Records
Records were introduced in modern C# to make data-centric types easier to express.
Example:
public record Employee(
int Id,
string Name,
string Department);
Now:
var e1 = new Employee(1, "John", "IT");
var e2 = new Employee(1, "John", "IT");
Records provide value-based equality semantics.
Conceptually:
e1
Employee(1, "John", "IT")
e2
Employee(1, "John", "IT")
e1 == e2
→ true
For ordinary classes, equality usually represents object identity unless equality is explicitly overridden.
Record Classes vs Record Structs
This is where advanced C# interviews become interesting.
Record class
public record Customer(int Id, string Name);
This is a reference type.
Record struct
public record struct Customer(int Id, string Name);
This is a value type.
So:
record class
↓
reference type
↓
value-based equality
record struct
↓
value type
↓
value-based equality
with expressions
Records also make non-destructive mutation convenient.
var employee1 = new Employee(1, "John", "IT");
var employee2 = employee1 with
{
Name = "David"
};
Instead of modifying the original object, a new value is produced.
Conceptually:
employee1
Name = John
↓ with
employee2
Name = David
This is particularly useful for immutable-style application design.
Records vs Classes vs Structs
Feature Class Record Class Struct Record Struct
Reference/value type Reference Reference Value Value
Value-based equality by default No Yes Yes Yes
Good for data models Yes Excellent Sometimes Sometimes
Supports inheritance Yes Yes No class inheritance No class inheritance
with expression Modern support depending on type/features Yes Modern C# supports relevant forms Yes
Typical use Behavior/entities Immutable/data-centric models Small values Small data values
Enterprise example
A domain model might use:
public record Address(
string Street,
string City,
string PostalCode);
An application can safely treat two identical addresses as equivalent values.
A struct could instead be appropriate for a small mathematical value:
public readonly struct Money
{
public decimal Amount { get; }
public string Currency { get; }
public Money(decimal amount, string currency)
{
Amount = amount;
Currency = currency;
}
}
Interview-level answer
A struct is a value type intended primarily for small value semantics, whereas a record is a data-oriented type designed to provide value-based equality, concise initialization, and immutable-style programming patterns. Records can be reference types (record class) or value types (record struct). The choice should depend on semantics, mutability, size, allocation behavior, inheritance requirements, and how equality should work—not simply on performance assumptions.
Your Answer
Login required
Please login to participate in this discussion
and post your answer.