I want to write a generic extension method that throws an error if there is no value. SO I want something like:
public static T GetValueOrThrow(this T? candidate) where T : class
{
if (candidate.HasValue == false)
{
throw new ArgumentNullException(nameof(candidate));
}
return candidate.Value;
}
- C# is not recognizing the T: The type or namespace name "T" cannot be found
- C# is not recognizing where : Constraints are not allowed on non generic declarations
Any idea if this works? What am I missing?
I also come up with:
public static T GetValueOrThrow<T>(this T? candidate) where T : class
{
if (candidate.HasValue == false)
{
throw new ArgumentNullException(nameof(candidate));
}
return candidate.Value;
}
Now C# complains about the candidate: The type T must be a non nullable value type in order to use it as parameter T in the generic type or method Nullable
This is not related to comparing.