Would like to implement a method like the following by using the C#8.0 Nullable Check feature:
public static T Min<T>(IEnumerable<T> objects) where T : IComparable<T>
{
bool first = true;
T result = default; // <---- CS8653
foreach (var obj in objects)
{
// Bla Bla
}
return result;
}
I get an error at the marked line (fourth line):
MathHelper.cs(129,24,129,31): warning CS8653: Ein Standardausdruck führt einen NULL-Wert ein, wenn "T" ein Verweistyp ist, der keine NULL-Werte zulässt.
English translation: A default value introduces a null value, if T is a reference type which does not allow null values...
But now the challenge:
1) I'd like to have this function for 'structs' and for 'classes', so introducing "where class" or "where struct" is not an option.
2) T? result = default givers an error:
CS8627 Ein Nullable-Typparameter muss als Werttyp oder als Nicht-Nullable-Verweistyp bekannt sein. Sie sollten eine class-, struct- oder eine Typeinschränkung hinzufügen. BurnSystems
3) T! result = default also does not work..,..
Anybody having an idea how to get the nullable check feature running for generic functions which allow a reference and value type while the reference value may be null....
EDIT: The project file:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>