I use C# 8 nullable reference types.
I have a generic class that might accept nullable reference type as a type parameter.
Is there a way to declare non-nullable type based on generic type parameter that might be nullable reference type (or even Nullable struct)?
abstract class Selector<T>
{
T SelectedItem;
// how to make item parameter not nullable?
abstract string Format(T! item);
// how to make item parameter not nullable?
Func<T!, string> FormatFunction;
}
Use DisallowNullAttribute
:
using System.Diagnostics.CodeAnalysis;
abstract class Selector<T>
{
T SelectedItem;
public abstract string Format([DisallowNull] T item);
}
var selector = default(Selector<string?>)!;
selector.Format(null); //warning CS8625: Cannot convert null literal to non-nullable reference type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With