Skip to content

C# Init-Only Setters — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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

You want an object whose properties cannot change after construction, but using a constructor with ten parameters is unwieldy. Object initializers are clean but require mutable set accessors. Init-only setters give you the best of both.

Wrong

public class Config
{
    public string ConnectionString { get; set; } // Can be changed anywhere
    public int Timeout { get; set; }
}

var cfg = new Config { ConnectionString = "...", Timeout = 30 };
cfg.Timeout = 60; // Works — but should it?

Output: Properties can be mutated after initialization, breaking immutability guarantees.

public class Config
{
    public string ConnectionString { get; init; }
    public int Timeout { get; init; }
}

var cfg = new Config { ConnectionString = "...", Timeout = 30 };
// cfg.Timeout = 60; // CS8852: Init-only property can only be assigned in an object initializer

Output: Compiler error CS8852 if you try to mutate after initialization.

The init accessor allows assignment only during object construction — in the constructor, an object initializer, or a with expression.

Prevention

  • Use init instead of set for properties that should be immutable after creation.
  • Combine with required modifier (C# 11) to force callers to provide the value.
  • Use init on DTOs, configuration objects, and event arguments.
  • Use with expressions to create modified copies of init-only objects.
  • Avoid init on properties that need to change during the object's lifetime.

Common Mistakes with init only setter

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

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

What is the difference between init and readonly?

readonly applies to fields — they can only be assigned in a constructor or field initializer. init applies to properties — they can be assigned via object initializer, constructor, or with expression. Init-only properties typically wrap readonly fields behind them.

Can init-only properties be set in a base class constructor?

Yes. Init-only properties can be set in any constructor in the inheritance chain, as well as via object initializer syntax in derived class construction.

How do init-only setters work with serialization?

JSON serializers like System.Text.Json support init-only properties. They use the public init accessor during deserialization, which counts as initialization. No extra configuration is needed.

Init-only setters are used in DodaZIP for immutable archive entry metadata. For more C# patterns, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro