使用条件运算符时的评估顺序[关闭](Order of evaluation when using conditional operator [closed])

编程入门 行业动态 更新时间:2024-10-24 16:27:20
使用条件运算符时的评估顺序[关闭](Order of evaluation when using conditional operator [closed])

是否有以下行为?

public class TestClass { public int IntValue { get; set; } } TestClass instance = null; // this line throws exception: Object reference not set to an instance of an object string test1 = "abc" + instance != null ? instance.IntValue.ToString() : "0"; // this works string test2 = instance != null ? instance.IntValue.ToString() : "0"; // this works string test3 = "abc" + (instance != null ? instance.IntValue.ToString() : "0");

更新

为什么这不会抛出异常?

TestClass instance = null; string test4 = "abc" + instance; string test5 = "abc" + true;

Is the following behavior expected?

public class TestClass { public int IntValue { get; set; } } TestClass instance = null; // this line throws exception: Object reference not set to an instance of an object string test1 = "abc" + instance != null ? instance.IntValue.ToString() : "0"; // this works string test2 = instance != null ? instance.IntValue.ToString() : "0"; // this works string test3 = "abc" + (instance != null ? instance.IntValue.ToString() : "0");

UPDATE:

How come this doesn't throw an exception?

TestClass instance = null; string test4 = "abc" + instance; string test5 = "abc" + true;

最满意答案

您的问题是运营商优先级。 +排名高于!= ?: 。 你的行无效也可以这样写:

string test1 = ("abc" + instance) != null ? instance.IntValue.ToString() : "0";

表达式"abc" + instance 永远不会为 null ,即使instance为null 。 这意味着您的表达式始终计算为true ,这使您在instance.IntValue.ToString()遇到NullReferenceException 。案例instance的NullReferenceException instance.IntValue.ToString()表达式为null 。

您可以使用parenthisation覆盖运算符优先级。 这意味着你必须确保instance != null ? instance.IntValue.ToString() instance != null ? instance.IntValue.ToString()首先通过括在括号中来计算:

string test1 = "abc" + (instance != null ? instance.IntValue.ToString() : "0");

更新:

就更新中的问题而言:这些表达式不会引发错误,因为关于字符串上的+运算符实现有两个细节:

只要在另一端有一个有效的字符串,它就可以容忍null值。 由于ToString方法在System.Object实现,因此任何值都可以转换为带ToString的字符串。 因此,可以将非字符串值添加到字符串(仅在右侧 )。

Your problem is operator precedence. + ranks higher than != and ?:. Your line that is not working could be also written like this:

string test1 = ("abc" + instance) != null ? instance.IntValue.ToString() : "0";

The expression "abc" + instance is never null, even if instance is null. That means your expression always evaluates to true, which makes you run into a NullReferenceException in the instance.IntValue.ToString() expression in the case instance is null.

You can override operator precedence with parenthisation. That means you would have to make sure instance != null ? instance.IntValue.ToString() is evaluated first, by enclosing it in parentheses:

string test1 = "abc" + (instance != null ? instance.IntValue.ToString() : "0");

UPDATE:

As far as your questions in the update is concerned: These expressions don't throw an error, because there are two specifics about the + operator implementation on a string:

It's tolerant to null values as long as you have a valid string on the other side. Since the ToString method is implemented in System.Object, any value can be turned into a string with ToString. Therefore it is possible to add non-string values to a string (only on the right side).

更多推荐

本文发布于:2023-07-31 20:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   运算符   条件   Order   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!