当“回归”从'使用'块中调用该怎么办? [英] What happens when 'return' is called from within a 'using' block?
本文介绍了当“回归”从'使用'块中调用该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个方法,using块这样的...
If I have a method with a using block like this...
public IEnumerable<Person> GetPersons()
{
using (var context = new linqAssignmentsDataContext())
{
return context.Persons.Where(p => p.LastName.Contans("dahl"));
}
}
...从使用中返回值块,做了IDisposable对象仍然得到处理?
...that returns the value from within the using block, does the IDisposable object still get disposed?
推荐答案
是它。对象的处置发生在finally块,其执行甚至在回电话的脸。它本质上扩展到下面的代码
Yes it does. The disposing of the object occurs in a finally block which executes even in the face of a return call. It essentially expands out to the following code
var context = new linqAssignmentsDataContext();
try {
return context.Persons.Where(p => p.LastName.Contans("dahl"));
} finally {
if ( context != null ) {
context.Dispose();
}
}
这篇关于当“回归”从'使用'块中调用该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文