Join us on the demo, while our product experts provide a detailed walkthrough of our enterprise platform.

logoAsync

The latest news in the world of engineering.

Identity and Access Management (IAM), including security and customer experience.

Visit Blog
Fuel

Grow your business to millions.Engage and retain your customers.

Visit Blog

C# Init-Only Setters Property

In this article, we will talk about Init-Only Setters Property in C#.

Hemant Manwani
Hemant Manwani
July 15, 2021
3 min read

Free, Secure and Trusted Way to Authenticate Your Visitors

Add login to your website in 5 minutes completely for free!

Free Sign UpNo hidden costs. No credit card needed.

Introduction

In C# 9.0, there are multiple features introduced. One of them is the Init-Only setters property feature. To use this feature, there are two pre-requisite.

  1. You should have the .NET 5 SDK installed in your system. If not, you can download and install it from here.
  2. You should have at least a 16.7 version or the latest version of Visual Studio 2019. If not, then you have to update your Visual Studio 2019 to the latest version.

After this setup, you are ready to go with the Init-Only setters property feature.

Before knowing more about this feature, first, we will understand how we are using properties currently in C#.

How we are using the properties currently

public int Id { get; set; }

This is the way how we are using the properties, but whenever if we don't want to change the value of a property outside of a class and make them readable publicly, then we generally define the property as below.

public int Id { get; private set; }

In that case, the Id property can not be set outside of the class, and to set this property, we have to introduce a constructor or a public method that can set the value of the Id field. Like below

public class Company
{
    public int Id { get; private set; }

    public Company(int id) // Constructor
    {
        Id = id;
    }
    public void SetId(int id) //Public method
    {
        Id = id;
    }
}

Below are the problems which arise by using the above method

  1. We are not able to set the property value by using the object initialization
  2. If we want to set the property as immutable (Value can not be changed), then we can not achieve this because any public method can change the value of that property, so here the property is mutable (Value can be changed).

To fix those issues, In C# 9.0 Init-Only setters property feature was introduced. Have a look at this.

Init-Only Setters Property

Init-Only setters property gives us the flexibility to set the value of a property by using the object initializer as well the property is also immutable. So that will resolve the above issues, which were with the private set property.

public class Company
{
    public int Id { get; init; }
}
Company comp=new Company{
    Id=1
}; // It works fine if we initialize here

As we can, it is perfectly fine to set the value during object initialization.

public class Company
{
    public int Id { get; init; }

     public Company(int id)
    {
        this.Id = Id;
    }
}

We can define the constructor as well to initialize the Init-Only setters property.

If we will try to create a method in the class and want to change the value Init-Only setters property, then it will give us the compile-time error.

public class Company
{
    public int Id { get; init; }

    public void SetId(int Id)
    {
        this.Id = Id; // Compile-time Error
    }
}

Conclusion

In this article, we learned how we are setting the properties in C# currently and the new C# 9.0 feature Init-Only Setters Property. Also, we understood how this feature helps us to set the property value with the object initialization.



LoginRadius Docs

Implement Authentication in Minutes

click here

Most Popular Tags

EngineeringJavaScriptReactNodeJsAuthenticationCSSSecurityOAuthC#Go

Are your customers safe on your application?

According to Forbes, data breaches exposed 4.1 billion records in the first six months of 2019. If this gets you worried, we’ve got your back!

LoginRadius protects your customers’ identities. We provide world-class security for your customers during login, registration, password setup, and any other data touchpoints, and make sure that their data is safe. We do so through by offering open source SDKs, integrations with over 150 third party applications, pre-designed and customizable login interfaces, and best-in-class data security products such as MFA, RBA, and Advanced Password Policies. The platform is already loved by over 3,000 businesses with a monthly reach of 1.17 billion users worldwide.Secure Your Application Now

Hemant Manwani

Hemant Manwani

Hemant Manwani is a Software Engineer at LoginRadius who is interested in how the web works and developer tools that make working with web easier.

View Profile

Try a Modern Authentication Solution

$0/ month

Free Sign Up
  • 7,000 Monthly Active Users
  • 1 Web or Mobile App
  • Email/Password Login
  • 3 Social Login Providers
  • User Data Storage
  • Transactional Email Template
  • Customizable Login Interfaces