Most often for nullable types we end up writing code like
int? numerator = GetSomeIntValue();
int? denominator = GetSomeIntValue();
if (denominator.HasValue == false)
throw new InvalidDataException("denominator needs to have some value");
var result = numerator.GetValueOrDefault(-1) / denominator.GetValueOrDefault(-1);
The nullable types provide us a way to get default value for types that are null. For cases where we don't want to proceed when the value is null, we still have to check if the value is null and then throw the exception.
While the focus on C# 6.0 is to write cleaner code, can we have an additional method that allows us to transform the code to something like,
var result = numerator.GetValueOrDefault(-1) / denominator.GetValueOrThrow();
With nameof operator, this is achievable as well
public static T GetValueOrThrow<T>(this Nullable<T> nullable) where T : struct
if (nullable.HasValue == false)
throw new NullReferenceException(nameof(nullable));
Is it possible to have some addition in API to support this?