3 ms·
Introducing Nullable Reference Types in C#
- mephitix 9y agoThis looks a lot like Swift optionals - which to me is a great thing. 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.
- cm2187 9y agoI don’t know if it can be called a breaking change but it does feel like this is a pretty big deal. All your existing solutions are going to start throwing hundreds of warnings. 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.
- styfle 9y agoThis is great! TypeScript (another Microsoft language) did something similar not long ago but used a flag to opt-in to strict null checks. It seems this setting is strongly encouraged. 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.