Introducing Nullable Reference Types in C#
blogs.msdn.microsoft.com
blogs.msdn.microsoft.com
Looking at the examples, the compiler can determine if not null by reading the if statement like so
if (str != null)
But what about if the code uses the following? if (!string.IsNullOrEmpty(str))
TypeScript has a way to solve this using the `is` keyword but I'm not sure if this is introduced in C# yet.In particular these future warnings should be all over everyone’s code:
1. Assigning or passing null to a non-nullable reference type: many functions have non optional arguments and for some actually expect null as an argument
6. Leaving a nonnullable field of a newly constructed object null after construction: there are many reasons why you may want to populate a field at a later stage, say you create two objects and you want them to cross reference each other’s.
It’s basically a fundamental change in the expected behaviour of reference types.
I haven't tried this out yet but I also would love to see Swift-like optional binding (if let...) While it's great if the C# compiler + intellisense could figure it out I prefer having different vars to differentiate nullability/non-nullability.