-4

I am using VS 2022 and its default version of c#. The following lines of code are being compiled succefully.

enter image description here

I was trying to find out what does =null! mean. It seams that it is one of a new features of c# 10 that was not spoken about yet...

2
  • 3
    Assigning null with null forgiven operator ! learn.microsoft.com/bs-cyrl-ba/dotnet/csharp/language-reference/… Commented Dec 5, 2021 at 15:53
  • 5
    " It seams that it is one of a new features of c# 10 that was not spoken about yet..." - dude, C# 10 is released for more than a week now. This includes FULL DOCUMENTATION. "not spoken about" is more like "not bothered to read about". One does not wait for a blog post somewhere, one goes to the documentation and reads the what is new section. You will not find this there, though, because it is NOT a C# 10 feature - you must go back to C# 8 for that - learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Commented Dec 5, 2021 at 16:06

1 Answer 1

2

If you enable Nullable Reference Types, the compiler will ensure that all non-nullable reference types are initialized after object creation. So they should be initialized either in constructor or in-place.

But for some classes, this is not useful, because they'll be initialized by reflection. This mainly relates to DTO or Entity Framework entities that are used in reflection-heavy frameworks.

So there exists a syntax to tell the compiler to "shut up, I know it's null, trust me". In this line you explicitly assign null to the field and tell the compiler to trust you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.