I was asking myself if there is a possibility to use return in a method that only gets executed if a condition is true but without using an if statement. If the condition is false, nothing would be returned.
For better understanding:
public bool MyMethod()
{
if (HasErrors())
return HasErrors();
// Some more code
}
Some more code would then also return something. I now thought of something like this:
public bool MyMethod()
{
return HasErrorsButReturnsOnlyIfTrue();
// Some more code
}
But return HasErrorsButReturnsOnlyIfTrue(); only has to be executed if HasErrors() returns true. Otherwise it would be skipped.
Is there any possibility to achieve something like that without using if?
if? No. Without calling the method twice? Yes:if (hasErrors()) return true;return hasErrors(). Why would you doif (true) return true?