Skip to content
Maximilian edited this page Mar 1, 2022 · 49 revisions

Main goal

Go through the null references migration tutorial at https://docs.microsoft.com/dotnet/csharp/nullable-migration-strategies

Prerequisites

Visual Studio installation (16.3.0 or later)

Understanding the nullable references feature

What is the problem? Why is null to be avoided if possible?

https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/

How does the new C# 8.0 "best practice" code look like?

More clarity when expressing intent. See https://docs.microsoft.com/dotnet/csharp/whats-new/tutorials/nullable-reference-types and https://docs.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-8.0/nullable-reference-types

What is the obviously implied but not too clearly stated design goal?

Backwards compatibility and easy migration.

How is the feature defined?

The main documentation page is at https://docs.microsoft.com/dotnet/csharp/nullable-references

Not for the faint of heart, but quite useful if you're into compilers, see https://docs.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-9.0/nullable-reference-types-specification

Other relevant features

https://docs.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator

https://docs.microsoft.com/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-

https://docs.microsoft.com/dotnet/csharp/language-reference/attributes/nullable-analysis

Is it that easy? Gotchas

https://www.infoq.com/articles/csharp-nullable-reference-case-study/

https://codeblog.jonskeet.uk/2019/02/10/nullableattribute-and-c-8/

Assignment

Go through the null references migration tutorial at https://docs.microsoft.com/dotnet/csharp/whats-new/tutorials/nullable-reference-types

Extra assignments

Compare JCL + Kotlin decisions to the BCL + C# decisions

Implement a generic Map / Dictionary that does not allow for null values. Do it in both Kotlin and C# and compare the resulting code and the run-time implications.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html#get(java.lang.Object)

https://docs.microsoft.com/dotnet/api/system.collections.generic.dictionary-2.item

Interesting read: http://www.natpryce.com/articles/000818.html

Run-time representation

Look over https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-metadata.md Write code that does not have this feature enabled (or using the old tool-chain) and compile + disassemble; try to also use generics, value types and other nice edge-cases that you can think of for this feature. Modify the code to enable this feature using the new tool-chain (potentially add some nullable types to the existing code) and compile + disassemble. Compare the output.

You may find these helpful:

https://codeblog.jonskeet.uk/2019/02/10/nullableattribute-and-c-8/

https://blog.rsuter.com/the-output-of-nullable-reference-types-and-how-to-reflect-it/

https://github.com/dotnet/runtime/issues/29039

Bonus: figure out how https://github.com/RicoSuter/Namotion.Reflection implements run-time reflection of nullability. Implement your own light-weight version of what this does and/or find and fix bugs in that implementation.

Interesting links and further references

Recommended timeline for migrating to using this: https://devblogs.microsoft.com/dotnet/embracing-nullable-reference-types/

Noda time migration: https://codeblog.jonskeet.uk/2018/04/21/first-steps-with-nullable-reference-types/

List of related warnings: https://edument.se/en/news/exploring-the-non-nullable-type-warnings-in-c-8

https://github.com/dotnet/csharplang/issues/36 and https://github.com/dotnet/csharplang/discussions/790 If you ever want to add a feature to the C# language...

https://habr.com/en/company/pvs-studio/blog/455234/ static analysis, pros and cons, emotional programming, that apparently didn't make it to the final release of the feature...

https://www.infoq.com/news/2019/06/CSharp-8-More-Nullable-Reference/ stuff you may not think of when adding such a feature, but stuff that does actually come up...

https://github.com/dotnet/csharplang/issues/2145 the "dammit" operator could have done more! But that is quite controversial (Load 137 hidden items?)...

https://stackoverflow.com/questions/54593923/nullable-reference-types-with-generic-return-type https://stackoverflow.com/questions/55975211/nullable-reference-types-how-to-specify-t-type-without-constraining-to-class https://devblogs.microsoft.com/dotnet/try-out-nullable-reference-types/ some issues with generics and nullable types; also https://docs.microsoft.com/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#notnull-constraint

Announcements of the feature https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-8#nullable-reference-types and https://devblogs.microsoft.com/dotnet/announcing-net-core-3-0/

First Visual Studio version with full C# 8.0 support: https://devblogs.microsoft.com/visualstudio/dot-net-core-support-in-visual-studio-2019-version-16-3/

How Entity Framework is already using nullable reference types: https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/

Nice early overview http://code.fitness/post/2019/02/nullableattribute.html

Really early post on this whole thing https://devblogs.microsoft.com/dotnet/building-c-8-0/

Interesting insights into API design using this https://softwareengineering.stackexchange.com/questions/387674/c-8-non-nullable-references-and-the-try-pattern

Not directly related to any of this, but really good to know in case you didn't already read this one... https://devblogs.microsoft.com/dotnet/net-core-is-the-future-of-net/

A write-up from JetBrains https://blog.jetbrains.com/dotnet/2020/05/26/nullable-contexts-nullable-attributes/

And a webinar https://blog.jetbrains.com/dotnet/2020/07/13/how-to-stop-worrying-and-adopt-nullable-reference-types-webinar-recording/

C# 9.0 follow-up https://daveabrock.com/2020/06/24/simplified-null-validation

Null & Void – Everything About Nothing in .NET, by Stefan Pölz https://www.youtube.com/watch?v=h7uVMKPHGtM

Async/Await Calls Gotcha with the CSharp ? Null Propagator https://weblog.west-wind.com/posts/2021/May/15/Async-Await-with-the-Null-Propagator

Description of the feature on Channel 9 https://channel9.msdn.com/Shows/On-NET/C-Language-Highlights-Nullable-Reference-Types

Jetbrains tooling for nullable types https://blog.jetbrains.com/dotnet/2021/06/29/whats-new-for-csharp-nullable-reference-types-in-resharper-and-rider-2021-2-eap/

C# Nullable Features thru the times https://csharp.christiannagel.com/2022/02/14/nullable/

C# 11.0 preview: parameter null checking https://endjin.com/blog/2022/02/csharp-11-preview-parameter-null-checking

Part of the GoTech World 2019 agenda

Full workshop description follows:

C# 8.0 Nullable Reference Types

The new language version, expected to launch in September 2019 together with .NET Core 3.0, introduces nullable reference types, aiming to prevent null reference exceptions. We will look at the feature and discover together why it was added to the language, how it works in practice and how to migrate an existing codebase to using it. We will roughly follow the official documentation tutorial ("Migrate existing code with nullable reference types" here https://docs.microsoft.com/dotnet/csharp/tutorials/upgrade-to-nullable-references ) and explore the topics that come up during the workshop. If you've already gone through the tutorial do not despair: there will be more advanced assignments and questions that you can explore on your own and everyone will be assigned a partner to help out when in trouble, so there should be plenty of opportunity to not get bored. Prerequisites: Visual Studio installation with support for C# 8.0. Other toolsets (Rider, Visual Studio Code, etc.) can be used, but it is unlikely that there will be time to address any differences from Visual Studio or issues that come up during the workshop.