为什么在using语句中声明的变量被视为只读? [英] Why is a variable declared in a using statement treated as readonly?

查看:245
本文介绍了为什么在using语句中声明的变量被视为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么使用变量被视为只读?是c#语言规范还是托管语言规范?这是因为c#是.net语言吗?

Why using variable treated as readonly? It is c# language specification or managed languages specification? It is because c# is a .net language? Thanks in advance.

注意:使用变量是使用语句中出现的变量

Note: using variable is variable that appears in using statement

示例代码:

using (Form s = new Form)
{
    myfunc(ref s);
}

我们无法在using块中更改using变量的值。该代码将引发错误。

注意:我不想让您讨论只读关键字。

Note : i dont want you discuss about readonly keyword.

推荐答案

我现在正在查看一个(过时的)规格[1]。

I'm looking at an (outdated?) spec [1] right now.

15.13表示您在资源获取部分声明的变量是只读的。即:

15.13 says that variables that you declare in the resource acquisition part are readonly. That is:

var form = new Form1();
using (form) {
    form = null;
}

有效,但是

using (var form = new Form1()) {
    form = null;
}

没有。
这回答了部分问题(即为什么?因为它是规范的一部分..),但是我知道这并不是很令人满意。但是,您为什么还要这么做呢?

doesn't. This answers part of the question (i.e. Why? Because it is part of the spec..), but I understand that this is not really satisfying. But why would you even want to do that?

编辑:考虑了这一点之后,让我为您提供一个可能的解释这条规则:

After thinking about this, let me offer a possible explanation for this rule:

您已经

using (var something = new Foo()) {
   something = /* whatever */
}

并且编译器允许这样做。现在,如果Foo需要大量非托管资源(也许这就是您首先要使用 using 的原因)怎么办?在using块之后,您将无法再访问此引用。它没有被处理,因为您重新分配了某事却忘了自己处理。您根本无法保证GC能够运行。或何时。您刚刚创建了一个被隐藏和隐藏的资源泄漏。

and the compiler allows this. Now what if Foo needs a lot of unmanaged resources (maybe that's the reason you wanted to use using in the first place)? After the using block you have no way to access this reference anymore. It wasn't disposed, because you reassigned something and forgot to handle it yourself. You don't have a guarantee that the GC runs, at all. Or when. You just created a resource leak that is obscured and hidden.

最后一个,灵感来自Henk链接到Eric Lippert的博客,这又再次向我们抛出了规范:

A final one, inspired by Henk's link to Eric Lippert's blog, which again just ends up throwing the spec at us:


A使用以下形式的语句

A using statement of the form

使用(表达式)语句

具有相同的两个可能的扩展,但是在这种情况下ResourceType是
隐式地是表达式,资源变量
在嵌入式语句中不可访问,也不可见。

has the same two possible expansions, but in this case ResourceType is implicitly the compile-time type of the expression, and the resource variable is inaccessible in, and invisible to, the embedded statement.

换句话说:

var form = new Form1();
using (form) {
    form = null;
}

有效,因为它已扩展为

var form = new Form1();
var invisibleThing = form;
try {
   form = null;
} finally {
    if (invisibleThing != null) ((IDisposable)invisibleThing).Dispose();
}

因此,在这种情况下,您对<$ c没有任何影响$ c>使用引用对您而言是隐藏的,与上一种情况完全相同。

So in this case the fact that you have no influence over the using reference is just hidden from you and is exactly like in the previous case.

1:http://www.ecma -international.org/publications/standards/Ecma-334.htm

1:http://www.ecma-international.org/publications/standards/Ecma-334.htm

这篇关于为什么在using语句中声明的变量被视为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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