什么是"的目的;从"在主叫方(在C#)的关键字? [英] What is the purpose of the "out" keyword at the caller (in C#)?
问题描述
在C#函数有一个输出参数,你说清楚如下:
When a C# function has an output parameter, you make that clear as follows:
private void f(out OutputParameterClass outputParameter);
这指出参数没有进行初始化时,函数被调用。但是,调用这个函数的时候,您必须重复out关键字
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?
推荐答案
最好的答案我被张贴通过的底座:
The best answer I got was posted as a comment by plinth:
有关出来的重复最重要的原因/ REF是,如果你调用该函数获取与重构不同的签名,你会得到一个编译错误。最值得注意的是,如果一个参数非出去了的话,你马上就会知道。
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.
这篇关于什么是"的目的;从"在主叫方(在C#)的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!