System.Nullablenull * int 值的值是多少?

发布于 2024-08-03 06:58:06 字数 174 浏览 7 评论 0原文

考虑以下陈述:

int? v1 = null;

int? v2 = 5 * v1;

v2 的值是多少? (null 或空字符串?)

如何防止编译器将其标记为无效操作?我需要遵循自定义异常处理吗?

Consider the following statements:

int? v1 = null;

int? v2 = 5 * v1;

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺ぐ爱和你が 2024-08-10 06:58:06

它是null

C# 语言规范 3.0(第 §7.2.7 节:提升运算符)

对于二元运算符 + - * / % & | ^ << >> :

如果操作数和结果类型都是不可空值类型,则存在运算符的提升形式。提升形式是通过向每个操作数和结果类型添加单个 ? 修饰符来构造的。 如果一个或两个操作数为 null,则提升运算符会生成 null 值(&bool? 类型的 | 运算符,如第 7.10.3 节中所述)。否则,提升的运算符将解包操作数,应用底层运算符,并包装结果。


如何防止编译器将其标记为无效操作?我需要遵循自定义异常处理吗?

这不是无效操作。它不会抛出异常,因此在这种情况下您不需要处理异常。

It's null.

C# Language Specification 3.0 (Section §7.2.7: Lifted operators)

For the binary operators + - * / % & | ^ << >> :

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.

淡墨 2024-08-10 06:58:06

如果您希望编译器阻止该操作,请将第二个变量设置为不可为 null。那么你将被迫这样写:

int v2 = 5 * v1.Value;

如果 v1 为 null,这将在运行时抛出异常。

If you want the operation to be prevented by the compiler, make the second variable non-nullable. Then you will be forced to write:

int v2 = 5 * v1.Value;

This will throw an exception at run time if v1 is null.

千仐 2024-08-10 06:58:06

它的值将是“null”,在这种情况下至少可以被认为与数据库null相同,即而不是Nothing或Zero,它意味着“Unknown”。五批未知仍然是未知,它也永远不会是“空字符串”,因为您处理的是数字,而不是字符串。

我不确定“如何防止编译器将其标记为无效操作?”是什么意思?因为这段代码在 Visual Studio 2008 下编译并运行良好 =)

Its value will be "null", in this context at least can be considered to be the same as a database null, i.e. rather than Nothing, or Zero, it means "Unknown". Five lots of Unknown is still Unknown, it'll also never be "empty string" as you're dealing with numbers, not strings.

I'm not sure what you mean by "How can I prevent the compiler to mark it as invalid operation?" as this code compiles and runs fine for me under Visual Studio 2008 =)

耀眼的星火 2024-08-10 06:58:06

我不确定你想做什么,但如果你希望 v2 为空(如果 v1 为空),那么你应该在使用 v1 之前测试它是否有值。

int? v2 = v1.HasValue ? 5 * v1.Value : null;

或者

int? v2 = null;
if (v1.HasValue) { v2 = 5 * v1.Value; }

I'm not sure what you are trying to do, but if you want v2 to be null if v1 is null then you should test if v1 has a value prior to using it.

int? v2 = v1.HasValue ? 5 * v1.Value : null;

or

int? v2 = null;
if (v1.HasValue) { v2 = 5 * v1.Value; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文