I have this code, where LongMethodWithResult is a method that takes a long time to run:
object o = LongMethodWithResult() == someVal ? LongMethodWithResult() : someOtherResult;
Now the LongMethodWithResult method is evaluated two times, isn't it?
I know I could write a method that uses variables to store the result of the long method, something like this:
public static object ConciseConditionalOperator(object a, object b, object c)
{
return a == b ? a : c;
}
But I would be interested in whether there is a best way of doing this, or some functionality served by C# or the .NET.
Any ideas are welcomed!
object o = LongMethodWithResult() == someVal ? someVal : someOtherResult;?LongMethodWithResult() <= someVal ? ...LongMethodWithResult() <= someVal