Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ?? 和| |操作员计时_C#_Timing_Null Coalescing Operator - Fatal编程技术网

C# ?? 和| |操作员计时

C# ?? 和| |操作员计时,c#,timing,null-coalescing-operator,C#,Timing,Null Coalescing Operator,我开始学习C语言,刚刚开始使用?操作符,但对时间有点困惑。以下面的代码示例为例: string x = null; string y = null; string v = null; var watch = System.Diagnostics.Stopwatch.StartNew(); if (string.IsNullOrEmpty(x ?? y ?? v ?? y ?? v)) { } watch.Stop();

我开始学习C语言,刚刚开始使用
操作符,但对时间有点困惑。以下面的代码示例为例:

    string x = null;
    string y = null;
    string v = null;

    var watch = System.Diagnostics.Stopwatch.StartNew();

    if (string.IsNullOrEmpty(x ?? y ?? v ?? y ?? v))
    {

    }

    watch.Stop();
    var elapsedMs = watch.ElapsedTicks;
    Console.WriteLine(elapsedMs.ToString());

    watch.Restart();

    if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y) || string.IsNullOrEmpty(v) || string.IsNullOrEmpty(y) || string.IsNullOrEmpty(v))
    {

    }

    var elapsedMs2 = watch.ElapsedTicks;
    Console.WriteLine(elapsedMs2.ToString());
我得到的结果是,第一个if语句需要大约100个刻度,而第二个通常需要大约3个刻度。我的印象是,这两个语句在时间上是相似的,或者带有
|
的第二个if语句将花费更长的时间


很明显,我在这里遗漏了一些东西。我认为在第一个
x
被求值为null之后,将是它,其余的将不会被求值。有人能解释我这里的错误吗?

你可能有
的定义吗<代码>a??b表示:

  • 如果
    a
    为空,
    b
  • 否则,
    a
因此,如果左侧是notnull,那么我们不计算
b
,因为我们知道
a??b
a
。如果
a
像您的情况一样为空,我们必须计算
b

if(string.IsNullOrEmpty(x??y??v??y??v))

运行方式为:

  • 评估
    x
  • 其计算结果为null,因此计算
    y
  • 它的计算结果为null,因此计算
    v
  • 其计算结果为null,因此计算
    y
  • 它的计算结果为null,因此计算
    v
  • 其计算结果为null。将此值(null)传递到
    字符串中。IsNullOrEmpty
  • 其计算结果为
    true
    。进入区块
if(string.IsNullOrEmpty(x)| string.IsNullOrEmpty(y)| string.IsNullOrEmpty(v)| string.IsNullOrEmpty(y)| string.IsNullOrEmpty(v))

评估为:

  • 评估
    x
  • 其计算结果为null。将此值(null)传递到
    字符串中。IsNullOrEmpty
  • 其计算结果为
    true
    。由于如果只有一条边为真,则满足
    |
    ,因此我们不计算第一条
    |
    的右侧或更右侧。进入区块

您可能有
的定义吗?
向后<代码>a??b表示:

  • 如果
    a
    为空,
    b
  • 否则,
    a
因此,如果左侧是notnull,那么我们不计算
b
,因为我们知道
a??b
a
。如果
a
像您的情况一样为空,我们必须计算
b

if(string.IsNullOrEmpty(x??y??v??y??v))

运行方式为:

  • 评估
    x
  • 其计算结果为null,因此计算
    y
  • 它的计算结果为null,因此计算
    v
  • 其计算结果为null,因此计算
    y
  • 它的计算结果为null,因此计算
    v
  • 其计算结果为null。将此值(null)传递到
    字符串中。IsNullOrEmpty
  • 其计算结果为
    true
    。进入区块
if(string.IsNullOrEmpty(x)| string.IsNullOrEmpty(y)| string.IsNullOrEmpty(v)| string.IsNullOrEmpty(y)| string.IsNullOrEmpty(v))

评估为:

  • 评估
    x
  • 其计算结果为null。将此值(null)传递到
    字符串中。IsNullOrEmpty
  • 其计算结果为
    true
    。由于如果只有一条边为真,则满足
    |
    ,因此我们不计算第一条
    |
    的右侧或更右侧。进入区块

啊,好的,谢谢!在开始测试计时之前,我应该更加关注操作员的实际操作。啊,好的,谢谢!在开始测试计时之前,我应该更加关注操作员的实际操作。