using 块中实例化的所有一次性对象是否都已处理?

发布于 2024-08-22 03:37:00 字数 351 浏览 9 评论 0原文

这是我过去多次问自己的问题,因为我使用 5 层深度的语句进行嵌套。

阅读文档,发现没有提及任何有关其他< /em> 在块内实例化的一次性物品我认为这对于 SO 档案来说是一个很好的 Q。

考虑一下:

using (var conn = new SqlConnection())
{
    var conn2 = new SqlConnection();
}

// is conn2 disposed?

This is a question I have asked myself many times in the past as I nested using statements 5 deep.

Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives.

Consider this:

using (var conn = new SqlConnection())
{
    var conn2 = new SqlConnection();
}

// is conn2 disposed?

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

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

发布评论

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

评论(7

走过海棠暮 2024-08-29 03:37:00

不,他们不是。只有 using 子句中明确列出的变量集才会被自动处理。

No they are not. Only the set of variables explicitly listed in the using clause will be automatically disposed.

长途伴 2024-08-29 03:37:00

显然我已经有了答案...;-)

答案是否定的。仅处理 using 声明中的对象

[Test]
public void TestUsing()
{
    bool innerDisposed = false;
    using (var conn = new SqlConnection())
    {
        var conn2 = new SqlConnection();
        conn2.Disposed += (sender, e) => { innerDisposed = true; };
    }

    Assert.False(innerDisposed); // not disposed
}

[Test]
public void TestUsing2()
{
    bool innerDisposed = false;
    using (SqlConnection conn = new SqlConnection(), conn2 = new SqlConnection())
    {
        conn2.Disposed += (sender, e) => { innerDisposed = true; };
    }
    Assert.True(innerDisposed); // disposed, of course
}

Obviously I have the answer... ;-)

The answer is no. Only the objects in the using declaration are disposed

[Test]
public void TestUsing()
{
    bool innerDisposed = false;
    using (var conn = new SqlConnection())
    {
        var conn2 = new SqlConnection();
        conn2.Disposed += (sender, e) => { innerDisposed = true; };
    }

    Assert.False(innerDisposed); // not disposed
}

[Test]
public void TestUsing2()
{
    bool innerDisposed = false;
    using (SqlConnection conn = new SqlConnection(), conn2 = new SqlConnection())
    {
        conn2.Disposed += (sender, e) => { innerDisposed = true; };
    }
    Assert.True(innerDisposed); // disposed, of course
}
断念 2024-08-29 03:37:00

如果您想要 using 语句的确切规则,请参阅规范的第 8.13 节。您所有的问题都应该在那里得到明确的答案。

If you want the exact rules for the using statement see section 8.13 of the specification. All your questions should be clearly answered there.

纸短情长 2024-08-29 03:37:00

不,它不起作用,conn2 将不会被释放。
请注意,多个 using 是我允许不使用括号以获得更多灵活性的唯一情况:

        using (var pen = new Pen(color, 1))
        using (var brush = new SolidBrush(color))
        using (var fontM60 = new Font("Arial", 15F, FontStyle.Bold, GraphicsUnit.Pixel))
        using (var fontM30 = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel))
        using (var fontM15 = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Pixel))
        using (var fontM05 = new Font("Arial", 10F, FontStyle.Regular, GraphicsUnit.Pixel))
        using (var fontM01 = new Font("Arial", 8F, FontStyle.Regular, GraphicsUnit.Pixel))
        using (var stringFormat = new StringFormat())
        {
        }

这样,嵌套 using 就不是什么大问题了。

No, it doesn't work, conn2 will not be disposed.
Note that multiples using are the only situation where I allow not using brackets for more lisibility :

        using (var pen = new Pen(color, 1))
        using (var brush = new SolidBrush(color))
        using (var fontM60 = new Font("Arial", 15F, FontStyle.Bold, GraphicsUnit.Pixel))
        using (var fontM30 = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel))
        using (var fontM15 = new Font("Arial", 12F, FontStyle.Regular, GraphicsUnit.Pixel))
        using (var fontM05 = new Font("Arial", 10F, FontStyle.Regular, GraphicsUnit.Pixel))
        using (var fontM01 = new Font("Arial", 8F, FontStyle.Regular, GraphicsUnit.Pixel))
        using (var stringFormat = new StringFormat())
        {
        }

This way, nested using are not a big deal.

简单 2024-08-29 03:37:00

不会。Using 会导致using 语句中的对象被释放。如果您希望释放两个对象,则应将其重写为:

using (var conn = new SqlConnection())
{
    using (var conn2 = new SqlConnection())
    {
        // use both connections here...
    }
}

或者,您可以使用更简洁的语法:

using (SqlConnection conn = new SqlConnection(), conn2 = new SqlConnection())
{
    // use both connections here...
}

No. Using causes the object in the using statement to be disposed. If you want both of your objects to be disposed, you should rewrite this as:

using (var conn = new SqlConnection())
{
    using (var conn2 = new SqlConnection())
    {
        // use both connections here...
    }
}

Or, alternatively, you can use the more succinct syntax:

using (SqlConnection conn = new SqlConnection(), conn2 = new SqlConnection())
{
    // use both connections here...
}
煞人兵器 2024-08-29 03:37:00

否。使用 ILDASM 或 Reflector 检查生成的 IL。

No. Check the generated IL with ILDASM or Reflector.

も让我眼熟你 2024-08-29 03:37:00

只有 using() 中的变量会被释放,而不是实际的代码块。

Only the variables within the using() will be disposed, not the actual code block.
.

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