C#,可为空的十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12480029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#, Nullable decimal
提问by Expert wanna be
In my database, v1 field is nullable field. but I define 0 (default) if the value is null.
在我的数据库中, v1 字段是可空字段。但如果值为空,我定义 0(默认值)。
public decimal? v1 {
get {
return this._v1;
}
set {
this._v1 = value ?? 0M;
}
}
so now, the v1 is not nullable variable anymore.
所以现在, v1 不再是可以为空的变量。
but I can not do this,
但我不能这样做,
decimal v2 = v1;
The error message say, Cannot implicitly convert type 'decimal?' to 'decimal'.
错误消息说,无法隐式转换类型“十进制?” 到'十进制'。
In this case, Do I have to convert to decimal, like this?
在这种情况下,我是否必须像这样转换为十进制?
decimal v2 = Convert.ToDecimal(v1);
It very annoying job. and codes are look dirty too.
这很烦人的工作。而且代码看起来也很脏。
Anyone know better solution? please advice me.
有人知道更好的解决方案吗?请给我建议。
采纳答案by James
No you don't have to convert the decimal?, you have access to the underlying value from the Nullabletype e.g.
不,您不必转换decimal?,您可以访问Nullable类型中的基础值,例如
decimal v2 = v1.Value;
Assigning a default value to a Nullabletype does not make it non-nullable, it just means it has a value. Nullable types have a HasValueproperty which helps you determine this.
为Nullable类型分配默认值不会使其不可为空,它只是意味着它有一个值。可空类型有一个HasValue属性可以帮助您确定这一点。
Just for the record, I wouldn't recommend defaulting the value to 0it would probably make more sense letting it default to nullconsidering it canindeed be null. If you need to have a default value in your app you will probably want to use the GetValueOrDefaultmethod e.g.
只是为了记录在案,我不会推荐默认值,以0它可能会更有意义,让它默认为null考虑它可能确实是空的。如果您需要在您的应用程序中有一个默认值,您可能需要使用该GetValueOrDefault方法,例如
decimal v2 = v1.GetValueOrDefault(0m);
回答by gsharp
The Value you assigned to v1 is decimal, but the type is still decimal?. That's why you can't assing it.
您分配给 v1 的值是decimal,但类型仍然是decimal?。这就是为什么你不能评估它。
Try
尝试
decimal v2 = v1.Value;
回答by dknaack
Nullable Types have the properties Valueand HasValue.
可空类型具有属性Value和HasValue。
HasValue- Gets a value indicating whether the current Nullable object has a value.
Value- If HasValue is true, this contains the value if not its null.
HasValue- 获取一个值,该值指示当前 Nullable 对象是否具有值。
值- 如果 HasValue 为真,则包含该值(如果不是空值)。
decimal v2;
if (v1.HasValue) // check for null
{
v2 = v1.Value;
}
回答by Matthew
The easiest way to convert a decimal?to decimalbut have the value to be 0when null, you can do:
将 a 转换decimal?为decimal但值为0when的最简单方法null,您可以执行以下操作:
decimal? a = null;
decimal b = a.GetValueOrDefault(0m); // will contain 0 when null, otherwise the value
Read up on GetValueOrDefault
You could also call GetValueOrDefaultwith no arguments, which will yield default(T), which in the case of Decimal is 0, but I like to be explicit.
您也可以GetValueOrDefault不带参数调用,这将产生 yield default(T),在 Decimal 的情况下为 0,但我喜欢明确表示。
回答by Sinaesthetic
I'm curious why you would define your app code variable as nullable but then set it to 0 if it's null. Is it just to avoid the exception if passing a null value from the database to your app variable? If your application doesn't absolutely need the nullable decimal variable, just check it for a DbNull as you're reading it out:
我很好奇为什么您将应用程序代码变量定义为可为空,但如果为空则将其设置为 0。如果将空值从数据库传递给您的应用程序变量,是否只是为了避免异常?如果您的应用程序并不绝对需要可空的十进制变量,只需在读取它时检查它是否为 DbNull:
decimal myAppVal = rdr["DbColumn"] == DBNull.Value
? default(decimal)
: (decimal) rdr["DbColumn"];
or explicitly set 0.0M instead of using default(decimal)
或明确设置 0.0M 而不是使用默认值(十进制)
Design-wise, if you're finding yourself doing this often, then i'd suggest creating a generic extension method to read values out of the database. This way you can ensure that invalid casts are avoided and exceptions are handled, but more importantly that default values are returned in case of nulls. ;)
在设计方面,如果您发现自己经常这样做,那么我建议创建一个通用扩展方法来从数据库中读取值。通过这种方式,您可以确保避免无效转换并处理异常,但更重要的是,如果出现空值,则返回默认值。;)
Something like (and I'm just spitballing here):
类似的东西(我只是在这里吐痰):
public static T CastFromDbTo<T>(object readerObject)
{
T returnVal = default(T);
if (readerObject is T)
{
var myValue = (T) readerObject;
returnVal = readerObject != DbNull.Value && myValue != null
? (T) readerObject
: default(T);
}
return returnVal;
}
Then you could grab your value like this:
然后你可以像这样获取你的价值:
var myAppValue = HelperClass.CastFromDbTo<decimal>(rdr["DbColumn"]);
or actually make an extension:
或实际进行扩展:
public static T CastFromDbTo<T>(this object readerObject)
{
T returnVal = default(T);
if (readerObject is T)
{
var myValue = (T) readerObject;
returnVal = readerObject != DbNull.Value && myValue != null
? (T) readerObject
: default(T);
}
return returnVal;
}
then you could just do this:
那么你可以这样做:
var myAppVal = rdr["DbColumn"].CastFromDbTo<decimal>();

