I discovered a very puzzling behavior for the following code:
public double ReturnBehavior(List<double> ptList)
{
return ptList.Count==0? 0:ptList[0];
}
I thought it should be equivalent to
public double ReturnBehavior(List<double> ptList)
{
if(ptList.Count==0)
return 0;
return ptList[0];
}
But it is not, because the first method will evaluate both true and false condition together. So this means that first method will try an IndexOutOfRange exception if ptList.Count==0.
Am I missing something here? Or is it a bug in vs 2008?
I’ve checked both in VS2010 and VS2008, behavior is expected – no exceptions. If you have errors – they are not in the given code fragment