为什么我们喜欢哪一种?至 ??运营商在C#? [英] why do we prefer ? to ?? operator in c#?

查看:87
本文介绍了为什么我们喜欢哪一种?至 ??运营商在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,我们可以使用?运算符来检查空值。请检查下面的示例代码:

I recently found that we can use ?? operator to check nulls. Please check the below code samples:

   var res = data ?? new data();

这正是类似于

   var res = (data==null) ? new data() : data ;



我检查了我的整个项目源代码库和其他一些开源项目。而这个 ?? 运营商从未使用过。

我只是想知道是否有任何理由背后,像性能问题?什么

I just wondering is there any reason behind this, like performance problems or something?

编辑:

我刚刚更新了我的示例代码的基础上从递归和放大器的意见;安东。它在不小心的错误。 (

I just updated my sample code based on the comments from recursive & Anton. Its a mistake in careless. :(

推荐答案

空COALESCE操作符是空检查时更清楚,这是它的主要目的,它也可以链接。

The null coalesce operator is much clearer when checking for null, that is its main purpose. It can also be chained.

object a = null;
object b = null;
object c = new object();
object d = a ?? b ?? c; //d == c.

虽然该运营商被限制为null检查,三元算不算例如

While that operator is limited to null checking, the ternary operator is not. For example

bool isQuestion = true;
string question = isQuestion ? "Yes" : "No";

我认为人们只是AREN •解空COALESCE运营商,因此他们使用三元运算符。三元,所以如果你不知道C#的内部和外部和/或你用另一种语言编写之前C#中​​的大多数C风格语言的存在,三元是一个自然的选择。如果你虽然检查空,使用空COALESCE运营商,它是专为,而IL稍有优化(如果当时别人比较??到)。

I think people just aren't aware of the null coalesce operator so they use the ternary operator instead. Ternary existed before C# in most C style languages so if you don't know C# inside and out and/or you programmed in another language, ternary is a natural choice. If you are checking for null though, use the null coalesce operator, it is designed for that, and the IL is slightly optimized (compare ?? to an if then else).

下面是比较使用的每个

object a = null;
object b = null;
object c = null;

object nullCoalesce = a ?? b ?? c;

object ternary = a != null ? a : b != null ? b : c;

object ifThenElse;

if (a != null)
	ifThenElse = a;
else if (b != null)
	ifThenElse = b;
else if (c != null)
	ifThenElse = c;



首先,光看语法空聚结,这是方式更清晰。三元确实是令人困惑。现在,让我们看看IL

First, just look at the syntax for null coalesce, it is way clearer. Ternary is really confusing. Now lets look at the IL

空凝聚只有

.entrypoint
.maxstack 2
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object nullCoalesce)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: dup 
L_000c: brtrue.s L_0015
L_000e: pop 
L_000f: ldloc.1 
L_0010: dup 
L_0011: brtrue.s L_0015
L_0013: pop 
L_0014: ldloc.2 
L_0015: stloc.3 
L_0016: ldloc.3 
L_0017: call void [mscorlib]System.Console::WriteLine(object)
L_001c: ret

三元只有

.entrypoint
.maxstack 2
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object ternary)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: brtrue.s L_0016
L_000d: ldloc.1 
L_000e: brtrue.s L_0013
L_0010: ldloc.2 
L_0011: br.s L_0017
L_0013: ldloc.1 
L_0014: br.s L_0017
L_0016: ldloc.0 
L_0017: stloc.3 
L_0018: ldloc.3 
L_0019: call void [mscorlib]System.Console::WriteLine(object)
L_001e: ret

如果然后否则只有

.entrypoint
.maxstack 1
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object ifThenElse)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: brfalse.s L_0011
L_000d: ldloc.0 
L_000e: stloc.3 
L_000f: br.s L_001a
L_0011: ldloc.1 
L_0012: brfalse.s L_0018
L_0014: ldloc.1 
L_0015: stloc.3 
L_0016: br.s L_001a
L_0018: ldloc.2 
L_0019: stloc.3 
L_001a: ldloc.3 
L_001b: call void [mscorlib]System.Console::WriteLine(object)
L_0020: ret

IL不是一天我的强项,所以也许有人可以修改我的答案和扩大它。我会解释我的理论,但我宁愿不要混淆自己和他人。 LOC的数目是所有三个类似,但不是所有的IL运算需要大量的时间来执行相同的长度。

IL isn't one of my strong points, so maybe someone can edit my answer and expand on it. I was going to explain my theory, but I'd rather not confuse myself and others. The number of LOC is similar for all three, but not all IL operators take the same length of time to execute.

这篇关于为什么我们喜欢哪一种?至 ??运营商在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆