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

 三丶yield

1. yield必須出現在IEunmerable中

2. yield是迭代器的狀態機,能做到延遲查詢,使用的時候才查詢,可以實現按序加載

四丶Ref和Out

C#有兩種參數傳遞方式:傳值和引用,傳值就是變量的值,而引用則是傳遞的變量的地址;

本文中說的Ref和Out都是引用傳遞,Ref的重點是把值傳給調用方法,Out則是得到調用方法的值,類似於有返回類型的方法返回的值;

在使用兩者時一定要注意一下兩點,否則編譯出現錯誤

a) ref 變量使用前要先聲明同時要賦值 a=20;

b)方法調用參數要加上相應的關鍵字 ref or out;

static void main()
{
   int a = 20;
   int b = 30;
   int c;
   SwapMethod(ref a, ref b);
   Console.WriteLine(" After Swap a is {0},b is {1} ",a,b);

   OutTest(out c);
   Console.WriteLine("The out value is {0}.",c);
}

static void SwapMethod(ref int a,ref int b)
{
     int tem;
     tem = a;
     a = b;
     b = tem;
}

static void OutTest(out int a)
{
   a = 10 * 10;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM