C# 中使用块的处置顺序
我真的很烦恼必须在 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));
}
}
}
我希望它的工作方式是 a
在 b
之前构造,并且 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 C# 规范:
“A <
using
形式的语句using (ResourceType r1 = e1, r2 = e2, ..., rN = eN)
完全等价于一系列嵌套的using< /code> 声明:“
所以可以公平地说,这是一成不变的。
From the C# spec:
"A
using
statement of the formusing (ResourceType r1 = e1, r2 = e2, ..., rN = eN)
is precisely equivalent to a sequence of nestedusing
statements:"So it seems fair to say that this is quite set in stone.
有什么理由不能使用多重使用技巧吗?这完全是确定性的,可以解决您的问题
Is there any reason why you can't use the multiple-using trick? That is entirely deterministic, and solves your problem