In C#, the required keyword was introduced in C# 11. It's used to enforce initialization of properties and fields within a class, record, or struct. This helps ensure data integrity and prevents potential issues arising from uninitialized members.

Understanding required

Required focuses on class members. When applied to a property or field, it mandates that the member be assigned a value during object initialization. This can be done through either a constructor or an object initializer.

Key points about required

Benefits of Using required

Example of required in action

C#
public class Person
{
    public required string Name { get; set; }
    public int? Age { get; set; } // nullable int

    public Person(string name, int? age) // constructor for full initialization
    {
        Name = name;
        Age = age;
    }
}

// Valid object creation with initializer
var person1 = new Person { Name = "Bob" };

// Valid object creation with constructor
var person2 = new Person("Charlie", 30);

// Error: missing required member initialization
// var person3 = new Person(); // This would cause a compile-time error

In this example, the Name property is marked as required, ensuring it's always assigned a value during object creation.

Non-nullable Reference Type with Default Initialization

C#
public class Product
{
    public required string Name { get; set; }
    public decimal Price { get; set; } // not required, has default value
}

var product1 = new Product { Name = "Headphones" }; // Valid: initializes required member

// This would cause a compile-time error because Name is required
// var product2 = new Product();

In this example, Name is required and must be initialized. Price is not required and has a default value of 0.

Record with Required Property

C#
public record Order(
    required string CustomerName,
    int ItemsCount,
    DateTime OrderDate)
{
    // ... other properties and methods
}

var order1 = new Order("John Doe", 2, DateTime.UtcNow); // Valid: initializes required member

// Error: CustomerName is required
// var order2 = new Order(0, DateTime.UtcNow);

This example demonstrates required within a record. CustomerName is mandatory, while other properties can be provided during object creation.

Struct with Required Field

C#
public struct Point
{
    public required int X { get; set; }
    public int Y { get; set; }
}

var point1 = new Point { X = 5, Y = 10 }; // Valid: initializes both members
var point2 = new Point { X = 3 }; // Error: Y is not initialized

// Alternative: initialize Y with default value (0)
var point3 = new Point { X = -2 };

This example shows required with a struct field. X is mandatory, but Y can be left uninitialized with a default value of 0.

Important Considerations

By effectively utilizing the required keyword, you can write C# code that is more reliable, easier to understand, and less prone to errors.