what is use of out parameter in c#
请问
Related Question:
What is the difference between ref and out? (C#)
the best of an example of a good使用参数的方法
1 2 3 4 5 6 | int result =-1; if (!Int32.TryParse(SomeString, out result){ // log bad input } return result; |
本质allows for the
The out method parameter keyword on a
method parameter causes a method to
refer to the same variable that was
passed into the method. Any changes
made to the parameter in the method
will be reflected in that variable
when control passes back to the
calling method.Declaring an out method is useful when
you want a method to return multiple
values. A method that uses an out
parameter can still return a value. A
method can have more than one out
parameter.To use an out parameter, the argument
must explicitly be passed to the
method as an out argument. The value
of an out argument will not be passed
to the out parameter.A variable passed as an out argument
need not be initialized. However, the
out parameter must be assigned a value
before the method returns.
an example:P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System; public class MyClass { public static int TestOut(out char i) { i = 'b'; return -1; } public static void Main() { char i; // variable need not be initialized Console.WriteLine(TestOut(out i)); Console.WriteLine(i); } } |
msdn.microsoft.com http:/ / / / / aa336814.aspx vcsharp销售额P></
超时参数输出参数的意义是他们唯一可以从函数值只跟随在"停电"。我们的创建日期型参数by the parameter with the preceding modifier超时。当听到"超时"is only an unassigned参考参数passed passed is to the function。P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; class ParameterTest { static void Mymethod(out int Param1) { Param1=100; } static void Main() { int Myvalue=5; MyMethod(Myvalue); Console.WriteLine(out Myvalue); } } |
输出of the above the program的摇篮100 since参数value of the"超时"passed Calling is back to the part。注释P></
"前面的"should the modifier参数被passed茶茶叫"业余甚至。cannot be used within"参数值函数在assigning before the to it.在指定value should be to the before the method参数"超时"归来。P></
除了允许有多个返回值外,另一个用途是在将大值类型复制到方法时减少开销。当您将某个东西传递给某个方法时,会生成该东西的值的副本。如果它是一个引用类型(例如字符串),那么就复制一个引用(引用类型的值)。但是,当您复制一个值类型(像
1 2 3 4 5 6 7 8 | public struct BigStruct { public int A, B, C, D, E, F, G, H, J, J, K, L, M, N, O, P; } SomeMethod(instanceOfBigStruct); // A copy is made of this 64-byte struct. SomeOtherMethod(out instanceOfBigStruct); // No copy is made |
与此直接对应的第二个用法是,因为您不复制结构,但在方法中引用与在方法外部相同的内容,所以对方法内部对象所做的任何更改都将在方法外部持久化。引用类型中已经存在这种情况,但值类型中没有。
一些例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void ReferenceExample(SomeReferenceType s) { s.SomeProperty ="a string"; // The change is persisted to outside of the method } public void ValueTypeExample(BigStruct b) { b.A = 5; // Has no effect on the original BigStruct that you passed into the method, because b is a copy! } public void ValueTypeExampleOut(out BigStruct b) { b = new BigStruct(); b.A = 5; // Works, because you refer to the same thing here } |
现在,你可能已经注意到,在
但是,还有另一个关键字,
1 2 | int a; if(TrySomething(out a)) {} |
这是因为
1 2 | int a; if(TrySomething(ref a)) {} |
这不起作用,因为
这是因为
1 2 | int a = 0; if(TrySomething(ref a)) {} |
但是,在这两种情况下(
正如我已经说过的,对引用类型所做的更改被持久化在生成它们的方法之外,因为通过引用,您引用了相同的东西。
但是,这没有任何作用:
1 2 3 4 | public void Example(SomeReferenceType s) { s = null; } |
这里,您只需将对
如果您想这样做,无论出于什么原因,请使用:
1 2 3 4 | public void Example1(ref SomeReferenceType s) { s = null; // Sets whatever you passed into the method to null } |
我认为这涵盖了
msdn.microsoft.com from http://///EN -美国vcsharp aa336814.aspxP></
一路想is that they are out of parameters of a method类额外的返回值。他们是非常方便,当超过一个值在方法返回firstName和lastname example,this。不管一个人多abused can be out参数。良好的程序设计风格作为物理法如果你发现自己在写作,那么你应该用many out参数重构你的代码,我的一切。一个可能的解决方案是在the values to包装单struct回车进入。P></
在增强模式initially ref参数是指定在被叫方的事情考虑。as such is not required to the被叫,assign to the ref参数之前使用。参数都是ref和out of passed into a method。P></
简单地说,通过引用将任何变量传递给函数,以便当函数从执行返回时,对该函数一侧的变量所做的任何更改都将保持不变。
the method that is a使用典型的房子超过一件事needs to Return,知道它不只是使用the返回值。commonly value is used for the Return,while the旗在成功收购参数值(S)when the method is successful集合。P></
茶经example is:P></
1 2 3 4 | public bool TryGet( string key, out string value ) |
如果它返回true,则value is集。这是not otherwise,。这让你写这样的代码为:P></
1 2 3 | string value; if (!lookupDictionary.TryGet("some key", out value)) value ="default"; |
注意,这并不要求你在使用呼叫indexer which contains an清洁,让它更快。我也非常相似于add that,the modifier ref,编译器不会complain if the was never initialized超时参数。P></
我们generally cannot get inside the Bernoulli函数if we get a *值返回到待机模式。使用关键字"超时",但我们可以改变它在函数值模式。P></
斯基特Describes the different parameters of detail的方式通过在在这条大。总之,安超时参数passed that is is a参数初始化的方法。that the method is then来初始化参数在任何可能的收益。P></