Thomas Eyde
Thomas Eyde

Reputation: 3934

Why in C# 10 do I get a compiler warning CS8618 on init properties

I have this code

#enable nullable
public class SomeClass
{
  public string SomeProperty { get; init; }
}

With the following compiler warning:

[CS8618] Non-nullable property 'SomeProperty' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

I thought the whole point of declaring the property as init was to ensure the properties are set at creation time with the property initializer syntax, like

var s = new SomeClass { SomeProperty = "some value" };

So why do I get this compiler warning? Have I completely misunderstood what the init is for?

Edit 1:

I am not the only one being tripped by this. There is a Roslyn issue about almost the same thing.

There is also a proposal about required properties where Richiban asks:

What's the reasoning being init props being non-mandatory? I may have missed something, but I imagine that they'll be very rarely used without a req modifier.

There is a difference between 'can initialize' and 'must initialize', which the init keyword fails to communicate clearly. With all the fuzz around non-nullable reference types and correct code, I believed, incorrectly, that init meant 'must initialize'.

Upvotes: 11

Views: 12050

Answers (3)

simonra
simonra

Reputation: 285

As the edit and the other answers point out, this behaviour arises from the fact that init only makes it so that the object/value cannot be changed after initialization, which becomes weird if it's a property you don't allow to be null but at the same time allow for the parent to initialized without the said property.

With C# 11 and the introduction of Required Members you can now use the required keyword to specify that the member has to be initialized at creation, if that's what you wanted to achieve:

public class SomeClass
{
    public required string SomeProperty { get; set; }
}

Or, if you want to both demand that the property is set when the instance is created and that it becomes immutable, you can use it in combination with init:

public class SomeClass
{
    public required string SomeProperty { get; init; }
}

Upvotes: 5

Joma
Joma

Reputation: 3859

with init keyword the property value can only be changed in constructor or initilize block.

if you use the default constructor

var s = new SomeClass ();
s.SomeProperty = "A value";//Compiler error CS8852  Init-only property or indexer "SomeClass.SomeProperty" can only be assigned in an object initializer, or on "this" or "base" in an instance constructor or an "init" accessor.

The property SomeProperty was initialized with null, but is not nullable reference type. the compiler is reporting a possible future runtime error. Warning CS8618 Non-nullable property "SomeProperty" must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

If there is no problem that this happens, the warning can be removed with the following

1. Use null forgiving operator

public class SomeClass
{
    public string SomeProperty { get; init; } = null!;// indicating null value is a correct not null value. 
}

When you try to access and use the SomeProperty value, you need to check nullability to avoid a null reference exception even though the type indicates that it is non-nullable. This is irritating.

if(s.SomeProperty==null) //OK
{
   //DO SOME STUFF
}

var length = s.SomeProperty?.Length ?? 0; //OK

var length2 = s.SomeProperty;//NullReferenceException

2. Assigning/Initialize a valid value for property

public class SomeClass
{
    public string SomeProperty { get; init; } = "My initial value"; 
}

you can invoke default constructor without initialze the property
var s = new SomeClass (); //Some property is "My initial value"

or invoke constructor with initialization block

var s = new SomeClass()
{
   SomeProperty = "My new intial value"
};


var length = s.SomeProperty.Length; //NO exception / No problem

3. Create constructor with parameter, the default constructor is avoided.

Errors/Warnings only appear in the caller code.

public class SomeClass
{
    public string SomeProperty { get; init; } //ALL OK. CS8618 dissapeared / No Warning.

    public SomeClass(string someProperty)
    {
        SomeProperty = someProperty;
    }
}

var s = new SomeClass(); //CS7036 ERROR.


var s2 = new SomeClass() //CS7036 ERROR.
{
    SomeProperty = "My new intial value"
};

var s3 = new SomeClass("My new initial value"); //OK. No Warning

var s4 = new SomeClass("My new initial value") //OK. No Warning.
{
    SomeProperty = "My other new intial value"
};

s4.SomeProperty = "A value"; //CS8852 ERROR. SomeProperty is enabled to only be initialized in constructor or intialize block.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1502476

An init-only property doesn't require that properties are set at creation time with the property initializer syntax. It allows them to be set that way instead of requiring that all read-only properties are set by constructors.

So with your code,

var s = new SomeClass();

... is still perfectly valid, and would end up with s.SomeProperty being null, contrary to the nullability of its type.

Upvotes: 7

Related Questions