C# > Language Features by Version > C# 6 to C# 12 Highlights > Record Types (C# 9)
Positional and Property-Based Records
This example demonstrates the difference between positional records and property-based records, illustrating how to define and use each type.
Positional Record
Positional records define properties directly in the constructor. They are concise and suitable for simple data structures. The `with` keyword creates a new record instance based on an existing one, with specified properties modified. This is a non-destructive operation; it doesn't modify the original record.
public record Point(int X, int Y);
public class Example
{
public static void Main(string[] args)
{
Point p1 = new Point(10, 20);
Console.WriteLine($"X: {p1.X}, Y: {p1.Y}");
Point p2 = p1 with { X = 30 }; // Create a new Point with updated X
Console.WriteLine($"X: {p2.X}, Y: {p2.Y}");
}
}
Property-Based Record
Property-based records define properties using the standard property syntax. The `init` accessor allows properties to be set only during object initialization, ensuring immutability after creation. They are suitable for more complex scenarios with specific initialization requirements.
public record Employee
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
public class Example2
{
public static void Main(string[] args)
{
Employee employee1 = new Employee { FirstName = "Alice", LastName = "Smith" };
Console.WriteLine($"First Name: {employee1.FirstName}, Last Name: {employee1.LastName}");
}
}
Concepts behind the snippet
The key difference lies in how the properties are defined and initialized. Positional records use constructor parameters, while property-based records use standard properties with the `init` accessor. Both achieve immutability but offer different levels of flexibility.
Real-Life Use Case Section
Positional records are useful for representing simple data structures like coordinates, dates, or currency amounts. Property-based records are better suited for representing more complex entities with validation rules or dependencies between properties.
Best Practices
Choose positional records for simple, immutable data structures where constructor-based initialization is sufficient. Use property-based records when you need more control over property initialization or require validation rules.
Interview Tip
Be prepared to explain the difference between positional and property-based records and when to use each type. Also, understand the concept of the `with` keyword and its role in creating new record instances based on existing ones.
When to use them
Use positional records when you want a concise syntax for defining immutable data structures with properties directly in the constructor. Use property-based records when you need more control over property initialization, validation, or dependencies between properties.
alternatives
Instead of records, you could use classes with manually implemented equality checks and immutable properties. However, records simplify this process significantly.
pros
cons
FAQ
-
Can I combine positional and property-based syntax in the same record?
Yes, you can combine positional and property-based syntax in the same record definition. This allows you to have both concise constructor-based initialization and more control over specific properties. -
What is the purpose of the `init` accessor?
The `init` accessor allows a property to be set only during object initialization. After the object is created, the property becomes read-only, ensuring immutability.