是,myval =(someconditon)? someVal:myval得到优化,以不设置值,如果它是假的 [英] does, myval = (someconditon) ? someVal : myval get optimized to not set the value in case it's false

查看:243
本文介绍了是,myval =(someconditon)? someVal:myval得到优化,以不设置值,如果它是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CPath = (CPath == null) ? Request.Path : CPath;

首先我希望CLR只是让我做? Request.Path,而不是打扰我创建一个:

First of all I wish CLR would just let me do the ? Request.Path and not bother me with creating a :

但是我问它会优化它吗?或者仍然分配。

But I'm asking will it optimize it away? Or still assign.

推荐答案

好吧,我亲自写成:

if (CPath == null)
{
    CPath = Request.Path;
}

另一种(如别处所述)是

to make it clearer. An alternative (as mentioned elsewhere) is

CPath = CPath ?? Request.Path;

但你为什么要关心是否有额外的作业?

But why do you care if there's an extra assignment? Do you really think that's going to be a significant performance hit?

请注意,如果 CPath c>是一个字段,而不是一个局部变量,它可能有区别 - 因为 CPath 的值可能在第一次检查和第二次评估之间变化,评估和分配。是否会被注意到取决于缓存等,但它不是那么简单,因为它可能看起来第一。

Note that if CPath is a field rather than a local variable, it could potentially make a difference - because the value of CPath could change between the first check and the second evaluation, and again between the evaluation and the assignment. Whether that will be noticed depends on caching etc, but it's not as simple as it might look at first.

这篇关于是,myval =(someconditon)? someVal:myval得到优化,以不设置值,如果它是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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