CPath = (CPath == null) ? Request.Path : CPath;
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.
CPath = (CPath == null) ? Request.Path : CPath;
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.