C# Type suffix for decimal

2019-03-22 15:37发布

I don't know what the correct wording is for what I am trying to achieve so it may already be posted online. Please be kind if it is.

Ok so basically I have this method.

public static T IsNull<T>(IDataReader dr, String name, T nullValue)
{
    return Helpers.IsNull(dr, dr.GetOrdinal(name), nullValue);
}

public static T IsNull<T>(IDataReader dr, Int32 index, T nullValue)
{
    if (dr.IsDBNull(index))
    {
        return nullValue;
    }
    else
    {
        return (T)dr.GetValue(index);
    }
}

Being called as Helpers.IsNull(dr, "UnitWholeSale", 0d) and the error I am getting is "Cannot convert from double to decimal".

Now I know I can use decimal.Zero but is there some way that I can simply go 0dec or something similar? I just hate those long shortcut values (especially when you are calling a constructor with 20 parameters).

7条回答
做个烂人
2楼-- · 2019-03-22 16:04

Instead of using 0d, you can use 0m for decimal values.

查看更多
登录 后发表回答