最近有人問到 ref 關鍵字的正確用法,下面我們來舉例說明。其實要更好的理解 ref 關鍵字,結合 C++ 代碼更加容易一些。另外在開始我們的例子之前,需要提前說明幾點:
- C# 中的資料有兩種類型:參考型別(reference types)和實值型別(value types)。 簡單類型(包括int, long, double等)和結構(structs)都是實值型別,而其他的類都是參考型別。 簡單類型在傳值的時候會做複製操作,而參考型別只是傳遞引用,就像 C++ 中的指標一樣。
- 注意 structs 在 C# 和 C++ 中的區別。在 C++ 中, structs 和類基本相同(except that the default inheritance and default access are public rather than private)。而在 C# 中,structs 和類有很大的區別。其中最大的區別(我個人覺得,同時也是容易忽略的一個地方)可能就是它是實值型別,而不是參考型別。
下面這段代碼是 MSDN 中的例子:
// cs_ref.csusing System;public class MyClass { public static void TestRef(ref char i) { // The value of i will be changed in the calling method i = 'b'; } public static void TestNoRef(char i) { // The value of i will be unchanged in the calling method i = 'c'; } // This method passes a variable as a ref parameter; the value of the // variable is changed after control passes back to this method. // The same variable is passed as a value parameter; the value of the // variable is unchanged after control is passed back to this method. public static void Main() { char i = 'a'; // variable must be initialized TestRef(ref i); // the arg must be passed as ref Console.WriteLine(i); TestNoRef(i); Console.WriteLine(i); }}
大家很容易看出輸出結果是:
bb
那麼如果把這個例子做一些新的改動,將實值型別(這裡用的是 char)改成參考型別,程式運行又是什麼效果呢?
// ----------------------------------------// MyClass definitionpublic class MyClass{ public int Value;} // ----------------------------------------// Tester methodspublic static void TestRef(ref MyClass m){ m.Value = 10;} public static void TestNoRef(MyClass m){ m.Value = 20;} public static void TestCreateRef(ref MyClass m){ m = new MyClass(); m.Value = 100;} public static void TestCreateNoRef(MyClass m){ m = new MyClass(); m.Value = 200;} public static void Main(){ MyClass m = new MyClass(); m.Value = 1; TestRef(ref m); Console.WriteLine(m.Value); TestNoRef(m); Console.WriteLine(m.Value); TestCreateRef(ref m); Console.WriteLine(m.Value); TestCreateNoRef(m); Console.WriteLine(m.Value);}
大家能馬上給出正確的答案嗎?如果能,那看來你對 ref 的用法瞭解得還是非常不錯的。其實如果大家對 C++ 比較熟悉的話,把這段代碼換成 C++ 的就好理解的多了。
// ----------------------------------------// MyClass definition#pragma once class MyClass{public: int Value;}; typedef MyClass* MyClassPtr; // ----------------------------------------// Tester methodsvoid TestRef(char* i){ *i = 'b';} void TestNoRef(char i){ i = 'c';} void TestRef(MyClassPtr* m){ (*m)->Value = 10;} void TestNoRef(MyClassPtr m){ m->Value = 20;} void TestCreateRef(MyClassPtr* m){ delete (*m); *m = new MyClass(); (*m)->Value = 100;} void TestCreateNoRef(MyClassPtr m){ m = new MyClass(); m->Value = 200;} int main(int argc, char* argv[]){ char c = 'a'; TestRef(&c); printf("%c\n", c); // output: b TestNoRef(c); printf("%c\n", c); // output: b MyClassPtr m = new MyClass; m->Value = 1; TestRef(&m); printf("%d\n", m->Value); TestNoRef(m); printf("%d\n", m->Value); TestCreateRef(&m); printf("%d\n", m->Value); TestCreateNoRef(m); printf("%d\n", m->Value); delete m; return 0;}