-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...

  • 3
    Assigning `null` with *null forgiven operator* `!` https://learn.microsoft.com/bs-cyrl-ba/dotnet/csharp/language-reference/operators/null-forgiving – Dmitry Bychenko Dec 05 '21 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 - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving – TomTom Dec 05 '21 at 16:06

1 Answers1

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.

Oleh Nechytailo
  • 2,155
  • 17
  • 26