when to use "ref" and when not? please help!

  • Thread starter Thread starter Tee
  • Start date Start date
T

Tee

Hi guys,

I am confused about the usage of "ref".

Please see this code:

private void button3_Click(object sender, System.EventArgs e)

{

DataTable dt = new DataTable();

AddColumn(dt);

Console.WriteLine(dt.Namespace);

RefAddColumn(ref dt);

Console.WriteLine(dt.Namespace);

}

private void AddColumn(DataTable table)

{

table.Namespace = "NoRef";

}

private void RefAddColumn(ref DataTable table)

{

table.Namespace = "Ref";

}



In this case, ref and without ref have no difference. The table got the
namespace changed.



But in this piece of code,

private void button4_Click(object sender, System.EventArgs e)

{

char i = 'a';

TestRef(ref i);

Console.WriteLine(i);

TestNoRef(i);

Console.WriteLine(i);

}



private void TestRef(ref char i)

{

i = 'b';

}

private void TestNoRef(char i)

{

i = 'c';

}



only the one with "ref" works for this "char i"...

why ref & without-ref work for DataTable but not char i?? Can anyone tell
me?



Thanks,

Tee
 
why ref & without-ref work for DataTable but not char i?? Can anyone tell
me?

Because you only changed a member of DataTable, not the DataTable object
itself, whereas you changed the object being pointed to for char i.

Joanna
 
Tee,

Because that one of the big differences between a value and a reference to
an object.

A value passes his value in the value
An object passes his reference in the value
A ref boxes the reference in both situation in the passed value

I hope this helps?

Cor
 
Hi Tee,

ref is handy when the reference might get changed in a method and you want
to be able to use the new object, and it is handy in methods where you
want multiple objects returned instead of just one.

Consider this code

ArrayList list = new ArrayList();
list.Add("Hello");
list.Add("World");

DoSomething(list);

int n = list.Count; // n = 3 because "Tee" is now added

DoSomethingElse(list)

int o = list.Count; // n = 3 because list still references the original
ArrayList

DoSomethingAgain(ref list)

int p = list.Count; // n = 0 because list now references the new ArrayList

....

void DoSomething(ArrayList list)
{
list.Add("Tee");
}

void DoSomethingElse(ArrayList list)
{
list = new ArrayList();
}

void DoSomethingAgain(ref ArrayList list)
{
list = new ArrayList();
}

....

When we use this method for parsing a string containing a double the
method returns true or false. If the method returns true, the string
contained a valid double but to be able to get the double we pass it an
empty double.

'out' is the same as 'ref' but you do not need to reference a valid object
before you call a method, but you do need to set the reference to
something before you return (with ref you do not need to set the reference
inside the method).

public static bool TryParse(string s, NumberStyles style, IFormatProvider
provider, out double result);

....

double d;
CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;

bool result = Double.TryParse("12", NumberStyles.Any, ci.NumberFormat, out
d);

if(result)
MessageBox.Show("We got a valid number: " + d); // 12
 
Back
Top