System.Nullable< T>什么是空* int值的价值? [英] System.Nullable<T> What is the value of null * int value?
问题描述
考虑下面的语句:
int? v1 = null;
int? v2 = 5 * v1;
什么是 V2
的价值? (空
或空字符串?)
What is the value of v2
? (null
or empty string?)
如何防止编译器将其标记为无效操作?我是否需要遵循自定义异常处理?
How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling?
推荐答案
这是空
。
对于二元运算符 +
-
*
/
%
&安培;
|
^
<<
>>
:
C# Language Specification 3.0 (Section §7.2.7: Lifted operators)
For the binary operators
+
-
*
/
%
&
|
^
<<
>>
:
运算符的提升形式存在,如果操作数和结果类型都为非可空值类型。该提升形式是通过将一个?
修改到每个操作数和结果类型构造的。 提升运算产生了空
价值,如果一个或两个操作数是空
(异常作为&放大器;
和 |
的布尔运营商
型,如在§7.10.3描述)。否则,提升运算解开操作数,应用基础运算符,并包装结果。
a lifted form of an operator exists if the operand and result types are all non-nullable value types. The lifted form is constructed by adding a single ?
modifier to each operand and result type. The lifted operator produces a null
value if one or both operands are null
(an exception being the &
and |
operators of the bool?
type, as described in §7.10.3). Otherwise, the lifted operator unwraps the operands, applies the underlying operator, and wraps the result.
如何防止编译器将其标记为无效操作?我是否需要遵循自定义异常处理?
How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling?
这不是一个无效的操作。它不会抛出异常,所以你不必处理这种情况下的例外。
It's not an invalid operation. It won't throw an exception so you don't need to handle exceptions in this case.
这篇关于System.Nullable< T>什么是空* int值的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!