does, myval = (someconditon) ? someVal : myval get optimized to not set the value in case it's false
CPath = (CPath == null) ? Request.Path : CPath;
开发者_JAVA百科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.
Well, I would personally write that as:
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?
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.
精彩评论