

C# 8 Nullable Reference Types Update
source link: https://www.tuicool.com/articles/V7NR3ar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Work continues on nullable refence types for C# 8, revealing edge cases that need to be addressed before the final release and new opportunities for reducing the amount of boilerplate developers have to write.
Here are some of the open questions for nullable reference types:
-
Which takes precedence,
MaybeNull
orNotNull
? -
Should
AllowNull
andNotNull
be allowed on value types? -
Should
!
change top level nullability when there was no warning? - Should ref arguments always be assumed to be assigned to?
- Which conversions should preserve the nullable state of members?
-
Should we support
var?
, and if so, what are the open issues? -
Along those lines, would we allow
var? (x, y) = ...
deconstructions? -
Should we permit (or forbid)
typeof(string?)
, given that we can never honor the nullable annotation? - When we compute a nullable annotation in flow analysis, should we use the context?
- Should the result of a dynamic invocation be treated as non-null, as it is generally outside the scope of static analysis?
Other questions have been answered. Here are some of the highlights that may affect how code is written.
x?.F()
Declarative Argument Null Checks
One of the holes in nullable reference types is they are merely a suggestion as far as the compiler is concerned. Even if you mark a parameter as non-nullable, there is nothing to stop someone from accidentally passing in a null, especially when using reflection or dynamic. Which means you still need to add a null argument check at the beginning of your function.
With the Simplified parameter null validation , you’ll be able to tell the compiler it should create the null check for you. Two versions of the syntax have been proposed.
void Insert(string s!) void Insert(string! s)
In both cases the bang operator ( !
) is used, either on the type name or the parameter name. The argument for the first version is it is an implementation detail of the function and not part of the method signature. The argument for the second version is it is more consistent with the nullable version ( void Insert(string? s)
) and perhaps it should be of the method signature. This is significant because if it’s part of the signature, then it can be applied to interface methods as well. And this then opens up questions about inheritance and overriding methods.
Another question is how it will be implemented. Options include:
- C# emits the check inline in IL
- C# emits a call to a helper method in IL
- C# just emits a tag, allowing the JIT process to decide how to perform the check
Another question is what happens when the type is nullable ( int?
) but the null check is desired. If supported, this may result in an contradiction in the signature ( void Insert(int?! i)
).
Not yet discussed is using an attribute ( [NotNull]
) instead of custom syntax. While more verbose, it would set a precedent for other forms of declarative parameter checking.
Recommend
-
64
In the few next posts I’d like to share with you some of the most interesting C# 8.0 features. Today we’re going to start with examining nullable reference types. Let’s see then Learn about building C# 8.0...
-
46
The introduction of nullable reference types in C# represents the biggest change to how .NET developers write code since async/await. Once it goes live, countless libraries will have to be updated with nullable annotation...
-
35
Phillip August 6th, 2019 Try out Nullable Reference Types With the release of .NET Core 3.0 Previ...
-
13
The C# Nullable Reference Types Preview Welcome to the preview implementation of C# Nullable Reference Types! Nullable reference types are a feature currently planned for C# 8.0. It is introduced in
-
10
Nullable reference types; CSharp's very own strictNullChecks 'Tis the season to play with new compiler settings! I'm a very keen TypeScript user and have been merrily using
-
11
Posted on March 18, 2021March 18, 2021 Protected: How to Stop NullReferenceExceptions in .NET: Implementi...
-
8
Nullable Reference types in C# – Best practicesIn recent years, there’s been a trend of strict null checking in programming languages: TypeScript has the strictNullChecks option. In Kotlin (preferred language for Andr...
-
7
C# Language Highlights: Nullable Reference Types
-
11
This article gives you a toolset for stopping NullReferenceExceptions NullReferenceExceptions in .NET code. The article centers around
-
5
Using Nullable Reference Types Introduction One of the major sources of unexpected runtime errors is null references. With nullable reference types you can make the compiler check for null references, and ma...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK