ob.a ob.b : 15 20 ob.a ob.b : 35 -20
, Change () , .
, . , , , . - , . , .
ref out
, , int char, . , , , , , . , ref out . , .
ref out, , . , : . .
, , . Swap (), . , , C# , , , int. ref.
, return. . , , , ? , out.
ref
ref , . , . . Sqr (), . ref.
// ref .
Using System;
class RefTest {
// . // ref. public void Sqr(ref int i) {
i = i * i;
}
}
class RefDemo {
|
|
static void Main() {
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine(" : " + a); ob.Sqr(ref a); // ref Console.WriteLine(" : " + );
}
}
, ref . , , Sqr ().
: 10 : 100
, ref, , . , Swap () , .
// .
Using System;
class ValueSwap {
// , public void Swap(ref int a, ref int b) { int t; t = a; a = b; b = t;
}
}
class ValueSwapDemo { static void Main() {
ValueSwap ob = new ValueSwap();
int x=10, = 20;
Console.WriteLine("x : " + + " " + ); ob.Swap (ref , ref );
Console.WriteLine(" : " + + " " + );
}
}