Can I return null value for one of the items in a Tuple?
Yes, you can. If you do this it works:
var tuple = Tuple.Create<string, int>(null, 42);
What you tried to was have the compiler determine the type for the null
and it can't do that so you have to explicitly provide the generic types.
So, in your case, try this:
return Tuple.Create<T, HttpStatusCode>(null, webResponse.StatusCode);
You would also need to add the generic class
constraint to your method to allow null
to be cast to T
.
internal static Tuple<T, HttpStatusCode> GetRequest(arg1, arg2...)
where T : class
You can use the simple constructor: new Tuple<T, HttpStatusCode>()
or Tuple.Creare
. The tricky part here is that you need to cast null to your generic type, so it should allow nulls.
Alter your class declaration to support nulls:
internal sealed class OnlineHelper<T> where T: class
And later cast or use default(T)
return new Tuple<T, HttpStatusCode>((T)null, webResponse.StatusCode)