How Does Nullsafety in C# Fit Into the Type System? #6471
-
Hi I come from Dart background I want to have a deeper understanding of how null safety works in C#. In Dart, it is easy for me to understand null safety in terms of type hierarchy for example Thank you all. I went through the docs, but couldn't get the right answers. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's just a compile-time annotation to help developers be wary of potential nulls. Under the hood string or string? is the same type. "!" is just a "a know better than you, compiler" indicator. There are no casts involved. It's a different story with Nullable type - it's designed to enable "nullness" for ValueTypes. It's essentially a wrapper - a different type - which holds some value inside it and indicates whether that value was initialized properly or not (kinda "null"). It has a special runtime treatment in some cases like boxing. So basically "referenceType? (e.g. string?)" is just an compile time annotation and "valueType? (e.g. int?)" is a container type wrapping original value. |
Beta Was this translation helpful? Give feedback.
It's just a compile-time annotation to help developers be wary of potential nulls. Under the hood string or string? is the same type. "!" is just a "a know better than you, compiler" indicator. There are no casts involved.
It's a different story with Nullable type - it's designed to enable "nullness" for ValueTypes. It's essentially a wrapper - a different type - which holds some value inside it and indicates whether that value was initialized properly or not (kinda "null"). It has a special runtime treatment in some cases like boxing.
So basically "referenceType? (e.g. string?)" is just an compile time annotation and "valueType? (e.g. int?)" is a container type wrapping original value.