Given a situation like this:
using (var foo = CreateFoo()) {
if (foo != null) {
// do stuff
}
}
I would like to avoid the nested if. Sadly, the obvious solution is not possible because break does not work with using:
using (var foo = CreateFoo()) {
if (foo == null) {
break;
}
// do stuff
}
Is there a pattern to still avoid the additional indentation caused by the if != null
?
如果你对从CreateFoo()
返回的类有足够的控制,你可以只实现一个Null对象并返回它而不是实际的NULL值
I favor small clearly named methods:
public void DoWhatEver()
{
using (var foo = CreateFoo())
{
if (foo == null) return;
//DoWhatEver
}
}
Introduce a helper method that takes a lambda. So your code becomes:
UsingIfNotNull(CreateFoo(), foo => {
//do stuff
});
which has the indentation you want. The definition of UsingIfNotNull is:
public static void UsingIfNotNull<T>(T item, Action<T> action) where T : class, IDisposable {
if(item!=null) {
using(item) {
action(item);
}
}
}
This is just a style issue ... code is fine. Are you really that worried about indents? Here's another way to lose the indents anyway ...
public void DoWhatEver()
{
using(var foo = CreateFoo())
{
DoStuffWithFoo(foo);
}
}
private void DoStuffWithFoo(Foo foo)
{
if(foo == null) return;
//DoWhatEver
}
在那种通用意义上,我相信我会将try...catch
包装在try...catch
块中并在对象为null时抛出异常,但这是个人偏好。
It's an ugly hack, but it avoids the additional identation:
do using (var foo = CreateFoo()) {
if (foo == null) {
break;
}
// do stuff
} while (false);
(No, I don't recommend to do this. This is just a proof-of-concept to show that it's possible.)
If possible, I would suggest to refactor your code instead:
using (var foo = CreateFoo()) {
if (foo != null) {
doSomethingWith(foo); // only one line highly indented
}
}
Personally I would probably leave the code as you've posted it.
However, since you asked (and at the risk of exposing myself to downvotes to this often-maligned language feature), you could always use "goto":
using (var foo = CreateFoo()) {
if (foo == null) {
goto SkipUsingBlock;
}
// do stuff
}
SkipUsingBlock:
// the rest of the code...
C# compiler treats using(var foo = CreateFoo()) statement in:
try
{
var foo = CreateFoo();
}
finally
{
((IDisposable) foo).Dispose();
}
If your method CreateFoo return not disposable object - do not use using at all. In other case you can write:
try
{
var foo = CreateFoo();
//do stuff like foo.SomeMethod (if foo == null exception will be thrown and stuff will not be done)
}
finally
{
((IDisposable) foo).Dispose();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.