This question already has an answer here:
- returning in the middle of a using block 7 answers
Which way is better practice: return a value from a method inside an using
statement or declare a variable before, set it inside and return it after?
public int Foo()
{
using(..)
{
return bar;
}
}
or
public int Foo()
{
var b = null;
using(..)
{
b = bar;
}
return b;
}
I prefer the first example. Fewer variables, fewer lines of code, easier to follow, easier to maintain...
public int Foo()
{
using(..)
{
return bar;
}
}
Following the "less is more" principle (really just a variant of KISS), the former. There are fewer lines of code to maintain, no change in semantics and no loss of readability (arguably this style is easier to read).
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.
From the try-finally (C# Reference)
finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited.
To answer your question, yes its okay to return from a using statement.
The second is clearly better, and you can verify that it works fine by writing a test program.
The using
statement itself can't have a value, which is a limitation. Suppose you have a method called Open
that returns an open FileStream
, and you want to get the length of the file:
Console.WriteLine(Open().Length);
The bug there is that you aren't disposing the FileStream
. So you have to write (similar to your example):
long length;
using (FileStream file = Open())
length = file.Length;
Console.WriteLine(length);
But with a simple extension method, you can write this instead:
Console.WriteLine(Open().Use(file => file.Length));
Nice and neat, and the FileStream
gets properly disposed of.
No reason not to since that using
statement translates into a try...finally
block and the finally
part is guaranteed to be executed (even through a return or an unhandled exception).
It really comes down to personal preference. You'll find arguments on both sides of this particular fence. Myself, I favor option 1: returning as soon as you can. I believe it expresses the intent of the code better; there's no reason to stick around longer than you have to. If you've completed all your work, return.
Sometimes, you'll have multiple possible return points, and "end-of-method" work (logging, cleanup) that might lead you to a single return statement. Nothing terrible about that, but you can often handle those situations in finally
blocks or with aspects in aspect-oriented programming.
I feel second one better
public int Foo()
{
using(..)
{
return bar;
}
}
One thing that arises in mind while using this way is that we are returning in between the using so will the object(that we have wrapped in using ) will get disposed ,the Answer is yes because A using statement is just the mixture of try/finally block, it's fine to return from a try block too.The return expression will be evaluated, then the finally block will be executed, and the the method will return then.So go Ahead:)
来源:https://stackoverflow.com/questions/1223865/best-practice-regarding-returning-from-using-blocks