C#当中的out关键字(借鉴于CSDN)

一丶与ref关键字一样,out关键字也是按引用来传递的.out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字

尽管作为 out 参数传递的变量不需要在传递之前进行初始化,但需要调用方法以便在方法返回之前赋值。

我们发现,ref和out似乎可以实现相同的功能.因为都可以改变传递到方法中的变量的值.但是,二者本质的区别就是,ref是传入值,out是传出值.在含有out关键字的方法中,变量必须由方法参数中不含out(可以是ref)的变量赋值或者由全局(即方法可以使用的该方法外部变量)变量赋值,out的宗旨是保证每一个传出变量都必须被赋值

示例演示了out关键字的使用方法,其功能是获取数组中的最大值和最大值的索引

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Out_Key
 8 {
 9     class Program
10     {
11         ///<summary>
12         ///求数组的最大值和最大值的索引
13         ///</summary>
14         ///<param name="m_Array" >传入的数组</param>
15         ///<param name="m_Index" >要求的最大索引</param>
16         ///<returns >数组中的最大值</returns>
17         static int MaxIndex(int[] m_Array, out int m_Index)
18         {
19             m_Index = 0;
20             int m_Max = m_Array[0];
21 
22             for (int i = 0; i < m_Array.Length; i++)
23             {
24                 if (m_Array[i] > m_Max)
25                 {
26                     m_Max = m_Array[i];
27                     m_Index = i;
28                 }
29             }
30 
31             return m_Max;
32         }
33         static void Main(String[] args)
34         {
35             int[] myArray = new int[5] { 56, 89, 425, 21, 21 };
36             int maxIndex;
37             Console.WriteLine("数组中最大的值为:{0}", MaxIndex(myArray, out maxIndex));
38             //调用MaxIndex方法后  maxIndex的值为2
39             Console.WriteLine("数组中最大的值是第{0}元素", maxIndex + 1);
40             Console.ReadKey();
41         }
42     }
43 }
View Code

result:

说明:

1.out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于ref 要求变量必须在传递之前进行初始化。

2.方法定义和调用方法都必须显式使用 out 关键字。

3.属性不是变量,因此不能作为 out 参数传递。

二丶

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace sampsong
 6 {
 7     class Program1
 8     {
 9         static void Method(out String name,out String sex)
10         {
11             name = "张三";
12             sex="";
13         }
14 
15         static void Main(String[] args)
16         {
17             String Name;
18             String Sex;
19 
20             Method(out Name,out Sex);
21             Console.WriteLine("调用方法Method后");
22             Console.WriteLine("学生" + Name + "性别是:"+Sex );
23         }
24     }
25 }    
View Code
 1   static void Method(out int i)
 2         {
 3             i = 44;
 4         }
 5         static void Main()
 6         {
 7             int value;
 8             Method(out value);
 9             Console.Write(value);
10             // value is now 44
11             Console.ReadKey();
12         }
View Code

ref 和 out 关键字在运行时的处理方式不同,但在编译时的处理方式相同。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:

class class Program
{
        public void TestMethod(out int i) {    }
        public void TestMethod(ref int i) {    }
}

如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两类参数,则可以进行重载,如下所示:

 public void TestMethod(int i) { }
 public void TestMethod(out int i) {
            i = 1;
 }

属性不是变量,因此不能作为 out 参数传递。

有关传递数组的信息,请参见使用 ref 和 out 传递数组。 
当希望方法返回多个值时,声明 out 方法很有用。使用 out 参数的方法仍然可以将变量用作返回类型(请参见 return),但它还可以将一个或多个对象作为 out 参数返回给调用方法。此示例使用 out 在一个方法调用中返回三个变量。请注意,第三个参数所赋的值为 Null。这样便允许方法有选择地返回值。

 1 class Program
 2 {
 3         static void Method(out int a, out string b, out string c)
 4         {
 5                 a = 44;
 6                 b = "I've been returned";
 7                 c= null;
 8         }
 9         static void Main()
10         {
11                 int value;
12                 string str1, str2;
13                 Method(out value, out str1, out str2);
14         }
15 }
View Code

猜你喜欢

转载自www.cnblogs.com/chenze-Index/p/9329810.html