GetValueOrThrow for Nullable Types

Topics: C# Language Design
Jan 8, 2015 at 5:26 AM
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));

          return nullable.Value;
     }
}
Is it possible to have some addition in API to support this?
Jan 8, 2015 at 5:58 AM
punitganshani wrote:
Most often for nullable types we end up writing code like...
Not in my experience. If you're using a nullable type then it means that you're accepting null as a valid value. Of course, in many cases you'll probably need to have special handling for it but throwing an exception is not the norm.
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,
As far as I can tell your suggestion has nothing to do with the C# language, this looks like suggestion for the .NET Framework itself so it doesn't belong here.
With nameof operator, this is achievable as well.
You make it sound like this isn't achievable without nameof. You can simply write "nullable" instead of nameof(nullable). nameof is just a convenience feature, it doesn't enable anything new.
Jan 8, 2015 at 6:48 AM
I agree.. nameof(..) was wrong there.

Throwing Null exception and giving back default values are few of the special handling used with nullable types.
GetDefaultValue(..) is available for defining a default value but there is no provision for throwing exceptions in case some part of the code needs the same variable to be non-Nullable
Jan 8, 2015 at 12:12 PM
punitganshani wrote:
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));

          return nullable.Value;
     }
}
Is it possible to have some addition in API to support this?
Why doesn't this work for you?
int? ni = null;
int? nj = 1;

int i = (int)ni;
int j = (int)nj;