C# 8.0 introduced the Nullable reference types:
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#nullable-reference-types
In short, this new feature allows the specification of a reference variable or property to be null, enabling compiler warnings in case of possible null-reference exception.
Anyway, in case of generic types, the developer is forced to specify whether the generic type must be a struct or a reference type. In other words, this would result in a compiler error:
public class Foo<T>
{
public T? Bar { get; set; }
}
[CS8627] A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
My question is: is there a way to make generic parameters work both with struct and reference types? Is it mandatory to write two different implementation for struct and reference types?