/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Swap two references. using System; class RefSwap { int a, b; public RefSwap(int i, int j) { a = i; b = j; } public void show() { Console.WriteLine("a: {0}, b: {1}", a, b); } // This method now changes its arguments. public void swap(ref RefSwap ob1, ref RefSwap ob2) { RefSwap t; t = ob1; ob1 = ob2; ob2 = t; } } public class RefSwapDemo { public static void Main() { RefSwap x = new RefSwap(1, 2); RefSwap y = new RefSwap(3, 4); Console.Write("x before call: "); x.show(); Console.Write("y before call: "); y.show(); Console.WriteLine(); // exchange the objects to which x and y refer x.swap(ref x, ref y); Console.Write("x after call: "); x.show(); Console.Write("y after call: "); y.show(); } }
Use two out parameters
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use two out parameters.
using System;
class Num {
/* Determine if x and v have a common denominator.
If so, return least and greatest common denominators in
the out parameters. */
public bool isComDenom(int x, int y,
out int least, out int greatest) {
int i;
int max = x < y ? x : y;
bool first = true;
least = 1;
greatest = 1;
// find least and treatest common denominators
for(i=2; i <= max/2 + 1; i++) {
if( ((y%i)==0) & ((x%i)==0) ) {
if(first) {
least = i;
first = false;
}
greatest = i;
}
}
if(least != 1) return true;
else return false;
}
}
public class DemoOut {
public static void Main() {
Num ob = new Num();
int lcd, gcd;
if(ob.isComDenom(231, 105, out lcd, out gcd)) {
Console.WriteLine("Lcd of 231 and 105 is " + lcd);
Console.WriteLine("Gcd of 231 and 105 is " + gcd);
}
else
Console.WriteLine("No common denominator for 35 and 49.");
if(ob.isComDenom(35, 51, out lcd, out gcd)) {
Console.WriteLine("Lcd of 35 and 51 " + lcd);
Console.WriteLine("Gcd of 35 and 51 is " + gcd);
}
else
Console.WriteLine("No common denominator for 35 and 51.");
}
}
[/csharp]
Use out
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use out. using System; class Decompose { /* Decompose a floating-point value into its integer and fractional parts. */ public int parts(double n, out double frac) { int whole; whole = (int) n; frac = n - whole; // pass fractional part back through frac return whole; // return integer portion } } public class UseOut { public static void Main() { Decompose ob = new Decompose(); int i; double f; i = ob.parts(10.125, out f); Console.WriteLine("Integer portion is " + i); Console.WriteLine("Fractional part is " + f); } }
Swap two values
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Swap two values. using System; class Swap { // This method now changes its arguments. public void swap(ref int a, ref int b) { int t; t = a; a = b; b = t; } } public class SwapDemo { public static void Main() { Swap ob = new Swap(); int x = 10, y = 20; Console.WriteLine("x and y before call: " + x + " " + y); ob.swap(ref x, ref y); Console.WriteLine("x and y after call: " + x + " " + y); } }
Use ref to pass a value type by reference
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use ref to pass a value type by reference. using System; class RefTest { /* This method changes its arguments. Notice the use of ref. */ public void sqr(ref int i) { i = i * i; } } public class RefDemo { public static void Main() { RefTest ob = new RefTest(); int a = 10; Console.WriteLine("a before call: " + a); ob.sqr(ref a); // notice the use of ref Console.WriteLine("a after call: " + a); } }
Objects are passed by reference
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Objects are passed by reference. using System; class Test { public int a, b; public Test(int i, int j) { a = i; b = j; } /* Pass an object. Now, ob.a and ob.b in object used in the call will be changed. */ public void change(Test ob) { ob.a = ob.a + ob.b; ob.b = -ob.b; } } public class CallByRef { public static void Main() { Test ob = new Test(15, 20); Console.WriteLine("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.change(ob); Console.WriteLine("ob.a and ob.b after call: " + ob.a + " " + ob.b); } }
Simple types are passed by value
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Simple types are passed by value. using System; class Test { /* This method causes no change to the arguments used in the call. */ public void noChange(int i, int j) { i = i + j; j = -j; } } public class CallByValue { public static void Main() { Test ob = new Test(); int a = 15, b = 20; Console.WriteLine("a and b before call: " + a + " " + b); ob.noChange(a, b); Console.WriteLine("a and b after call: " + a + " " + b); } }