I have a function:
public static object? ToType(Type type, object? value)
I want to create a helpful shortcut like:
public static T ToType<T>(object? value)
{
return (T)ToType(typeof(T), value);
}
C# 8.0 compiler with nullable checks enabled gives the following warning:
CS8601
: Possible null reference assignment.
Function accepts string
, int
and other types, so I cannot set a generic constraint like class
. Also, it can return null
. Using T?
for casting and return gives a compiler error.
Is there a way to fix the warning?