返回用于通过使用C#里面的变量 [英] return the variable used for using inside the using C#

查看:212
本文介绍了返回用于通过使用C#里面的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回到我创建了using语句内部使用语句变量(听起来很可笑):

I am returning the variable I am creating in a using statement inside the using statement (sounds funny):

public DataTable foo ()
{
    using (DataTable properties = new DataTable())
    {
       // do something
       return properties;
    }
}



这是否会处置的属性变量??

Will this Dispose the properties variable??

这样我仍然得到这个警告后:

After doing this am still getting this Warning:

警告34 CA2000:Microsoft.Reliability :在方法'test.test',所有引用之前调用System.IDisposable.Dispose的对象'属性'它超出范围。

Warning 34 CA2000 : Microsoft.Reliability : In method 'test.test', call System.IDisposable.Dispose on object 'properties' before all references to it are out of scope.

任何想法?

感谢

推荐答案

如果你想回到它,你不能把它包装在使用语句,因为一旦你离开了牙套,它超出范围,并得到处理。

If you want to return it, you can't wrap it in a using statement, because once you leave the braces, it goes out of scope and gets disposed.

您必须初始化它是这样的:

You will have to instantiate it like this:

public DataTable Foo() 
{ 
    DataTable properties = new DataTable();
    return properties; 
} 

和调用的Dispose()稍后再

这篇关于返回用于通过使用C#里面的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆