1.out参数的用法。
2.ref参数的基本用法,相当于c里面的指针。
3.有ref与无ref的比较。
4.此处应用了类,注意类是引用类型,直接进行引用,除非在函数内生成新的类。
5.ref,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]);
}
}
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]);
}
}
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);
}
}
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);
}
}
// ----------------------------------------
// 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);
}
}
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);
}
}