Cast object to decimal? (nullable decimal)

If you do not want to parse strings, but want to ensure that you receive either null, a decimal or a nullable decimal, then you could do something like this:

public static Nullable<T> Convert<T>(object input) 
    where T : struct
{
    if (input == null)
        return null;
    if (input is Nullable<T> || input is T)
        return (Nullable<T>)input;
    throw new InvalidCastException();
}

You could make it return null on the last line instead if you wished to avoid exceptions, although this would not distinguish between real null values and bad casts.

Note that you have to use the "is" operator, as the "as" operator does not work on value types, and casting without checking may thrown an InvalidCastException.

You could also make it an extension method:

public static class ObjectExtensions
{
    public static Nullable<T> ToNullable<T>(this object input)
        where T : struct
    {
        if (input == null)
            return null;
        if (input is Nullable<T> || input is T)
            return (Nullable<T>)input;
        throw new InvalidCastException();
    }
}

And use it like this:

object value = 123.45m;
decimal? dec = value.ToNullable<decimal>();

This will help avoid code contract warnings about unboxing null references.


and if you use decimal? temp = (decimal?)value;


you should parse the decimal. But if you want your decimal to be null when the string is not correct, use TryParse :

decimal parsedValue;
decimal? temp = decimal.TryParse(value, out parsedValue)
                ? value
                : (decimal?)null;

This way you will avoid exceptions while parsing ill formated strings.

Almost all primitive types provide a Parse and TryParse methods to convert from string.

Is is also recommended to pass a culture for the provider argument to the method to avoid problems with the decimal separator. If you're reading from another system, CultureInfo.InvariantCulture is probably the way to go (but it's not the default).

bool TryParse(string s, NumberStyles style,
  IFormatProvider provider, out decimal result)

Unboxing only works if the type is identical! You can't unbox an object that does not contain the target value. What you need is something along the lines of

decimal tmpvalue;
decimal? result = decimal.TryParse((string)value, out tmpvalue) ?
                  tmpvalue : (decimal?)null;

This looks whether the value is parsable as a decimal. If yes, then assign it to result; else assign null. The following code does approximately the same and might be easier to understand for people not familiar with the conditional operator ?::

decimal tmpvalue;
decimal? result = null;
if (decimal.TryParse((string)value, out tmpvalue))
    result = tmpvalue;