user1968030
user1968030

Reputation:

Why do we not need to return a value outside of the `using` scope?

Consider this code:

public int DownloadSoundFile()
{
   using (var x= new X())
   {
       return x.Value;
   }
}

and this code:

public int DownloadSoundFile()
{
    if (x!=null)
    {
       return x.Value;
    }
}

The first code doesn't give us any compile time errors but in the second code we get this error:

not all code paths return a value

This means we should return a value outside of the if scope.

Why do we have to return a value outside of the if scope but don't need to return a value outside of the using scope?

Upvotes: 3

Views: 334

Answers (4)

Toto
Toto

Reputation: 7719

using is an alias for following code :

IDisposable x = null;
try
{
     x= new X();
     //inside the using
     return x.Value;
}
finally
{
     if(x != null)
        x.Dispose();
}

So you can notice that every path return a value; in fact, there is only ONE path (or maybe an expeption).

Upvotes: 0

Narendra
Narendra

Reputation: 3117

public int DownloadSoundFile()
{
    if (x!=null)
    {
        return x.Value;
    }
}

In this code what if x is null!!! In this condition how the function will return the value. So the correct code would be.

public int DownloadSoundFile()
{
    if (x!=null)
    {
        return x.Value;
    }
    else
    {
        // your return statement;
    }
}

You can also use below code.

return x != null ? x.value : *something else*

Using is not used to return something, it is mainly used so that code will not break because of exception. Mainly database connections are created in using statement. Because this makes sure that connections exceptions are suppressed and connection is closed once it goes out of scope.Generally objects inherited from IDisposable are used inside using statement so that dispose() method will be called.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273274

so we should return value out of the if scope.

No. You should return a value from an int method(). It has nothing to do with if() vs using().

public int DownloadSoundFile()
{
    if (x!=null)
    {
        return x.Value;
    }
    // and now?? no return value for the method
    // this is a 'code path that does not return a value'
}

Upvotes: 7

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

why we should return value out of the if scope but don't need return value out of the using Scope?

Because the if scope might not execute (if the condition is not satisfied) whereas the body of the using scope is guaranteed to always execute (it will either return the result or throw an exception which is acceptable for the compiler). For the if scope your method is undefined if the condition is not satisfied and the compiler refuses that.

So you should decide what value to return if the condition you wrote is not satisfied:

public int DownloadSoundFile()
{
    if (x != null)
    {
       return x.Value;
    }

    // at this stage you should return some default value
    return 0;
}

Upvotes: 20

Related Questions