2007年8月9日 星期四

Call by reference ( ref )

Example

// cs_ref.cs

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);

TestNoRef(i);

Console.WriteLine(i);

}

}




Output

b

b

 

沒有留言:

張貼留言