“出”的目的是什么?调用者处的关键字(在 C# 中)?
当 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这意味着您知道自己在做什么 - 您承认它是一个
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 forref
, 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...
这是一个设计特点。很明显,这不是必需的,但它有助于提高可读性。
It is a design feature. It is clear that it was not necessary, but it aids in readability.
虽然我不知道这个决定的起源,但我知道它有一个超载的目的。
在同一个类中创建这两个函数是完全合法的:
并且
在调用此类重载时指定
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:
and
Specifying the
out
keyword when calling such overload make sense.为了便于阅读,了解该方法可以/将对变量执行什么操作。
从 MSDN 获得更多信息:
http://msdn.microsoft.com/en-us/vcsharp/aa336814。 ASPX
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 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.
我认为这是一个一致性和清晰度的问题。
显然,没有编译器也可以做得很好。然而,添加了 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.
我得到的最佳答案是 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.
为了清楚起见,您可能必须使用
out
。如果您不查看方法签名就无法知道。You probably have to use
out
for clarity. If you wouldn't know without looking at the method signature.