返回用于在 using C# 中使用的变量

发布于 2024-09-01 12:09:54 字数 433 浏览 5 评论 0原文

我正在返回我在 using 语句内的 using 语句中创建的变量(听起来很有趣):

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

这会处理属性变量吗?

执行此操作后,我仍然收到此警告:

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

有什么想法吗?

谢谢

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:

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.

Any Ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

誰ツ都不明白 2024-09-08 12:09:54

如果您想返回它,则不能将其包装在 using 语句中,因为一旦离开大括号,它就会超出范围并被释放。

您必须像这样实例化它:

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

并稍后对其调用 Dispose()

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; 
} 

and call Dispose() on it later.

青柠芒果 2024-09-08 12:09:54

是的,它会处理它 - 然后返回它。这几乎总是一件坏事。

事实上,对于 DataTable 来说,Dispose 几乎从不执行任何操作(例外是如果它远程在某个地方,IIRC),但这通常仍然是一个坏主意。通常您应该将已处置的对象视为无法使用。

Yes, it will dispose it - and then return it. This is almost always a bad thing to do.

In fact for DataTable, Dispose almost never does anything (the exception being if it's remoted somewhere, IIRC) but it's still a generally bad idea. Normally you should regard disposed objects as being unusable.

深陷 2024-09-08 12:09:54

据推测,这是创建一次性对象的工厂方法的模式。但是,我仍然看到代码分析对此抱怨:

        Wrapper tempWrapper = null;
        Wrapper wrapper = null;

        try
        {
            tempWrapper = new Wrapper(callback);
            Initialize(tempWrapper);

            wrapper = tempWrapper;
            tempWrapper = null;
        }
        finally
        {
            if (tempWrapper != null)
                tempWrapper.Dispose();
        }

        return wrapper;

这应该保证如果初始化失败,则正确处置对象,但如果一切成功,则从该方法返回未处置的实例。

MSDN 文章:CA2000:在失去范围之前释放对象

Supposedly, this is the pattern for a factory method that creates a disposable object. But, I've still seen Code Analysis complain about this, too:

        Wrapper tempWrapper = null;
        Wrapper wrapper = null;

        try
        {
            tempWrapper = new Wrapper(callback);
            Initialize(tempWrapper);

            wrapper = tempWrapper;
            tempWrapper = null;
        }
        finally
        {
            if (tempWrapper != null)
                tempWrapper.Dispose();
        }

        return wrapper;

This should guarantee that if the initialization fails, the object is properly disposed, but if everything succeeds, an undisposed instance is returned from the method.

MSDN Article: CA2000: Dispose objects before losing scope.

徒留西风 2024-09-08 12:09:54

是的。为什么要在代码块末尾使用 using 关键字来处理您不希望处理的内容?

using 关键字的目的是释放该对象。

http://msdn.microsoft.com/en-us/library/yh598w02。 ASPX

Yes. Why are you using the using keyword on something you don't want disposed at the end of the code block?

The purpose of the using keyword is to dispose of the object.

http://msdn.microsoft.com/en-us/library/yh598w02.aspx

爱她像谁 2024-09-08 12:09:54

using 块的要点是为值/对象创建一个人工范围。当 using 块完成时,该对象将被清除,因为不再需要它。如果您确实想返回正在创建的对象,那么您不想使用 using 。

这会工作得很好。

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

The point of a using block is to create an artificial scope for a value/object. When the using block completes, the object is cleaned up because it is no longer needed. If you really want to return the object you are creating, than it is not a case where you want to use using.

This will work just fine.

public DataTable foo ()
{
    DataTable properties = new DataTable();
    // do something
    return properties;
}
锦爱 2024-09-08 12:09:54

使用 using 关键字的代码扩展为:

{
    DataTable properties = new DataTable();
    try
    {
        //do something
        return properties;
    }
    finally
    {
        if(properties != null)
        {
            ((IDisposable)properties).Dispose();
        }
    }
}

您的变量根据 using 的工作方式进行处理。如果您希望能够返回属性,请勿将其包装在 using 块中。

Your code using the using keyword expands to:

{
    DataTable properties = new DataTable();
    try
    {
        //do something
        return properties;
    }
    finally
    {
        if(properties != null)
        {
            ((IDisposable)properties).Dispose();
        }
    }
}

Your variable is being disposed by nature of how using works. If you want to be able to return properties, don't wrap it in a using block.

一抹微笑 2024-09-08 12:09:54

其他响应是正确的:一旦退出 using 块,您的对象就会被释放。 using 块非常适合确保及时处置对象,因此如果您不想依赖函数的使用者记住稍后处置对象,您可以尝试如下操作

public void UsingDataContext (Action<DataContext> action)
{
    using (DataContext ctx = new DataContext())
    {
       action(ctx)
    }
}

:你可以这样说:

var user = GetNewUserInfo();
UsingDataContext(c => c.UserSet.Add(user));

The other responses are correct: as soon as you exit the using block, your object is disposed. The using block is great for making sure that an object gets disposed in a timely manner, so if you don't want to rely on the consumers of your function to remember to dispose the object later, you can try something like this:

public void UsingDataContext (Action<DataContext> action)
{
    using (DataContext ctx = new DataContext())
    {
       action(ctx)
    }
}

This way you can say something like:

var user = GetNewUserInfo();
UsingDataContext(c => c.UserSet.Add(user));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文