简体   繁体   English

调用方(在C#中)使用“ out”关键字的目的是什么?

[英]What is the purpose of the “out” keyword at the caller (in C#)?

When a C# function has an output parameter, you make that clear as follows: 当C#函数具有输出参数时,请按以下说明进行清除:

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 : 但是,调用此函数时, 必须重复out关键字

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? 有人知道吗?

It means you know what you're doing - that you're acknowledging it's an out parameter. 这意味着您知道自己在做什么-承认这是一个out参数。 Do you really want the utterly different behaviour to happen silently? 您是否真的希望完全不同的行为悄无声息地发生? The same is true for ref , by the way. refref也是如此。

(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... 这避免了在C ++中出现的那种情况,即您在不知不觉中通过引用传递某些东西的情况,但是假设该值没有改变...

It is a design feature. 这是一个设计功能。 It is clear that it was not necessary, but it aids in readability. 显然,这不是必需的,但有助于提高可读性。

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. 在调用此类重载时指定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 从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; 调用out参数的方法的调用者不需要在调用之前分配给作为out参数传递的变量。 however, the callee is required to assign to the out parameter before returning. 但是,被调用方必须在返回之前分配给out参数。

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. 我认为这是一件好事。

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. 但是,添加了out关键字后,您可以使意图更清晰,代码也更加清晰易读。

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. 重复out / ref的最重要原因是,如果您调用的函数使用不同的签名进行重构,则会出现编译错误。 Most notably, if a parameter goes from non-out to out, you'll know right away. 最值得注意的是,如果参数从非输出变为非输出,您将立即知道。

You probably have to use out for clarity. 为了清楚起见,您可能必须out If you wouldn't know without looking at the method signature. 如果您不查看方法签名就不会知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM