what is use of out parameter in c#

后端 未结 9 1171
南方客
南方客 2021-02-07 16:27

Can you please tell me what is the exact use of out parameter?

Related Question:
What is the differen

9条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 16:44

    http://msdn.microsoft.com/en-us/vcsharp/aa336814.aspx

    Out parameters are output only parameters meaning they can only passback a value from a function.We create a "out" parameter by preceding the parameter data type with the out modifier. When ever a "out" parameter is passed only an unassigned reference is passed to the function.

    using System;
    class ParameterTest
    {
     static void Mymethod(out int Param1)
     {
      Param1=100;
     }
     static void Main()
     {
      int Myvalue=5;
      MyMethod(Myvalue);
      Console.WriteLine(out Myvalue);             
     }
    }
    

    Output of the above program would be 100 since the value of the "out" parameter is passed back to the calling part. Note

    The modifier "out" should precede the parameter being passed even in the calling part. "out" parameters cannot be used within the function before assigning a value to it. A value should be assigned to the "out" parameter before the method returns.

提交回复
热议问题