1

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?

7
  • 4
    Without if? No. Without calling the method twice? Yes: if (hasErrors()) return true; Commented Apr 26, 2016 at 7:56
  • @Dennis_E return hasErrors(). Why would you do if (true) return true ? Commented Apr 26, 2016 at 7:57
  • @GuillaumeBeauvois He doesn't want to return if it's false. Commented Apr 26, 2016 at 7:58
  • 4
    Throw might be an option. Commented Apr 26, 2016 at 7:59
  • In such case, when you dont want the method to return anything, what do you expect to be found in the returning object?? Commented Apr 26, 2016 at 8:00

3 Answers 3

3

This might be the closest you'll get:

public bool MyMethod()
{
    return HasErrors()
        ? true
        : SomeMoreCode();
}

Note that this means you'll have to put your 'Some More Code' in a separate method, and that method must now also return a boolean.

Sign up to request clarification or add additional context in comments.

2 Comments

Or even shorter: return HasErrors() || SomeMoreCode();
This doesn't solve: only returns some value if a condition is true, but that might not be what OP really wanted.
1

Disclaimer: this is just a joke.

public bool MyMethod()
{
    try
    {
        return HasErrorsButReturnsOnlyIfTrue();
    }
    catch
    {
        // Some more code
        Console.WriteLine("Test");
        return false;
    }
}

public bool HasErrorsButReturnsOnlyIfTrue()
{
    if (some condition)
        return true;
    else
        throw new Exception();
}

Comments

0

Make a nullable function.

void bool? isBar()
{
    SomeObj someObj = check(); // Returns someObj;
    if (someObj == null)
        return false; // Nothing there
    else if (someObj.something == 1)
        return null; // It's there! Don't return!
    else
        return true; // Something's there, but not what we want
}

void doSth()
{
    bool? isValid = isBar();
    if (isValid != null)
        return (bool)isValid;
}

Alternately, the same could be done by making a struct or class that indicates whether to return or not.

struct Validator
{
    public bool ShouldReturn;
    public bool ReturnBool; // Could skip this and just make ShouldReturn nullable like the above example
    public string ErrMsg;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.