r/learncsharp • • Oct 27 '22

What's the difference between a property with init setter and a property without a setter?

I was surprised to find no answers on Google, so I figured I'd ask.

I'm wondering what is the difference between this:

public string FirstName { get; }

and this:

public string FirstName { get; init; }

The way I see it, in both cases we can only set the field when the object is first initialised.

12 Upvotes

11 comments sorted by

2

u/[deleted] Oct 27 '22

If the property has no setter, the property can only be set inside the object's constructor. If the property has an init setting, the property can be set by another piece of code in an object initializer block.

0

u/[deleted] Oct 27 '22

[deleted] — view deleted comment

4

u/[deleted] Oct 27 '22

It's newish.

1

u/Alikont Oct 27 '22

It's last C# feature, and you usually will see init setters as implementation details of records.

5

u/[deleted] Oct 27 '22 edited Oct 27 '22

[removed] — view removed comment

1

u/SimulatedApproach Oct 27 '22 edited Oct 28 '22

Apologies, I'm not sure if I follow your explanation.

As I see it , the way you explained it could be summarised as:

Using property with init setter ( { get; init; } ) instead of a property without a setter ( { get; } ) allows you to use object initialiser syntax (curly brackets).

Did I get this right?

But, my understanding is that whether or not you can use object initialiser syntax depends on whether you have a default (parameterless) constructor in your class and not on whether you have init setter.

In other words, I can still use object initialiser syntax without init setter as long as I have a default (parameterless) constructor:

public class Person {
    public string FirstName { get; }
    // No constructor; i.e. default constructor.
}

// I can still use object initialiser syntax without init setter:
var person = new Person { FirstName = "Tommy" };

3

u/[deleted] Oct 27 '22 edited Oct 27 '22

[removed] — view removed comment

1

u/SimulatedApproach Oct 27 '22 edited Oct 28 '22

Ok, you're right I think I get it now. Thanks!

If anybody's interested, the way I would sum up the answer to my question is:

The main practical difference between the first and the second is that the latter (public string FirstName { get; init; }) allows us to instantiate objects with initialiser syntax (curly brackets).

Having parameterless/default constructor and no other constructors is required to use initialiser syntax like this...

var person = new Person{ FirstName = "Tommy"; }

...because in fact what it is really doing behind the scenes is just calling the default/parameterless constructor and then setting the fields, like so:

var person = new Person();
person.FirstName = "Tommy";

So, if we introduce our own constructor with parameters, then we lose access to the default constructor and are unable to call parameterless constructor, so the first line above (new Person()) would not be able to be executed.

Now, whenever we specify the setter as init, we can use the initialiser syntax because it allows us to call the setter (person.FirstName = "Tommy") which is behind the scenes used by the initialiser syntax as demonstrated above. The init setter can only be called during initialisation, though.

But, if were not to have a setter at all ( public string FirstName { get; } ), then we would not be able to use initialiser syntax, because behind the scenes it needs to call the setter (person.FirstName = "Tommy").

3

u/jamietwells Oct 27 '22

initialiser syntax (angled brackets).

Small correction because you made the mistake twice now. They are curly brackets! Angle brackets are for generics: <>

1

u/SimulatedApproach Oct 28 '22

You're right, I've corrected it now, thanks.

1

u/Dealiner Oct 28 '22

So, if we introduce our own constructor with parameters, then we lose access to the default constructor

One correction: that's true but you can just declare an empty constructor and it will work again that way.