最近有人問到 ref 關鍵字的正確用法
C# 中的數據有兩種類型
注意 structs 在 C# 和 C++ 中的區別
下面這段代碼是 MSDN 中的例子
// cs_ref
using System;
public class MyClass
{
public static void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i =
}
public static void TestNoRef(char i)
{
// The value of i will be unchanged in the calling method
i =
}
// 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
{
char i =
TestRef(ref i); // the arg must be passed as ref
Console
TestNoRef(i);
Console
}
}
大家很容易看出輸出結果是
b
b
那麼如果把這個例子做一些新的改動
[
From:http://tw.wingwit.com/Article/program/net/201311/15037.html