Required Members

C# 11.0 .NET 7.0

Published Updated Author Jeffrey T. Fritz Reading time

Required members enforce that specific properties must be set during object initialization, preventing incomplete objects and catching errors at compile time.

Introduced in C# 11.0, the required keyword marks properties that must be initialized when creating an instance. Instead of relying on conventions or runtime checks, the compiler enforces that all required members are set before the object can be used. This is particularly valuable for ensuring data-transfer objects and other models are properly initialized.

Why it matters

Required members provide compile-time safety for object initialization. They prevent runtime errors from incomplete objects and eliminate the need for defensive null-checking in constructors or factory methods. This is especially valuable for APIs, DTOs, and domain models where certain data must always be present.

Cautions

Required members can be circumvented with reflection or by using the [ObsoletionAttribute] with error-level warnings, so they are not a security boundary. Additionally, when implementing required members in derived classes, you must explicitly mark them as required—inheritance does not automatically propagate the required modifier. Be aware that required properties change the ABI and may break existing code that uses reflection to construct instances.

Marking properties as required

The required keyword ensures that properties must be initialized when creating an instance, with compile-time enforcement.

Valid since C# 11.0

using System;

// Immutable record with required init-only properties
public record User
{
    public required int Id { get; init; }
    public required string Email { get; init; }
    public required string FirstName { get; init; }
    public required string LastName { get; init; }
    public string? MiddleName { get; init; }
}

public static class Program
{
    public static void Main()
    {
        // This compiles - all required properties are initialized
        var user = new User
        {
            Id = 1,
            Email = "alice@example.com",
            FirstName = "Alice",
            LastName = "Smith"
        };

        // This does NOT compile
        // var incomplete = new User
        // {
        //     Id = 2,
        //     Email = "bob@example.com"
        //     // Error: Missing required member 'FirstName' and 'LastName'
        // };

        Console.WriteLine($"User: {user.FirstName} {user.LastName} ({user.Email})");
    }
}

Required with init-only properties

Combine required with init-only properties for immutable objects with compile-time initialization guarantees.

Valid since C# 11.0

using System;

// Product class with required members
public class Product
{
    public required string Name { get; set; }
    public required decimal Price { get; set; }
    public string? Description { get; set; }
    public int? StockCount { get; set; }
}

public static class Program
{
    public static void Main()
    {
        // This compiles - all required properties are set
        var product1 = new Product
        {
            Name = "Laptop",
            Price = 999.99m,
            Description = "High-performance laptop"
        };

        // This does NOT compile - missing required property 'Price'
        // var product2 = new Product
        // {
        //     Name = "Tablet"
        //     // Error: Missing required member 'Price'
        // };

        Console.WriteLine($"Product: {product1.Name}, Price: {product1.Price}");
    }
}

Learn more

Required Members (Microsoft Learn)