R
raylopez99
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et
al. (3rd edition) (highly recommended--it's packed with information,
and is a desktop reference book) the following statement is made: (p.
39: "The ref modifier is essential in implementing a swap method")
This is untrue. The following program demonstrates it. See how
method "func4Swap" does a swap of two int variables without using ref,
but using a 'helper' class and using only pass by value
However, ref is important when using 'new' in the method calling the
variables to be used (not shown here), or, if you want to permanently
change the variables passed from inside the class doing the passing
(see func1 method and swap1 method below). In fact, I'm not sure you
can do a swap without using ref and without using a 'helper' class as
in the below.
RL
// OUTPUT AT END
// Pass by reference, Pass by Value demonstrated // October 15, 2008
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace console1
{
class Program
{
static void Main(string[] args)
{
////////////////////////////////////////////
// show pass by ref
PassByRef01 myPBRclas1 = new PassByRef01();
Console.WriteLine("I. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k,myPBRclas1.m);
myPBRclas1.func2(myPBRclas1.i, myPBRclas1.j);
Console.WriteLine("II. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
myPBRclas1.func1(ref myPBRclas1.k, ref myPBRclas1.m);
Console.WriteLine("III. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
PassByRef01 placeHolderPassByRef01cl = new PassByRef01();
placeHolderPassByRef01cl.func3(myPBRclas1);
Console.WriteLine("IV. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
placeHolderPassByRef01cl.func2(myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("V. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("VI. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("reset values");
myPBRclas1.k = 100;
myPBRclas1.m = 200;
Console.WriteLine("1. k,m's are: {0}, {1}", myPBRclas1.k,
myPBRclas1.m);
placeHolderPassByRef01cl.func4Swap(myPBRclas1);
Console.WriteLine("2. i,j's and k,m's are: {0}, {1}",myPBRclas1.k,
myPBRclas1.m);
Console.WriteLine("now use 'traditional' swap");
placeHolderPassByRef01cl.func4TraditionalSwap(ref myPBRclas1);
Console.WriteLine("3. i,j's and k,m's are: {0}, {1}",
myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("now use traditional swap from inside of class
(no helper class used)");
myPBRclas1.swap1(ref myPBRclas1.k, ref myPBRclas1.m);
Console.WriteLine("4. i,j's and k,m's are: {0}, {1}",
myPBRclas1.k, myPBRclas1.m);
}
}
}
/////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console1
{
class PassByRef01
{
public int k, m;
int _i, _j;
public PassByRef01()
{
_i = 10;
_j = 11;
k = 1;
m = 2;
}
public void swap1(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
public void func1(ref int a, ref int b)
{
a = 1111;
b = 2222;
Console.WriteLine("inside func1, k,m's are: {0}, {1}", k,
m);
}
public void func2(int a, int b)
{
a = 1110000; //since ref not used, this assignment never
made for k,m
b = 2220000;
Console.WriteLine("inside func2, k,m's are: {0}, {1}",
k,m);
}
public void func3(PassByRef01 x)
{
x.k = 66;
x.m = 67;
Console.WriteLine("inside func3, k,m's are: {0}, {1}", k,
m);
}
public void func4TraditionalSwap(ref PassByRef01 y)
{
int temp;
temp = y.k;
y.k = y.m;
y.m = temp;
}
public void func4Swap(PassByRef01 x)
{
int temp1, temp2;
temp1 = x.k;
temp2 = x.m;
x.m = temp1;
x.k = temp2;
Console.WriteLine("inside func3, k,m's are: {0}, {1} and
x.k, x.m are: {2}, {3}", k, m, x.k, x.m);
}
public int i //cannot pass by ref a property
{
get { return _i; }
set { _i = value; }
}
public int j
{
get { return _j; }
set { _j = value; }
}
}
}
/////////////////////// OUTPUT
I. i,j's and k,m's are: 10, 11, 1, 2
inside func2, k,m's are: 1, 2
II. i,j's and k,m's are: 10, 11, 1, 2
inside func1, k,m's are: 1111, 2222
III. i,j's and k,m's are: 10, 11, 1111, 2222
inside func3, k,m's are: 1, 2
IV. i,j's and k,m's are: 10, 11, 66, 67
inside func2, k,m's are: 1, 2
V. i,j's and k,m's are: 10, 11, 66, 67
VI. i,j's and k,m's are: 10, 11, 66, 67
reset values
1. k,m's are: 100, 200
inside func3, k,m's are: 1, 2 and x.k, x.m are: 200, 100
2. i,j's and k,m's are: 200, 100
now use 'traditional' swap
3. i,j's and k,m's are: 100, 200
now use traditional swap from inside of class (no helper class used)
4. i,j's and k,m's are: 200, 100
Press any key to continue . . .
al. (3rd edition) (highly recommended--it's packed with information,
and is a desktop reference book) the following statement is made: (p.
39: "The ref modifier is essential in implementing a swap method")
This is untrue. The following program demonstrates it. See how
method "func4Swap" does a swap of two int variables without using ref,
but using a 'helper' class and using only pass by value
However, ref is important when using 'new' in the method calling the
variables to be used (not shown here), or, if you want to permanently
change the variables passed from inside the class doing the passing
(see func1 method and swap1 method below). In fact, I'm not sure you
can do a swap without using ref and without using a 'helper' class as
in the below.
RL
// OUTPUT AT END
// Pass by reference, Pass by Value demonstrated // October 15, 2008
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace console1
{
class Program
{
static void Main(string[] args)
{
////////////////////////////////////////////
// show pass by ref
PassByRef01 myPBRclas1 = new PassByRef01();
Console.WriteLine("I. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k,myPBRclas1.m);
myPBRclas1.func2(myPBRclas1.i, myPBRclas1.j);
Console.WriteLine("II. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
myPBRclas1.func1(ref myPBRclas1.k, ref myPBRclas1.m);
Console.WriteLine("III. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
PassByRef01 placeHolderPassByRef01cl = new PassByRef01();
placeHolderPassByRef01cl.func3(myPBRclas1);
Console.WriteLine("IV. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
placeHolderPassByRef01cl.func2(myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("V. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("VI. i,j's and k,m's are: {0}, {1}, {2}, {3}",
myPBRclas1.i, myPBRclas1.j, myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("reset values");
myPBRclas1.k = 100;
myPBRclas1.m = 200;
Console.WriteLine("1. k,m's are: {0}, {1}", myPBRclas1.k,
myPBRclas1.m);
placeHolderPassByRef01cl.func4Swap(myPBRclas1);
Console.WriteLine("2. i,j's and k,m's are: {0}, {1}",myPBRclas1.k,
myPBRclas1.m);
Console.WriteLine("now use 'traditional' swap");
placeHolderPassByRef01cl.func4TraditionalSwap(ref myPBRclas1);
Console.WriteLine("3. i,j's and k,m's are: {0}, {1}",
myPBRclas1.k, myPBRclas1.m);
Console.WriteLine("now use traditional swap from inside of class
(no helper class used)");
myPBRclas1.swap1(ref myPBRclas1.k, ref myPBRclas1.m);
Console.WriteLine("4. i,j's and k,m's are: {0}, {1}",
myPBRclas1.k, myPBRclas1.m);
}
}
}
/////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console1
{
class PassByRef01
{
public int k, m;
int _i, _j;
public PassByRef01()
{
_i = 10;
_j = 11;
k = 1;
m = 2;
}
public void swap1(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
public void func1(ref int a, ref int b)
{
a = 1111;
b = 2222;
Console.WriteLine("inside func1, k,m's are: {0}, {1}", k,
m);
}
public void func2(int a, int b)
{
a = 1110000; //since ref not used, this assignment never
made for k,m
b = 2220000;
Console.WriteLine("inside func2, k,m's are: {0}, {1}",
k,m);
}
public void func3(PassByRef01 x)
{
x.k = 66;
x.m = 67;
Console.WriteLine("inside func3, k,m's are: {0}, {1}", k,
m);
}
public void func4TraditionalSwap(ref PassByRef01 y)
{
int temp;
temp = y.k;
y.k = y.m;
y.m = temp;
}
public void func4Swap(PassByRef01 x)
{
int temp1, temp2;
temp1 = x.k;
temp2 = x.m;
x.m = temp1;
x.k = temp2;
Console.WriteLine("inside func3, k,m's are: {0}, {1} and
x.k, x.m are: {2}, {3}", k, m, x.k, x.m);
}
public int i //cannot pass by ref a property
{
get { return _i; }
set { _i = value; }
}
public int j
{
get { return _j; }
set { _j = value; }
}
}
}
/////////////////////// OUTPUT
I. i,j's and k,m's are: 10, 11, 1, 2
inside func2, k,m's are: 1, 2
II. i,j's and k,m's are: 10, 11, 1, 2
inside func1, k,m's are: 1111, 2222
III. i,j's and k,m's are: 10, 11, 1111, 2222
inside func3, k,m's are: 1, 2
IV. i,j's and k,m's are: 10, 11, 66, 67
inside func2, k,m's are: 1, 2
V. i,j's and k,m's are: 10, 11, 66, 67
VI. i,j's and k,m's are: 10, 11, 66, 67
reset values
1. k,m's are: 100, 200
inside func3, k,m's are: 1, 2 and x.k, x.m are: 200, 100
2. i,j's and k,m's are: 200, 100
now use 'traditional' swap
3. i,j's and k,m's are: 100, 200
now use traditional swap from inside of class (no helper class used)
4. i,j's and k,m's are: 200, 100
Press any key to continue . . .