简体   繁体   English

? 操作员VS ?? 运营商用法

[英]? Operator VS ?? Operator Usage

The following statement works: 以下声明有效:

Class.ID = odrDataReader["ID"] == null ? 0 : Convert.ToInt32(odrDataReader["ID"]);

But the following does not: 但以下不是:

Class.ID = odrDataReader["ID"] as int? ?? 0; //ID is always 0

Any one can explain why ?? 谁能解释为什么? operator always returns 0 even when ID column is not null? 即使ID列不为空,运算符也始终返回0?

Solution(suggested by Kirk): 解决方案(Kirk建议):

Class.ID = Convert.ToInt32(odrDataReader["ID"] ?? 0);

In first one you use Convert.ToInt32(odrDataReader["ID"]) in second one you use odrDataReader["ID"] as int? 在第一个中你使用Convert.ToInt32(odrDataReader["ID"])在第二个你使用odrDataReader["ID"] as int? . From what you say first one is correct, so you should use Convert in second too. 从你说的第一个是正确的,所以你也应该在第二个使用转换。

Actualy I think first is fine, because it would look weird if you really wanted to use ?? Actualy我认为首先是好的,因为如果你真的想使用它会看起来很奇怪? operator. 运营商。

Edit : To explain a bit odrDataReader["ID"] as int? 编辑 :将odrDataReader["ID"] as int? is NOT a conversion. 不是转换。 It will always return null if odrDataReader["ID"] is string. 如果odrDataReader [“ID”]是字符串,它将始终返回null。

It all depends on the type of the ID column. 这一切都取决于ID列的类型。 If it was indeed of type int? 如果确实是int?类型int? , then you should be able to do this with no problems: ,那么你应该能够毫无问题地做到这一点:

Class.ID = (int?)odrDataReader["ID"] ?? 0;

However it is more likely that this is not the case and is a different type (I'd guess string ). 然而,更有可能的情况并非如此,并且是一种不同的类型(我猜猜string )。 A string is clearly not a int? string显然不是int? and thus odrDataReader["ID"] as int? 因此odrDataReader["ID"] as int? always returns null and you get the value 0 . 始终返回null并获得值0 As Euphoric mentions, the as operator only does reference/boxing conversions. 正如Euphoric所提到的asas运算符只能进行引用/装箱转换。 If this is the case, using ?? 如果是这种情况,使用?? is not an option for you and you must use the first expression. 不适合您,您必须使用第一个表达式。

It's definitely because you are doing odrDataReader["ID"] as int? 这肯定是因为你在做odrDataReader["ID"] as int? - that is the difference between the two statements, and using the as keyword is not the same as doing a Convert . - 这是两个语句之间的区别,使用as关键字与执行Convert

If you want to use ?? 如果你想用?? you could try 你可以试试

Class.ID = Convert.ToInt32(odrDataReader["ID"] ?? 0);

Misread the question. 误读了这个问题。 I'm guessing it's because you're casting odrDataReader["ID"] to int? 我猜是因为你将odrDataReader [“ID”]转换为int? before checking it. 在检查之前。 You should try skipping the cast, though I doubt that's what's really the problem. 你应该尝试跳过演员,但我怀疑这是什么问题。

Coalescing operator is new operator added in C#2.0. 合并运算符是C#2.0中添加的新运算符。 Coalescing operator is also known as ??. 合并算子也称为??。

Nullable<int> a = null;
Nullable<int> b = 10;
int c = a ?? b.Value;

Coalescing operator Coalescing operator work some what similar to ternary operator but it works only with the Nullable types only. 合并运算符Coalescing运算符与三元运算符类似,但它仅适用于Nullable类型。 so its sort hand operator to deal with Nullable types only. 所以它的sort hand操作符只能处理Nullable类型。

check my blog post : Coalescing operator - ?? 检查我的博客文章: 合并运算符 - ??

the ?? ?? ?? operator is the null-coalescing operator . operator是null-coalescing运算符 It defines a default value if the value is found to be null. 如果发现该值为null,则它定义默认值。 Int32 is a value type in .NET, which normally can't take a null value, so the int? Int32是.NET中的一个值类型,它通常不能取空值,所以int? designates an Int32 that can be null. 指定可以为null的Int32。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM