Alex Rivera | Logout

Get out of a void method?

Asked 2012-01-18T15:23:41.723
46

I have this method (modified code) :

public static void PublishXmlForCustomTypes(MyOwnClass DefaultOutputInformation)
{
    if (DefaultOutputInformation != null)
    {
        ///lot of code
    }
}

and my whole code was inside the if statement and after thinking about it, I changed to this :

public static void PublishXmlForCustomTypes(MyOwnClass DefaultOutputInformation)
{
    if (DefaultOutputInformation == null)
    {
        return;
    }
    ///lot of code
}

As far as I tested it, it seems to be strictly equivalent but is that really the case ? I mean, the "return" statement get us out of the method right ?

Edit
Report

2 Answers

1

Yes, your assumptions is correct.

For some background, learn about duality.

answered 2012-01-18T15:24:48.113
1

Indeed the return gets you out of the method, so it is equivalent to the first way you used. Which way is better depends on your code, although generally I would prefer the second version.

answered 2012-01-18T15:25:50.457

Your Answer