Home [C# 筆記] out, ref
Post
Cancel

[C# 筆記] out, ref

取得計算結果的方式有兩種:

  • 第一種方式:返回值 return 的方式 (by value)
  • 第二種方式:參數輸出的方式 out, ref
    • out 輸出參數 (by output)
    • ref 傳址參數 (by reference)

返回值 return 的方式

1
2
3
4
int result = Add(10, 20);
int Add(int x, int y) {
    return x + y;
}

使用 out 參數輸出 (by output)

1
2
3
4
5
6
7
8
//調用out參數的方法
int result;
Add(10, 20, out result);

//定義out參數的方法
void Add(int x, int y, out int result) {
    result = x + y;
}

程式說明:

  • 不要返回值,將方法改成void
  • 在定義方法中,加上要回傳的參數int result,並在前面加上out變成引用類型,成為out int result
  • 在定義方法中,將運算結果賦值給 out 參數 result = x + y;。(out參數一定要賦值,不然會報錯)
  • 在調用方法前,必須先宣告變數,不必初始化給值int result;
  • 宣告的變數,就算給值也會被忽略,所以它只用來接收回傳值。(by output)

也可以直接將result定義寫在調用的方法中Add(10, 20, out int result);,省去這段int result;。 也就是說,在調用方法的時,順便定義一個result這個變數。

1
2
3
4
5
6
7
8
9
10
11
//調用有out參數的方法
Add(10, 20, out int result); 

//在調用方法以下部分的程式碼,都可以使用這個result進行操作。  
Console.WriteLine(result); 
Console.ReadKey();

//定義有out參數的方法
void Add(int x, int y, out int result) { 
    result = x + y; 
}

out最大的用途就是,調用一次方法,就可以取得多個返回值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Operation(100, 20, out int addResult, out int subResult, out int mulResult, out int divResult);

Console.WriteLine($"100+20={addResult}");
Console.WriteLine($"100-20={subResult}");
Console.WriteLine($"100*20={mulResult}");
Console.WriteLine($"100/20={divResult}");
Console.ReadKey();

void Operation(int x, int y, out int addResult, out int subResult, out int mulResult, out int divResult)
{
    addResult = x + y;
    subResult = x - y;
    mulResult = x * y;
    divResult = x / y;
}

使用 ref 參數輸出 (by reference)

1
2
3
4
5
6
int result = 0;
Add(10, 20, ref result);

void Add(int x, int y, ref int result) {
    result = x + y;
}

程式說明:

  • 不要返回值,方法改成void
  • 在定義方法中,加上要回傳的參數int result,並在前面加上ref關鍵字,成為ref int result
  • 在定義方法中,將運算結果賦值給 ref 參數 result = x + y;。(ref參數沒有賦值,不會報錯)
  • 調用該方法前,必須先宣告變數,一定要初始化給值int result = 0;。 (必須有初始值)
  • 宣告的變數,給的值可以傳遞到方法中,所以它可以傳遞值,也可以接收回傳值。(by reference)

範例:
希望能夠將宣告變數的數值,傳入方法中做運算後,處理過的結果能夠回存到原本的變數中

1
2
3
4
5
6
7
8
9
int result = 100; //初始化
Add(10, 20, ref result); //調用ref參數的方法(它可以將100帶入方法中)

void Add(int x, int y, ref int result) {
    result = x + y + result; //result= 10+20+100
}

Console.WriteLine(result); //130, 處理過的結果回存到原本的變數中
Console.ReadKey();

調用方法前 result = 100
調用方法後 result = 130


R Notes:

out, ref 兩者都是 by reference,差別在:

  • out 輸出參數 (by output),只有輸出。
  • ref 傳址參數 (by reference),有輸入有輸出。
  • out 只需宣告變數,就算給值也會被忽略,所以也可以在調用out方法時,直接將變數定義在方法中。 宣告變數給調用的方法
    1
    2
    
    int result;
    Add(10, 20, out result);
    

    把要宣告的變數result,直接定義在調用的方法中。也就是說,在調用的方法中,引數直接加資料類型int

    1
    
    Add(10, 20, out int result);
    
  • ref 宣告變數,必須初始化給值。(必須要有初始值)
  • out 最大的用途就是,調用一次方法,就可以取得多個返回值。

out, ref 使用的時機:

像是碰到這樣的需求:
提供給某方法的引數,會希望輸出處理過的結果,回存到原本的變數上。

引數?! 參數?!

  • 引數:呼叫方法的時候
    1
    
    Add(10, ref int result); //10,result 叫「引數」
    
  • 參數:定義方法的時候
    1
    
    void Add (int x, ref int result) { ...} //x, result 叫「參數」
    

keywords/out-parameter-modifier
C#中in,out,ref,params的作用和区别
C# ref/out 關鍵字與傳遞參考型別參數

This post is licensed under CC BY 4.0 by the author.

聖嚴法師:以善來對治糾正不善

[C# 筆記] params