Skip to content

C# Required Members — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# Required Members. We cover key concepts, practical examples, and best practices.

You create an object using an object initializer but forget to set a critical property. The code compiles and runs, but produces incorrect behavior because a mandatory field was left at its default. The required modifier (C# 11) forces callers to provide a value.

Wrong

public class UserConfig
{
    public string ConnectionString { get; set; } // Forgot to set this
    public int Timeout { get; set; } = 30;
}

var config = new UserConfig { Timeout = 60 };
// ConnectionString is null — will crash at runtime

Output: No compiler warning. Runtime NullReferenceException when ConnectionString is used.

public class UserConfig
{
    public required string ConnectionString { get; init; }
    public int Timeout { get; set; } = 30;
}

var config = new UserConfig { Timeout = 60 };
// CS9035: Required member 'UserConfig.ConnectionString' must be set

Output: Compiler error CS9035. The caller must provide ConnectionString:

var config = new UserConfig { ConnectionString = "Server=...", Timeout = 60 };
// Compiles fine

The required modifier works with properties, fields, and init-only setters. It enforces that the member is initialized during construction or object initializer.

Prevention

  • Use required on properties that must be provided by the caller.
  • Use required on init-only properties to enforce required initialization without a constructor parameter.
  • Use required on record types to make positional parameters mandatory in object initializers.
  • Combine required with SetsRequiredMembers attribute on constructors for bypass scenarios.
  • Avoid required on properties with sensible defaults.
  • Use required in configuration objects and DTOs to catch missing values at compile time.

Common Mistakes with required member

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can required members be set in a constructor?

Yes. If a constructor is marked with [SetsRequiredMembers], it satisfies the requirement. Otherwise, callers must use an object initializer or set the property after the constructor.

Do required members work with serialization?

Yes, most serializers (System.Text.Json, Newtonsoft.Json) can set required members during deserialization. The serializer treats them as regular settable properties. However, JSON deserialization does not validate requirement at compile time.

Can I have a required field instead of a property?

Yes. public required string _connectionString; — the required modifier works on fields too. This is useful for performance-sensitive code where property overhead matters.

Required members enforce initialization contracts in DodaTech's configuration system. For more C# features, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro