473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
5 6685
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dmitry | last post by:
Hi, I have defined interface for COM components which inludes an argument being filled with additional error info, if such occurs. If inside I raise COM Error, I populate that parameter. In COM...
22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
1
by: Kris Takacs | last post by:
Hello, I recently upgraded a project from Visual Studio 6 to .NET and get the following error message when I call help: "The window name "REF" was not defined in your project file." If I...
5
by: Cmtk Software | last post by:
The following code: public __gc class MyClass { public: void MyFunc (int __gc* number); }; Generates the following metadata for C# when compiled in VC 2005 with the /clr:oldsyntax switch...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.