3

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?

Chris
  • 6,914
  • 5
  • 54
  • 80

4 Answers4

20

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;
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • But whay we should in using –  Jul 22 '13 at 07:47
  • 3
    You should use `using` when you are dealing with `IDisposable` instances. It will ensure that the Dispose method will be always invoked. – Darin Dimitrov Jul 22 '13 at 07:48
  • @ShahroozJefriㇱ http://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp – Jodrell Jul 22 '13 at 07:51
  • 1
    @ShahroozJefriㇱ You don't have to `return` from within a `using` block. You can very well call `return` when you're outside of the `using` block. Also, you don't *have to* `return` from outside an `if` block. If you `return` from within an `if` block, then you *must* also `return` from within an `else` block. – Nolonar Jul 22 '13 at 07:51
  • @ShahroozJefri, `return` returns from a method not from all blocks. A using block is not a method, http://msdn.microsoft.com/en-us/library/1h3swy84(v=vs.110).aspx – Jodrell Jul 22 '13 at 07:57
  • 1
    @FSou1, that's generally why we +1, yes :P – Moo-Juice Jul 22 '13 at 07:57
  • 1
    @Moo-Juice yeap ^.^ but sometimes, i have a strange feeling to tell about it – Maxim Zhukov Jul 22 '13 at 08:01
7

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'
}
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
H H
  • 263,252
  • 30
  • 330
  • 514
0
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.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Narendra
  • 3,069
  • 7
  • 30
  • 51
  • What is wrong if we use ?: operator in this manner. http://msdn.microsoft.com/en-US/library/ty67wk28(v=VS.80).aspx. return statements are also expressions so we can use them inside ?: – Narendra Jul 22 '13 at 08:01
  • From the [docs](http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx): `The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.` This means `condition ? first_statement : second_statement` is invalid. – Leri Jul 22 '13 at 08:02
  • Yes yes got it. My bad. – Narendra Jul 22 '13 at 08:03
  • You misunderstood the question. Shahrooz was asking why the code below the using statement doesn't require a return value, as it looked like another code path. – KappaG3 Jul 22 '13 at 08:06
  • To be clear for future readers: _Expression_ is something that evaluates to value; _Statement_ is line of code that does something. – Leri Jul 22 '13 at 08:07
0

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).

Leri
  • 12,367
  • 7
  • 43
  • 60
Toto
  • 7,491
  • 18
  • 50
  • 72