我有几件一次性物品要处理。CA2000规则要求我在退出作用域之前释放我的所有对象。如果我可以使用using子句,我不喜欢使用.Dispose()方法。在我的具体方法中,我应该编写许多在使用中使用的:
using (Person person = new Person()) {
using (Adress address = new Address()) {
// my code
}
}是否可以用另一种方式来写:
using (Person person = new Person(); Adress address = new Address())发布于 2012-12-13 15:41:04
可以在using语句中声明两个或多个对象(用逗号分隔)。缺点是它们必须是相同的类型。
Legal:
using (Person joe = new Person(), bob = new Person())非法:
using (Person joe = new Person(), Address home = new Address())您能做的最好的就是嵌套使用语句。
using (Person joe = new Person())
using (Address home = new Address())
{
// snip
}发布于 2012-12-13 15:37:31
你能做的最好就是:
using (Person person = new Person())
using (Address address = new Address())
{
// my code
}发布于 2012-12-13 15:44:35
你可以
using (IDisposable iPerson = new Person(), iAddress = new Address())
{
Person person = (Person)iPerson;
Address address = (Address)iAddress;
// your code
}但这几乎不是什么进步。
https://stackoverflow.com/questions/13863200
复制相似问题