Codectionary / Developer documentation / C#

Require Initialisation with required Members

Make object initialisation obligations explicit without confusing them with runtime validation.

Read the syntax

Make object initialisation obligations explicit without confusing them with runtime validation.

The object initializer supplies Arrays while the object is being created.

What it is and why to use it

A type that needs a title should make that expectation visible to callers. The required modifier, introduced in C# 11, tells the compiler that construction must initialise a field or property. It works with object initialisers and can complement init-only properties. It does not itself ensure that a string is nonempty or that data from outside C# is valid.

How it works

A caller normally supplies every required member in the object initializer. Nullable annotations separately describe whether null is expected. required and init solve different problems: required demands assignment during construction, while init restricts later assignment. Constructors marked SetsRequiredMembers can assert that they fulfil the obligation, but the attribute is a promise rather than compiler verification of every assignment.

Common mistakes and limitations

Do not treat required as a substitute for range, format or null validation at runtime. Do not add SetsRequiredMembers simply to suppress a useful compiler error. Reflection and serializers have their own construction rules and may need separate configuration or validation.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

public required string Title { get; init; }

Examples

Provide every required property

Construct a small lesson whose title must be supplied.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

var lesson = new Lesson { Title = "Arrays" };
Console.WriteLine(lesson.Title);
class Lesson {
    public required string Title { get; init; }
}
  1. Title is required, so ordinary C# construction must include its initialisation.
  2. The object initializer supplies Arrays while the object is being created.
  3. The init accessor prevents ordinary reassignment after construction. Omitting Title would be a compiler error, not a blank title at runtime.
  4. Expected output: Arrays
  5. Use required for construction obligations and validation for valid values.

See why required is not value validation

Assign an empty string, then inspect a separate application rule.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

var lesson = new Lesson { Title = "" };
Console.WriteLine(string.IsNullOrWhiteSpace(lesson.Title) ? "Invalid title" : "Valid title");
class Lesson {
    public required string Title { get; init; }
}
  1. The assignment satisfies the compiler’s required-member obligation.
  2. An empty string still violates this application’s title rule.
  3. The separate check exposes that difference. Enforce such invariants at a suitable constructor, factory or input-validation boundary in real code.
  4. Expected output: Invalid title
  5. Use required for construction obligations and validation for valid values.

Best practices

  • Use required for construction obligations and validation for valid values.
  • Combine it with init only when later reassignment should be restricted.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading