passing a ref to a method

K

Ken

Objects are reference types.

When i pass an object as a parameter to a method, in Java
this automatically means I am passing a reference of that
Object, not a copy of it.

In C#, this means the same, except there is the 'ref'
keyword which explicitly tells the C# compiler to act on
the actual reference passed as a parameter.

Therefore I really do not see a situation where the
compiler does not know how is the parameter to be treated.

How can therefore tha ref keyword be useful to the
progeammer?

Thanks,

Ken
 
D

Daniel O'Connell

Ken said:
Objects are reference types.

When i pass an object as a parameter to a method, in Java
this automatically means I am passing a reference of that
Object, not a copy of it.
Yes.
In C#, this means the same, except there is the 'ref'
keyword which explicitly tells the C# compiler to act on
the actual reference passed as a parameter.

Not quite, ref tells the compiler to pass the object reference by reference.
By default, object references are passed by value.
Therefore I really do not see a situation where the
compiler does not know how is the parameter to be treated.

How can therefore tha ref keyword be useful to the
progeammer?
The end result is that when ref is used, the method can cahnge the value of
the REFERENCE that the caller sees, instead of simply the object, which
allows you to change the object that reference points to. Try this sample
code.
using System;
public class Class1
{

public static void Main(string[] args)
{
string myString = "Main String";
Console.WriteLine("Before ValTestMethod: {0}",myString);
ValTestMethod(myString);
Console.WriteLine("After ValTestMethod: {0}",myString);
RefTestMethod(ref myString);
Console.WriteLine("After RefTestMethod: {0}",myString);
}

static void ValTestMethod(string test)
{
test = "ValTestMethod String";
}

static void RefTestMethod(ref string test)
{
test = "RefTestMethod String";
}
}
the result should be
Before ValTestMethod: Main String
After ValTestMethod: Main String
After RefTestMethod: RefTestMethod String
 
K

Ken

Thank you, quite a useful (needed) feature indeed.

Regards,

Ken



-----Original Message-----

Objects are reference types.

When i pass an object as a parameter to a method, in Java
this automatically means I am passing a reference of that
Object, not a copy of it.
Yes.
In C#, this means the same, except there is the 'ref'
keyword which explicitly tells the C# compiler to act on
the actual reference passed as a parameter.

Not quite, ref tells the compiler to pass the object reference by reference.
By default, object references are passed by value.
Therefore I really do not see a situation where the
compiler does not know how is the parameter to be treated.

How can therefore tha ref keyword be useful to the
progeammer?
The end result is that when ref is used, the method can cahnge the value of
the REFERENCE that the caller sees, instead of simply the object, which
allows you to change the object that reference points to. Try this sample
code.
using System;
public class Class1
{

public static void Main(string[] args)
{
string myString = "Main String";
Console.WriteLine("Before ValTestMethod: {0}",myString);
ValTestMethod(myString);
Console.WriteLine("After ValTestMethod: {0}",myString);
RefTestMethod(ref myString);
Console.WriteLine("After RefTestMethod: {0}",myString);
}

static void ValTestMethod(string test)
{
test = "ValTestMethod String";
}

static void RefTestMethod(ref string test)
{
test = "RefTestMethod String";
}
}
the result should be
Before ValTestMethod: Main String
After ValTestMethod: Main String
After RefTestMethod: RefTestMethod String
Thanks,

Ken


.
 
J

Jon Skeet [C# MVP]

Ken said:
Objects are reference types.

When i pass an object as a parameter to a method, in Java
this automatically means I am passing a reference of that
Object, not a copy of it.
Yes.

In C#, this means the same, except there is the 'ref'
keyword which explicitly tells the C# compiler to act on
the actual reference passed as a parameter.

No, it tells C#/the CLR that the parameter is passed *by* reference.
This is subtly different.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top