By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Tee" <th*@streamyx.com> a crit dans le message de news:
#w**************@TK2MSFTNGP10.phx.gbl...
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

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #2

P: n/a
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
Nov 16 '05 #3

P: n/a
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.CurrentCultu re;

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

if(result)
MessageBox.Show("We got a valid number: " + d); // 12
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

P: n/a
You also might want to read Jon Skeet's article on parameter passing:

http://www.yoda.arachsys.com/csharp/parameters.html

Nov 16 '05 #5

P: n/a
Tee
Thanks for everyone who replied!
Now I understand it better. Thank you.

Tee.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
You also might want to read Jon Skeet's article on parameter passing:

http://www.yoda.arachsys.com/csharp/parameters.html

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.