Updated on
The init keyword is a property accessor, like get and set, that lets us assign a value only while an object is being created. After the constructor and the object initializer finish, the compiler treats the property as read-only.
C# 9.0 introduced init together with records, and C# 11 built on it with required members. First, we’ll take a closer look at what init is and how it works. Then, we’ll compare it with set and private set, and see where required, records and primary constructors fit in.
Let’s dive in!
What Is the Init Keyword in C#?
The init keyword is an accessor method, just like get and set. A property declared as { get; init; } can receive a value only during the initialization of the object: in a constructor, in an object initializer, or in a with expression on a record or a struct. Any assignment after that is a compile-time error, CS8852.
This allows us to create immutable objects that require a specific set of values during their creation, and those values remain unchanged during the object’s lifetime.
How is that different from a get-only property? A get-only property can only be assigned in its declaration or inside the type’s own constructor. An init property can also be set by the calling code, through an object initializer. That’s why it pairs so well with required members and records.
init also doesn’t force anyone to set the property. If the caller skips it, the property simply keeps its default value.
Because we can call the init accessors only during object creation, we can also initialize readonly fields.
How Does Init Work in C#?
To better illustrate how init works, let’s create a class representing a city:
public class City
{
public string Name { get; init; }
public double Latitude { get; init; }
public double Longitude { get; init; }
public City(string name, double latitude, double longitude)
{
Name = name;
Latitude = latitude;
Longitude = longitude;
}
}
We create a City class that has three properties Name,Latitude, and Longitude. They all have two accessors: get and init. We also have a constructor that takes in three parameters.
Now, let’s create a city:
var city = new City("Tokyo", 35.652832, 139.839478);
Console.WriteLine($"{city.Name}: {city.Latitude}, {city.Longitude}");
In our Program class, we create an instance of our City class by passing Tokyo as the Name and the corresponding Latitude and Longitude. Then, we print our city to the console:
Tokyo: 35.652832, 139.839478
Now, let’s try to change the latitude. This line won’t compile:
city.Latitude = 35.689487;
When we do that and try to build or run our solution, we get:
error CS8852: Init-only property or indexer 'City.Latitude' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.
The application won’t compile if we assign any Init-only properties outside of object initialization. This means that when we use init, we create truly immutable properties. For another option for creating immutable objects, you might also want to look at Records in C# as well.
What Is the Difference Between get; set; and get; init;?
With { get; set; }, any code that can see the setter can change the property at any time. With { get; init; }, the same assignments work, but only while the object is being created. Once the constructor and the object initializer finish, the value is fixed.
Both accessors work with object initializers, so the code that creates the object looks the same either way. The difference shows up on the next line, when something tries to assign the property again. With set, that assignment compiles. With init, the compiler stops it with error CS8852.
To choose, we ask whether the value should change after the object exists. A display name might. A city’s coordinates or an order number usually shouldn’t.
init is only a compiler rule. Reflection can still write to the property, and if the property holds a reference type such as a List<T>, the list’s contents can still change.
Let’s see it on a small class for a landmark:
public class Landmark
{
public string Name { get; set; } = "";
public string City { get; init; } = "";
}
A landmark’s display name can change, but it won’t move to another city. So Name gets set and City gets init. Both can go in an object initializer:
var landmark = new Landmark { Name = "Senso-ji", City = "Tokyo" };
landmark.Name = "Senso-ji Temple";
Console.WriteLine($"{landmark.Name} is in {landmark.City}");
Renaming works, and we get:
Senso-ji Temple is in Tokyo
Now, let’s try the same with City. This line won’t compile:
landmark.City = "Kyoto";
The compiler gives us the same error we saw with Latitude:
error CS8852: Init-only property or indexer 'Landmark.City' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.
Init vs Private Set in C#
We might have noticed that init and private set are very similar and wonder what is the difference between them. We can use both keywords to create read-only properties, but they have a subtle difference between them.Â
Let’s update our City class:
public class City
{
public string Name { get; private set; }
public double Latitude { get; init; }
public double Longitude { get; init; }
public City(string name, double latitude, double longitude)
{
Name = name;
Latitude = latitude;
Longitude = longitude;
}
public void ChangeName(string name)
{
Name = name;
}
}
We update the accessor of the Name property from init to private set. As it makes more sense to change a city’s name than its coordinates, we add the ChangeName method that takes a string parameter and updates the name of our city.
Let’s try updating the Name property directly:
city.Name = "Edo";
When we try to change the name of Tokyo to Edo (its old name), the compiler is not happy:
error CS0272: The property or indexer 'City.Name' cannot be used in this context because the set accessor is inaccessible
This is because properties with the private set accessor can only be modified from inside the class:
city.ChangeName("Edo");
Console.WriteLine($"{city.Name}: {city.Latitude}, {city.Longitude}");
Here, we use the ChangeName method to change the name of our city. This works and our code compiles:
Edo: 35.652832, 139.839478
We successfully changed the name!
If we try the same approach with init, our code will not compile. We will get the same error as when we tried to update Latitude in our Program class. This is the main difference between init and private set: properties marked as init can’t be changed once the object is initialized, whereas those marked with private set can be updated from inside the object after its creation.
Here’s how the four accessors compare, with required added for completeness:
| Declaration | Set in an object initializer, from outside the class | Change later, from outside the class | Change later, inside the class | Error when blocked |
|---|---|---|---|---|
{ get; set; } | Yes | Yes | Yes | None |
{ get; private set; } | No | No | Yes | CS0272 |
{ get; init; } | Yes | No | No | CS8852 |
{ get; } | No | No | No, constructor only | CS0200 |
required ... { get; init; } | Must be set | No | No | CS9035 when missing |
Each error code names the rule we broke: CS0272 is a setter we can’t see, CS8852 is an assignment after initialization, CS0200 is a property with no setter at all, and CS9035 is a required member we left out.
How Does Init Work With Required, Records and Primary Constructors?
Required Members
Microsoft’s init reference says: “An init accessor doesn’t force callers to set the property.” If we forget to set it, the property keeps its default value.
C# 11 added the required modifier for that:
public class Country
{
public required string Name { get; init; }
public string? Capital { get; init; }
}
Now the object initializer has to set Name:
var japan = new Country { Name = "Japan", Capital = "Tokyo" };
Console.WriteLine($"{japan.Name}: {japan.Capital}");
That prints Japan: Tokyo. If we leave Name out, the code doesn’t compile:
var nowhere = new Country { Capital = "Tokyo" };
error CS9035: Required member 'Country.Name' must be set in the object initializer or attribute constructor.
required says the value must be there, and init says it can’t change afterwards. Together they give us a mandatory, read-only property without writing a constructor. We cover the rest of the rules, such as SetsRequiredMembers, in our article on required members in C#.
Records and the with Expression
A positional record gives us init properties without writing them:
public record Coordinates(double Latitude, double Longitude);
Its Latitude and Longitude are init properties, so we can’t assign them after creation. (A positional record struct would make them read-write instead.) To get a changed value, we make a changed copy with a with expression:
var tokyo = new Coordinates(35.652832, 139.839478);
var tower = tokyo with { Latitude = 35.658581, Longitude = 139.745438 };
Console.WriteLine(tokyo);
Console.WriteLine(tower);
The original stays as it was:
Coordinates { Latitude = 35.652832, Longitude = 139.839478 }
Coordinates { Latitude = 35.658581, Longitude = 139.745438 }
A with expression is allowed to set init properties because it runs during the copy’s initialization. It works on records, structs and anonymous types. On a plain class such as our Landmark, landmark with { City = "Kyoto" } fails with CS8858, “The receiver type ‘Landmark’ is not a valid record type and is not a struct type.” We look at records in more depth in Records in C#.
Primary Constructors
C# 12 lets ordinary classes have primary constructors, but Microsoft’s docs note that the compiler generates properties from the parameters only in records. In a class, we write the init properties ourselves and initialize them from the parameters:
public class Airport(string code, string name)
{
public string Code { get; init; } = code;
public string Name { get; init; } = name;
}
Because an object initializer runs after the constructor, it can still override an init value:
var haneda = new Airport("HND", "Haneda") { Name = "Tokyo International" };
Console.WriteLine($"{haneda.Code}: {haneda.Name}");
HND: Tokyo International
After that, haneda.Name is fixed like any other init property, and assigning it fails with CS8852. Our article on primary constructors for classes and structs covers the parameters themselves.
Conclusion
In this article, we learned that by marking properties with the init keyword, we can make it clear that they should not be modified after initialization. In comparison, we can also use the private set keyword to create read-only properties, but with the ability to modify them from within the class. Deciding which one to use depends on the level of immutability you need.
If we need truly immutable properties and fields, we should use init. If we need read-only properties that can be updated from inside the class, we should use private set. Understanding the difference between the two can help us write more robust and maintainable code. If the value must also be provided, we add required. And when we need a changed version of an immutable object, a record and a with expression give us a new copy.
Tested with .NET 10.0.10, C# 14 and xUnit 2.9.3.






