O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Best Practices for Nullable Reference Types in C#

33 visualizações

Publicada em

Of all the new features in C# 8, nullable reference types have the largest impact on our code. A year after the release we'll take a closer look at the feature. After covering the basics to refresh our memory, we'll move on to the challenges that the feature can impose, including:
- Introducing nullable reference types into an existing codebase
- Support in libraries
- Advanced annotations
- Support in Entity Framework Core

Publicada em: Tecnologia
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Best Practices for Nullable Reference Types in C#

  1. 1. Best practices for nullable reference types in C# Damir Arh
  2. 2. Agenda • What are nullable reference types? • Updating existing code • Support by libraries • Annotation attributes • Support in EF Core
  3. 3. „Billion-dollar mistake“ • Tony Hoare (2009): • „I call it my billion-dollar mistake. It was the invention of the null reference in 1965.“ • Trend of strict null-checking in many languages
  4. 4. C# implementation • Based on the approach already used for value types • Nullable values must be explicitly allowed • Static analysis warns about unsafe access • Functionality is disabled by default
  5. 5. Hands-on nullable reference types DEMO
  6. 6. Key features • Changes meaning of reference types • Are reference types enabled or not? • Static analysis is not perfect • Intentionally ignore using the new null-forgiving operator • Global state • Multithreaded applications • Interaction with non-annotated code
  7. 7. Prerequisites • C# 8 • Runtime support for .NET Standard 2.1 • .NET Core 3.1+ • Latest versions of Xamarin, Mono • Future versions of UWP, Unity? • No official support for .NET framework • Per-project opt-in • Project properties • <Nullable>enable</Nullable>
  8. 8. Incrementally updating existing code DEMO
  9. 9. #nullable directive • Opt-in/opt-out • enable • disable • restore • Defines context • annotations • warnings • Not recommended
  10. 10. Support by libraries DEMO
  11. 11. Transitional period • Not all libraries are fully annotated • Still a work in progress even in .NET 5: http://bit.ly/DotNet5NullableAnnotations • Extra work for open-source library maintainers • Microsoft’s plans • Work complete for .NET 6 (November 2021) • Feature enabled by default for new projects?
  12. 12. Nullable annotations in libraries • Libraries must still work with older runtimes • Limited C# 8 support with .NET Standard 2.0 • Annotated types visible only to supported clients • Multi-targeting in advanced scenarios • Recommendation: .NET Standard 2.0 in .NET Core 3.1 • Benefits: • Take advantage of other C# 8 features • More flexibility more type annotations
  13. 13. Annotation attributes DEMO
  14. 14. Advanced nullable annotations • Constraint on generic type parameters: notnull • Additional attributes • Description of input values: AllowNull, DisallowNull • Description of output values: MaybeNull, NotNull • Conditional: MaybeNullWhen, NotNullWhen, NotNullIfNotNull • Official documentation: https://bit.ly/AnnotatingNullableTypes
  15. 15. Entity Framework Core DEMO
  16. 16. Support in EF Core • Fake initialization in context • Nullable types in models • Instead of Required attribute • Initialization in constructor • Fake initialization of required navigation properties • Attention required when enabling nullable reference types • Additional details: https://bit.ly/EFCoreNullableTypes
  17. 17. Takeaways • Nullable types do not provide full null-safety • Feature is useful despite that • Recommended for new projects • Caution required with existing projects • No support for .NET framework

×