protected override Boolean IsValid(String propertyValue)
{
return !String.IsNullOrEmpty(propertyValue) && propertyValue.Trim().Length > 0;
}
This C# validation method does exactly what I want, but I wasn’t aware that you could use expression short-circuiting like this.
When propertyValue is null, doesn’t execution still need to evaluate the second part of the expression to yield a boolean result, and if so why doesn’t the second part then throw a null ref exception for the Trim().Length > 0 call?
I assume that the second part is evaluating to false or to null, but I am curious to know what is going on here.
Apologies if I’m missing something really obvious, please enlighten me.
God – I was missing something obvious what an idiot! – total blank on the fact that when the first part is false the second part is irrelevant and I even KNEW it was short-ciruciting – sorry for wasting people’s time, what’s the emoticon for embarassment?
Added another tag to reflect my embarrassment at typing before thinking.
This is what short-circuiting does. When you have
and e1 evaluates to false, e2 is not evaluated at all.
(And for
if e1 is true, e2 is not evaluated at all.)
This is important to understand in the case where e2 has “effects” (does I/O, throws an exception, …) as short-circuiting yields different program semantics than full evaluation would.