Home > database >  What is the result of initiating members with default! statement in C# .net?
What is the result of initiating members with default! statement in C# .net?

Time:08-16

Yes, I tried to google around and found everything possible from null-coalescing operator to default keyword, which everything kind of bypasses (as of now) my curiosity about these statements:

public record Person
{
    public string FirstName { get; init; } = default!;
    public string LastName { get; init; } = default!;
};

The snippet comes from this MS official site.

So while I can try out what this code yelds in runtime record instantiation, can anyone please generally explain what is the beauty and magic behind the default! statements ?

Thank you.

CodePudding user response:

default keyword on it's own says use the default value for this type, in this case a null since string is a reference type.

The ! suffix is the null forgiving operator. It suppresses any warnings about a value potentially being null. Without it in this code, you would the following warning which is a feature of nullable reference types:

CS8625: Cannot convert null literal to non-nullable reference type

CodePudding user response:

There are two things to consider

  1. The default keyword denotes the value of a type where all the bits are set to 0. E.g. for an int this is 0, for a double it is 0.0 and for a reference type like string, this is null.
  2. In a Nullable context the two properties are non-nullable, therefore, you are in principle not allowed to assign them null. If you know it better than the compiler, you can use the ! (null-forgiving) operator to tell the compiler that null is okay in this case.

Why assign default!, i.e., null to those properties? Declaring a property with an init accessor does not ensure that the constructor will be called with an object initializer setting these properties to a non-null value. Even if you know that your code will be doing it. The purpose of this = default! property initializer is simply to suppress any compiler warnings about this issue.

C# 11 will introduce a required modifier to force initialization of a property and to suppress this compiler warning.

public required string FirstName { get; init; }

A code using this property will not compile until you either initialize the property in the constructor or in an object initializer.

CodePudding user response:

As pointed out by @SouXin, you can use this official documentation link to check the default value of each C# type.

However, in your case, since string is a reference type, default! is the same as null!. This assignment is used to tell the compiler that the developer is sure that the property is never null, by using !, which suppresses the null warning. This is part of the nullable feature.

Otherwise, the compiler would return warning, saying that FistName and LastName can be null, and thus, should be of type string?.

This is just an intermediate solution, starting with C# 11, the recommended solution of telling the compiler that a property won't be null is by using the required keyword.

So instead of having:

public string FirstName { get; init; } = default!;

you would use:

public required string FirstName { get; init; }
  • Related