为什么 C# 不允许将 using 变量作为 ref 或 out 传递给函数

发布于 2024-11-07 01:21:00 字数 431 浏览 0 评论 0原文

可能的重复:
通过引用传递 IDisposable 对象会导致错误?

为什么 C# 不允许将变量作为 ref 或 out 从 using 块传递到函数?

这是我的代码:

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

函数在 using 块结束之前结束,为什么 C# 不让我将 s 作为 ref 或 out 参数传递?

Possible Duplicate:
Passing an IDisposable object by reference causes an error?

Why doesn't C# allow passing a variable from a using block to a function as ref or out?

This is my code:

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

The function ends before the using block ends, why doesn't C# let me pass s as ref or out parameter?

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

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

发布评论

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

评论(3

音盲 2024-11-14 01:21:00

using 变量被视为只读,因为任何重新分配都可能是错误。由于 ref 允许重新分配,这也是一个问题。在 IL 级别,outref 几乎相同。

但是,我怀疑您需要此处ref;您已经将引用传递给表单,因为它是一个类。对于引用类型,ref 的主要目的是允许您重新分配变量,并让调用者看到重新分配,即,

void doSomething(ref Form form)
{
    form = null; // the caller will see this change
}

如果您只是与表单对象对话,则不需要

void doSomething(Form form)
{
    form.Text = "abc"; // the caller will see this change even without ref
}

因为它是相同的表单对象

using variables are treated as readonly, as any reassignment is probably an error. Since ref allows reassignment, this would also be an issue. At the IL level, out is pretty-much identical to ref.

However, I doubt you need ref here; you are already passing a reference to the form, since it is a class. For reference-types, the main purpose of a ref would be to allow you to reassign the variable, and have the caller see the reassignment, i.e.

void doSomething(ref Form form)
{
    form = null; // the caller will see this change
}

it is not required if you are just talking to the form object:

void doSomething(Form form)
{
    form.Text = "abc"; // the caller will see this change even without ref
}

since it is the same form object.

慢慢从新开始 2024-11-14 01:21:00

using() 语句中的 var 在块内被视为只读。请参阅§ 8.13

在 a 中声明的局部变量
资源获取是只读的,
并且必须包含一个初始化程序。一个
如果出现编译时错误
嵌入语句尝试修改
这些局部变量(通过赋值
或 ++ 和 -- 运算符)或通过
它们作为 ref 或 out 参数。

但请注意,这仅适用于声明为 using 语句一部分的变量,以下内容是合法的(只是不是一个好主意):

var f2 = System.IO.File.OpenText("");
using (f2)
{
    f2 = null;
}

The var in a using() statement is considered read-only inside the block. See § 8.13:

Local variables declared in a
resource-acquisition are read-only,
and must include an initializer. A
compile-time error occurs if the
embedded statement attempts to modify
these local variables (by assignment
or the ++ and -- operators) or pass
them as ref or out parameters.

But note that this only applies to variables declared as part of the using statement, the following is legal (just not a good idea):

var f2 = System.IO.File.OpenText("");
using (f2)
{
    f2 = null;
}
辞慾 2024-11-14 01:21:00

原因之一可能是 doSomthing 可能使 s 引用另一个 Form 实例,而不是我们创建的实例。这可能会导致资源泄漏,因为 using 块随后会在来自该方法的 Form 实例上调用 Dispose,而不是在 using 块中创建的实例。

One reason could be that doSomthing could make s refer to another Form instance than the one we have created. That could introduce a resource leak since the using block would then invoke Dispose on the Form instance that came from the method, and not the one created in the using block.

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