Avoid CS8618 warning when initializing mutable non nullable property with argument validation

For now you can avoid this warning by initializing a _name field using default value with null-forgiving operator !, like private string _name = default!; or private string _name = null!; There is also an open GitHub issue for that. You can also declare the _name as string? and specify that return value of Name … Read more

Nullable reference types: How to specify “T?” type without constraining to class or struct

What to do if you are using C# 9 In C# 9, you can use T? on an unconstrained type parameter to indicate that the type is always nullable when T is a reference type. In fact, the example in the original question “just works” after adding ? to the property and constructor parameter. See … Read more

How to treat ALL C# 8 nullable reference warnings as errors?

It is now possible to treat all nullable-related warnings as errors without explicitly specifying them all. To achieve this, you have to set <WarningsAsErrors>Nullable</WarningsAsErrors> in your *.csproj file [source]. Full example: <Project Sdk=”Microsoft.NET.Sdk.Web”> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <Nullable>enable</Nullable> <WarningsAsErrors>Nullable</WarningsAsErrors> </PropertyGroup> </Project>

Nullable reference types with generic return type

You were very close. Just write your method like this: [return: MaybeNull] public T Get<T>(string key) { var wrapper = cacheService.Get(key); return wrapper.HasValue ? Deserialize<T>(wrapper) : default!; } You have to use the default! to get rid of the warning. But you can tell the compiler with [return: MaybeNull] that it should check for null … Read more

How to use .NET reflection to check for nullable reference type

In .NET 6, the NullabilityInfoContext APIs were added to handle this. See this answer. Prior to this, you need to read the attributes yourself. This appears to work, at least on the types I’ve tested it with. public static bool IsNullable(PropertyInfo property) => IsNullableHelper(property.PropertyType, property.DeclaringType, property.CustomAttributes); public static bool IsNullable(FieldInfo field) => IsNullableHelper(field.FieldType, field.DeclaringType, field.CustomAttributes); … Read more

What is the ?[]? syntax in C#?

Step by step explanation: params Delegate?[] delegates – It is an array of nullable Delegate params Delegate?[]? delegates – The entire array can be nullable Since each parameter is of the type Delegate? and you return an index of the Delegate?[]? array, then it makes sense that the return type is Delegate? otherwise the compiler … Read more

Why does this code give a “Possible null reference return” compiler warning?

I can make a reasonable guess as to what’s going on here, but it’s all a bit complicated 🙂 It involves the null state and null tracking described in the draft spec. Fundamentally, at the point where we want to return, the compiler will warn if the state of the expression is “maybe null” instead … Read more

How to enable Nullable Reference Types feature of C# 8.0 for the whole project

In Visual Studio 16.2 (from preview 1) the property name is changed to Nullable, which is simpler and aligns with the command line argument. Add the following properties to your .csproj file. <PropertyGroup> <Nullable>enable</Nullable> <LangVersion>8.0</LangVersion> </PropertyGroup> If you’re targeting netcoreapp3.0 or later, you don’t need to specify a LangVersion to enable nullable reference types. Alternatively, … Read more

The annotation for nullable reference types should only be used in code within a ‘#nullable’ context

For anyone ending up here. You can put #nullable enable on top of the file for a file-by-file approach as suggested by @Marc in the comments. You can also use combinations of #nullable enable/disable to annotate just parts of the file class Program { static void Main(string[] args) { #nullable enable string? message = “Hello … Read more