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

岁酱吖の 提交于 2020-01-27 08:31:47

问题


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?


回答1:


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...




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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



来源:https://stackoverflow.com/questions/1393946/what-is-the-purpose-of-the-out-keyword-at-the-caller-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!