[英]null-coalescing operator or conditional operator
Which coding style do you prefer: 您更喜欢哪种编码风格:
object o = new object();
//string s1 = o ?? "Tom"; // Cannot implicitly convert type 'object' to 'string' CS0266
string s3 = Convert.ToString(o ?? "Tom");
string s2 = (o != null) ? o.ToString() : "Tom";
s2 or s3? s2或s3?
Is it possible to make it shorter? 是否可以缩短时间? s1 does not obviously work.
s1显然不起作用。
In this case, I think my preference would be: 在这种情况下,我认为我的偏好是:
string s1 = (string)o ?? "Tom";
Or: 要么:
string s1 = (o as string) ?? "Tom";
Depending on whether or not o
is really expected to be a string
or not. 根据是否
o
真的有望成为一个string
或没有。 Either way, I prefer these because they better express what's being done, and don't pass this through an unnecessary conversion if o
is already a string
. 无论哪种方式,我都更喜欢这些,因为它们可以更好地表达正在执行的操作,并且如果
o
已经是string
,则不要通过不必要的转换来传递它们。
As a general rule, I prefer whichever is clearer and/or actually works. 一般而言,我更喜欢更清晰和/或更有效的方法。 When working with strings, I often need to write something like this instead:
使用字符串时,我经常需要编写如下代码:
string result = !string.IsNullOrEmpty(value) ? value : "Default";
...which can't really be done at all with the null-coalescing operator. ...使用null-coalescing运算符根本无法完成。 On the other hand, if I'm trying to coalesce a large number of values, it's about 500 times better:
另一方面,如果我尝试合并大量的值,则大约好500倍:
var result = firstTry ?? secondTry ?? thirdTry ?? fourthTry ?? fifthTry;
Try writing that with the ternary operator instead. 尝试改用三元运算符编写。
If the difference is not this dramatic, if it's just going to be a couple of characters on one line of code, it really doesn't matter, just use whatever you're comfortable with. 如果差异不是那么大,如果只是在一行代码中包含几个字符,那就没关系,只要使用您喜欢的任何东西即可。
这也会起作用,尽管在字符串上调用ToString
有点奇怪:
string s4 = (o ?? "Tom").ToString();
For this case I'd prefer using the ternary operator since it expresses the intent more clearly and avoids a redundant call to Convert.ToString()
. 对于这种情况,我更喜欢使用三元运算符,因为它可以更清楚地表达意图并避免对
Convert.ToString()
的多余调用。 In general I would prefer the null coalescing operator if the conversion is to an object of the same type eg 通常,如果转换是针对相同类型的对象,则我倾向于使用空合并运算符,例如
string s1 = null;
string s2 = s1 ?? string.Empty;
IMO, I tend to use: IMO,我倾向于使用:
var s = obj ?? "tom";
when s
is the same type as obj
without using the Convert
or any other type of casting. 当
s
与obj
类型相同时,不使用Convert
或任何其他类型的转换。
Then, I use: 然后,我使用:
var s = obj != null
? obj.ToString()
: "tom";
when I need to cast or have some other kind of transformation on the right side before assigning to the left side... 当我需要在分配左侧之前进行强制转换或在右侧进行其他类型的转换时...
Just my style of coding, I suppose. 我想这只是我的编码风格。
I don't like either. 我也不喜欢 Converting object to a specific type is fraught with trouble, it should never be hidden in an expression.
将对象转换为特定类型会带来很多麻烦,永远不要将其隐藏在表达式中。 I'd much prefer that, if it bombs, then it does so on a specific statement.
我非常希望这样做,如果它炸弹了,那么它会在特定声明中这样做。 And to make it absolutely obvious to a reader of the code that this conversion is being done.
为了使代码阅读者绝对清楚,此转换已完成。 So, at the very minimum:
因此,至少:
string s1 = o as string;
string s2 = s1 ?? "Tom";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.