C# 中使用块的处置顺序

发布于 2024-09-13 20:55:48 字数 1067 浏览 4 评论 0原文

我真的很烦恼必须在 C# 中嵌套 using 块。它不优雅并且占用大量空间。在某些情况下,这似乎是不可避免的,因为我需要声明不同数据类型的变量,但似乎单一类型的情况应该可以简化。我所说的“单一类型情况”是指串联声明多个相同类型的变量。这是我正在谈论的一个例子:

class Program
{
    static void Main(string[] args)
    {
        using (A a = new A("a"), b = new A("b"))
        {
        }
    }

    class A : IDisposable
    {
        string n = null;
        public A(string name)
        {
            n = name;
            Console.WriteLine(String.Format("Creating {0}", n));
        }

        public void Dispose()
        {
            Console.WriteLine(String.Format("Disposing {0}", n));
        }
    }
}

我希望它的工作方式是 ab 之前构造,并且 b被放置在a之前。不幸的是,C# 规范中似乎没有任何关于它实际上应该如何发生的指示。看起来微软的 C# 编译器是这样对待它的,因为这是运行上述程序的输出:

Creating a
Creating b
Disposing b
Disposing a

但是,我无法确保这是确定性行为。有人可以证实或反驳这个序列是确定性的想法吗?参考文献会很棒。显然,如果它容易破损(未记录等),它可能没有用,但这是一件好事。

关于确定性处置已经有一个类似的问题 讨论了多种类型的情况,我知道除了巧妙的语法技巧之外没有真正的解决方案。无论如何,大多数答案都没有抓住重点。我的问题只是关于单一类型的情况以及这种处理是否是确定性的和可靠的。谢谢。

I'm really bothered by having to nest using blocks in C#. It's not elegant and it takes up a lot of space. In some cases it appears to be unavoidable because I need to declare variables of different data types, but it seems like the single-type case should be possible to simplify. What I mean by "the single-type case" is when several variables of the same type are declared in series. Here's an example of what I'm talking about:

class Program
{
    static void Main(string[] args)
    {
        using (A a = new A("a"), b = new A("b"))
        {
        }
    }

    class A : IDisposable
    {
        string n = null;
        public A(string name)
        {
            n = name;
            Console.WriteLine(String.Format("Creating {0}", n));
        }

        public void Dispose()
        {
            Console.WriteLine(String.Format("Disposing {0}", n));
        }
    }
}

The way I want this to work is that a is constructed before b, and that b is disposed before a. Unfortunately there doesn't appear to be any direction in the C# specification as to how it should actually happen. It appears as though Microsoft's C# compiler treats it like this, as this is the output of running the above program:

Creating a
Creating b
Disposing b
Disposing a

However, I have no way of ensuring that this is deterministic behavior. Can someone either confirm or refute the idea that this sequence is deterministic? References would be great. And obviously, if it's prone to breakage (undocumented, etc.) it's probably not useful, but that's a good thing to know.

There's already a similar question about deterministic disposal that talks about the multiple-type case, and I understand that there's no real solution there aside from clever syntax tricks. Most of the answers there miss the point, anyway. My question is just about the single-type case and whether this disposal is deterministic and dependable or not. Thanks.

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

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

发布评论

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

评论(2

情绪 2024-09-20 20:55:48

来自 C# 规范:

“A < using 形式的语句 using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) 完全等价于一系列嵌套的 using< /code> 声明:“

using (ResourceType r1 = e1)
   using (ResourceType r2 = e2)
      ...
         using (ResourceType rN = eN)
            statement

所以可以公平地说,这是一成不变的。

From the C# spec:

"A using statement of the form using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) is precisely equivalent to a sequence of nested using statements:"

using (ResourceType r1 = e1)
   using (ResourceType r2 = e2)
      ...
         using (ResourceType rN = eN)
            statement

So it seems fair to say that this is quite set in stone.

故笙诉离歌 2024-09-20 20:55:48

有什么理由不能使用多重使用技巧吗?这完全是确定性的,可以解决您的问题

using (A a = new A("a"))
using (A b = new A("b")) {
    // ....
}

Is there any reason why you can't use the multiple-using trick? That is entirely deterministic, and solves your problem

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