1.out參數的用法。
using System;
class TestOut
...{
static public void FillArray(out int[] myArray)
...{
// 初始化數組(必須):
myArray = new int[5]...{1, 2, 3, 4, 5};
}
static public void Main()
...{
int[] myArray; // 初始化數組(不是必須的!)
// 傳遞數組給(使用out方式的)調用方:
FillArray(out myArray);
// 顯示數組元素
Console.WriteLine("數組元素是:");
for (int i=0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
2.ref參數的基本用法,相當於c裡面的指針。
using System;
class TestRef
...{
public static void FillArray(ref int[] arr)
...{
// 根據需要創建一新的數組(不是必須的)
if (arr == null)
arr = new int[10];
// 否則填充數組,就可以了
arr[0] = 123;
arr[4] = 1024;
}
static public void Main ()
...{
//初始化數組:
int[] myArray = ...{1,2,3,4,5};
// 使用ref傳遞數組:
FillArray(ref myArray);
//顯示更新後的數組元素:
Console.WriteLine("數組元素是:");
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
3.有ref與無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 = ''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);
i = ''a'';
TestNoRef(i);
Console.WriteLine(i);
}
}
4.此處應用了類,注意類是引用類型,直接進行引用,除非在函數內生成新的類。
using System;
// ----------------------------------------
// MyClass definition
public class MyClass
...{
public int Value;
// ----------------------------------------
// Tester methods
public 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);
}
}
5.ref,out,與無參參數的比較。
using System;
class TestApp
...{
static void outTest(out int x, out int y)
...{//離開這個函數前,必須對x和y賦值,否則會報錯。
//y = x;
//上面這行會報錯,因為使用了out後,x和y都清空了,需要重新賦值,即使調用函數前賦過值也不行
x = 1;
y = 2;
}
static void refTest(ref int x, ref int y)
...{
x = 1;
y = x;
}
static void noRefTest(int x, int y)
...{
x = 8;
y = x;
}
public static void Main()
...{
//out test
int a, b;
//out使用前,變量可以不賦值
outTest(out a, out b);
Console.WriteLine("a={0};b={1}", a, b);
int c = 11, d = 22;
outTest(out c, out d);
Console.WriteLine("c={0};d={1}", c, d);
//ref test
// int m, n;
//refTest(ref m, ref n);
//上面這行會出錯,ref使用前,變量必須賦值
int o = 11, p = 22;
refTest(ref o, ref p);
Console.WriteLine("o={0};p={1}", o, p);
int li = 99, zong = 89;
noRefTest(li, zong);
Console.WriteLine("{0},{1}", li, zong);
}
}