I want to create a simple method that accepts both value type and reference type parameters, i.e. int is value, and string is reference.
So this is what I start with:
public bool areBothNotNull<T>(T? p1, T? p2)
{
return (p1.HasValue && p2.HasValue);
}
So I want to be able to use it like this:
var r1 = areBothNotNull<int>(3, 4); // will be true
var r2 = areBothNotNull<int>(3, null); // will be false
var r3 = areBothNotNull<string>("three", "four"); // will be true
var r4 = areBothNotNull<string>(null, "four"); // will be false
But the first issue I encounter is
The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
To proceed I add a struct constraint to my method
public bool areBothNotNull<T>(T? p1, T? p2) where T : struct
But now the method won't accept the string based calls, and gives me this error:
The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method.
Is this possible? Or why are we prevented from doing this?