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.
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 receive the following warning which is a feature of nullable reference types:
CS8625: Cannot convert null literal to non-nullable reference type