输出参数的重要性 [英] significance of out parameter
本文介绍了输出参数的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好...
谁能解释我,为什么和在哪里使用"out"关键字及其含义.预先感谢
hi everyone...
Can anyone please explain me , why and where we use "out" keyword and the significance of it. Thanks in advance
推荐答案
out
关键字仅表示参数是通过引用而不是通过值传递的.这基本上意味着被调用的方法可以更改值.看看MSDN页面 out(C#) [ ^ ]示例和更清晰的解释.
The out
keyword just means that the parameter is passed by reference rather than by value. This basically means that the called method can change the value. Take a look at the MSDN page out(C#)[^] for examples and clearer explanation.
除了Wayne解释的内容:
关键字out
还表示参数应该更改方法实现中的值.下面的代码示例显示了这种约束,并解释了ref和out
之间的本质区别;两种模式均为参考:
In addition to what Wayne explains:
The keywordout
also says that the parameter should change the value in the method implementation. The code sample below shows that constraint and explains essential difference between ref andout
; both modes are by-reference:
struct MyStructure { /*...*/ }
struct SomeType {
void SomeMethod(ref MyStructure reference) { } //does nothing, but it will compile
void SomeBadMethod(out MyStructure reference) { } //will not compile because out parameter is not assigned
void FixedMethod(out MyStructure reference) {
//...
out = new MyStructure(/*...*/); //this will fix the above method, will compile
}
}
另外,不需要调用代码来初始化用作out
参数的变量(因为它将由于调用而被初始化).
Also, a calling code is not required to initialize a variable used as out
parameter (as it will be initialized as a result of the call).
您尝试使用Google搜索吗?我找到了这些:
http://msdn.microsoft.com/en-us/library/t3c3bfhx (v = vs.80).aspx [ http://msdn.microsoft.com/en-us/library/aa645764 (v = vs.71).aspx [
Did you try Googling this? I found these:
http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx[^]
http://msdn.microsoft.com/en-us/library/aa645764(v=vs.71).aspx[^]
这篇关于输出参数的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文