C# compiler throws Language Version (LangVersion) reference error “Invalid ‘nullable’ value: ‘Enable’ for C# 7.3”

In my case, I ran into this problem with Visual Studio 2022 when I changed the target framework from .NET Standard 2.1 to .NET Standard 2.0. I solved my problem by removing <Nullable>enable</Nullable> in the .csproj file and restarting Visual Studio. Original .csproj file: <PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> <Nullable>enable</Nullable> </PropertyGroup> New .csproj file: <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup>

Why do we get possible dereference null reference warning, when null reference does not seem to be possible?

This is effectively a duplicate of the answer that @stuartd linked, so I’m not going to go into super deep details here. But the root of the matter is that this is neither a language bug nor a compiler bug, but it’s intended behavior exactly as implemented. We track the null state of a variable. … Read more

Nullable reference type in C#8 when using DTO classes with an ORM

You can do either of the following: EF Core suggests initializing to null! with null-forgiving operator public string ServiceUrl { get; set; } = null! ; //or public string ServiceUrl { get; set; } = default! ; Using backing field: private string _ServiceUrl; public string ServiceUrl { set => _ServiceUrl = value; get => _ServiceUrl … Read more

Using Linq’s Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method

You have to update your extension method to the following public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> enumerable) where T : class { return enumerable.Where(e => e != null).Select(e => e!); } The point here is that you are converting the IEnumerable of nullable references to not nullable ones, therefore you’ll have to use IEnumerable<T?>. where T … Read more

How to use C# 8.0 Nullable Reference Types with Entity Framework Core models?

There is no proper way to handle non-nullable navigational properties. Documentation suggests two ways and both are not type safe. Use a backing field and throw InvalidOperationException. It is unclear how it differs from doing nothing and have a NullReferenceException Suppress it with null forgiving operator Official documentation link: https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#non-nullable-properties-and-initialization

How can I hint the C# 8.0 nullable reference system that a property is initalized using reflection

Whenever you want to tell the compiler “shut up, I know what I’m doing” in regards to nullable reference types, use the ! operator. You can fix your issue by declaring your properties like so: public DbSet<Probe> Probes { get; set; } = null!; public DbSet<ProbeUnitTest> ProbeUnitTests { get; set; } = null!;