Lead Product Evangelist @Kentico, Founding partner @craftbrewingbiz. love to learn / teach web dev & software engineering, collecting vinyl records, mowing my lawn, craft 🍺
Reference types can still be null in C#8... they just behave more like value types.
They have to explicitly opt into null.
This is the big problem with null and reference types currently... null is not the same as the type itself, it is a separate type.
All reference types in C# are type unions of the type and null but the language doesn't flow this type unions through our code so it's very easy to overlook.
Yes, that's why the concept is called nullable reference types (though I think it should have been called non-nullable reference types, since that's the big change...)
I think the problem with nulls is that many developers, even well experienced, sometimes forget the simple fact that any reference type might be null, especially if it's exposed to changes outside your class (like a get;set; property for example). In fact, as the years go by I find myself more and more in agreement with SO Legend Jon Skeet - as often as you can, make your types immutable. Of course, that doesn't prevent the need for null validations, but at least inside your own types you can be safe from nulls (and other unexpected behaviors).
Lead Product Evangelist @Kentico, Founding partner @craftbrewingbiz. love to learn / teach web dev & software engineering, collecting vinyl records, mowing my lawn, craft 🍺
Ya, strict constructors, guard clauses, and immutable state (or at least protected invariants) can go a long ways towards evicting null from your codebase (except where it's appropriate, of course).
If we take an Onion architecture approach for our application, and guard against nulls on the outside of our domain, then we can be safe from it within our business logic, which is where we typically have issues with null.
I like that there are many different ways to protect against or at least carefully handle the case of reference types with null values.
That's typically what I gravitate towards - validating on public methods in public classes - then farming things out to private methods or internal classes.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Reference types can still be null in C#8... they just behave more like value types.
They have to explicitly opt into null.
This is the big problem with null and reference types currently... null is not the same as the type itself, it is a separate type.
All reference types in C# are type unions of the type and null but the language doesn't flow this type unions through our code so it's very easy to overlook.
Yes, that's why the concept is called nullable reference types (though I think it should have been called non-nullable reference types, since that's the big change...)
I think the problem with nulls is that many developers, even well experienced, sometimes forget the simple fact that any reference type might be null, especially if it's exposed to changes outside your class (like a get;set; property for example). In fact, as the years go by I find myself more and more in agreement with SO Legend Jon Skeet - as often as you can, make your types immutable. Of course, that doesn't prevent the need for null validations, but at least inside your own types you can be safe from nulls (and other unexpected behaviors).
Ya, strict constructors, guard clauses, and immutable state (or at least protected invariants) can go a long ways towards evicting
null
from your codebase (except where it's appropriate, of course).If we take an Onion architecture approach for our application, and guard against nulls on the outside of our domain, then we can be safe from it within our business logic, which is where we typically have issues with
null
.I like that there are many different ways to protect against or at least carefully handle the case of reference types with
null
values.That's typically what I gravitate towards - validating on public methods in public classes - then farming things out to private methods or internal classes.