是否有以下行为?
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).更多推荐
发布评论