Use the ref keyword when you want to add another level of indirection.
You
can achieve the same effect by packing an object into a wrapper class.
5) Values and References to Objects are Passed By Value
By default, objects in C# are not passed by reference. (Unlike Java, C#
does
support passing by reference using the ref keyword.) In C#, you pass a
reference or a value to a method. You cannot pass an object to a method.
By
default, all calls to methods are by value. Now is that clear! In other
words,
you pass a reference to an object to a method, not the object itself.
The
reference is passed by value so that the a copy of the reference goes on
the
stack. The key here is that the object is not copied onto the stack and
you can
touch the object while inside the method. If you want to truly pass an
object
by reference (for a swap routine) use the ref keyword. Remember, you
cannot
pass an object, so you are actually passing a reference by reference.
Oh, I
have a headache.
Note: This topic has been a source of great confusion to C++ coders, so
I will
elaborate. If you want to write a swap routine then you need to add
another
degree of indirection to the method call. You can do this by use the
keyword
ref. Here is some sample code that demonstrates the concept. The swap
routine only works if you pass a reference to a concrete Drawable object
by
reference.
using System;
namespace TestSwap
{
abstract class Drawable
{
abstract public void DrawMe();
}
class Circle : Drawable
{
public override void DrawMe()
{
System.Console. WriteLine("Circ le");
}
}
class Square : Drawable
{
public override void DrawMe()
{
System.Console. WriteLine("Squa re");
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static public void SwapByValue(Dra wable d1, Drawable d2)
{
Drawable temp= d1;
d1 = d2;
d2= temp;
}
static public void SwapByRef(ref Drawable d1, ref
Drawable d2)
{
Drawable temp= d1;
d1 = d2;
d2= temp;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Drawable wasCircle= new Circle();
Drawable wasSquare= new Square();
wasCircle.DrawM e(); // outputs Circle
wasSquare.DrawM e(); // outputs Square
SwapByValue(was Circle, wasSquare); // fails
wasCircle.DrawM e(); // outputs Circle
wasSquare.DrawM e(); // outputs Square
SwapByRef(ref wasCircle, ref wasSquare); //
succeeds
wasCircle.DrawM e(); // outputs Square
wasSquare.DrawM e(); // outputs Circle
System.Console. ReadLine();
}
}
}
You can mimic pass by reference in Java by "packing" the concrete
Drawable
object into a wrapper class. You can then pass a reference to the
packing
object to a swap routine by value. This added level of indirection
allows you
to implement a swap routine, so that you can swap the concrete Drawable
object in each packing object.
Regards,
Jeff
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!