“出”的目的是什么?调用者处的关键字(在 C# 中)?

发布于 2024-08-04 08:12:39 字数 263 浏览 7 评论 0原文

当 C# 函数具有输出参数时,请按如下方式明确说明:

private void f(out OutputParameterClass outputParameter);

这表明调用函数时不必初始化参数。但是,在调用此函数时,您必须重复 out 关键字

f(out outputParameter);

我想知道这有什么用。为什么需要重复部分功能规范?有谁知道吗?

When a C# function has an output parameter, you make that clear as follows:

private void f(out OutputParameterClass outputParameter);

This states that the parameter does not have to be initialized when the function is called. However, when calling this function, you have to repeat the out keyword:

f(out outputParameter);

I am wondering what this is good for. Why is it necessary to repeat part of the function specification? Does anyone know?

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

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

发布评论

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

评论(8

芸娘子的小脾气 2024-08-11 08:12:39

这意味着您知道自己在做什么 - 您承认它是一个 out 参数。您真的希望完全不同的行为悄无声息地发生吗?顺便说一句,ref 也是如此。

(您也可以基于按值与 out/ref 进行重载,但我不推荐这样做。)

基本上,如果您有一个(未捕获的)局部变量并将其用作非 out/ref 参数,您知道该变量的值不会在方法内更改。 (如果它是引用类型变量,则它所引用的对象内的数据可能会更改,但这是非常不同的。)

这可以避免在 C++ 中出现的情况,即您不知不觉通过引用传递某些内容,但假设该值没有改变......

It means you know what you're doing - that you're acknowledging it's an out parameter. Do you really want the utterly different behaviour to happen silently? The same is true for ref, by the way.

(You can also overload based on by-value vs out/ref, but I wouldn't recommend it.)

Basically, if you've got an (uncaptured) local variable and you use it as a non-out/ref argument, you know that the value of that variable won't be changed within the method. (If it's a reference type variable then the data within the object it refers to may be changed, but that's very different.)

This avoids the kind of situation you get in C++ where you unknowingly pass something by reference, but assume that the value hasn't changed...

南…巷孤猫 2024-08-11 08:12:39

这是一个设计特点。很明显,这不是必需的,但它有助于提高可读性。

It is a design feature. It is clear that it was not necessary, but it aids in readability.

向日葵 2024-08-11 08:12:39

虽然我不知道这个决定的起源,但我知道它有一个超载的目的。

在同一个类中创建这两个函数是完全合法的:

private void f(out OutputParameterClass outputParameter);

并且

private void f(OutputParameterClass outputParameter);

在调用此类重载时指定 out 关键字是有意义的。

While I don't know the origin of such decision, I know that it has a purpose for overloading.

It is totally legal to create these two functions in the same class:

private void f(out OutputParameterClass outputParameter);

and

private void f(OutputParameterClass outputParameter);

Specifying the out keyword when calling such overload make sense.

廻憶裏菂餘溫 2024-08-11 08:12:39

为了便于阅读,了解该方法可以/将对变量执行什么操作。

从 MSDN 获得更多信息:
http://msdn.microsoft.com/en-us/vcsharp/aa336814。 ASPX

方法的调用者,该方法采用
不需要 out 参数
分配给作为传递的变量
调用前的输出参数;
但是,被调用者需要
之前分配给 out 参数
返回。

For readability, knowing what the method can/will do to your variable.

Got some more info from MSDN:
http://msdn.microsoft.com/en-us/vcsharp/aa336814.aspx

The caller of a method which takes an
out parameter is not required to
assign to the variable passed as the
out parameter prior to the call;
however, the callee is required to
assign to the out parameter before
returning.

情丝乱 2024-08-11 08:12:39

我能看到的唯一原因是确保函数的用户知道该参数的值可以由函数修改。我认为这是一件好事。

The only reason I can see is to make sure the user of the function knows that the value of this parameter can be modified by the function. I think it's a good thing.

云胡 2024-08-11 08:12:39

我认为这是一个一致性和清晰度的问题。

显然,没有编译器也可以做得很好。然而,添加了 out 关键字后,您的意图就变得清晰了,代码也变得更加清晰易读。

I think it's a matter of consistency and clarity.

Clearly, the compiler could do well without. However, with the out keyword added, you're making your intentions clear, and the code gets clearer and more legible.

铃予 2024-08-11 08:12:39

我得到的最佳答案是 plinth 的评论:

重复 out/ref 的最重要原因是如果您调用的函数使用不同的签名进行重构,您将收到编译错误。最值得注意的是,如果参数从非输出变为输出,您会立即知道。

The best answer I got was posted as a comment by plinth:

The most important reason for the repeating of out/ref is that if the function you're calling gets refactored with a different signature, you will get a compile error. Most notably, if a parameter goes from non-out to out, you'll know right away.

眼趣 2024-08-11 08:12:39

为了清楚起见,您可能必须使用 out。如果您不查看方法签名就无法知道。

You probably have to use out for clarity. If you wouldn't know without looking at the method signature.

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