472,103 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Using the 'ref' keyword.

Hello,

Something is quite puzzled to me in using the 'ref' keyword, probably
due to some lack of basic understanding.
Whenever I wanna change the parameter passed to a function I will use
the 'ref' keyword, that's fine, and it even makes sense when sending a
Value Type parameter (int, float or a struct, for example). However, if
I'm sending a class object to the function, and this object is a
Reference Type, of course, isn't it being sent Byref as a default
behavior because it's a Reference value??? When sending a Reference
Type to a function is it being sent Byval and therefore I have to use
the 'ref' keyword?

What am I missing here?

Thanks,

Elad

Nov 17 '05 #1
4 6306
Elad,

A reference type and sending a parameter by reference are two different
things. If you send a reference type to a method (passed by value), then
the reference is passed by value. Since you are sending a reference, you
can still access the same object outside of the method using another
variable set to the same reference.

Now, if you pass that reference by ref, you can modify the value of that
parameter so that it holds a new reference to a new object. When you pass
it by value, you can't do that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Elad Gutman" <el********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello,

Something is quite puzzled to me in using the 'ref' keyword, probably
due to some lack of basic understanding.
Whenever I wanna change the parameter passed to a function I will use
the 'ref' keyword, that's fine, and it even makes sense when sending a
Value Type parameter (int, float or a struct, for example). However, if
I'm sending a class object to the function, and this object is a
Reference Type, of course, isn't it being sent Byref as a default
behavior because it's a Reference value??? When sending a Reference
Type to a function is it being sent Byval and therefore I have to use
the 'ref' keyword?

What am I missing here?

Thanks,

Elad

Nov 17 '05 #2
Elad Gutman <el********@hotmail.com> wrote:
Something is quite puzzled to me in using the 'ref' keyword, probably
due to some lack of basic understanding.


See <http://www.pobox.com/~skeet/csharp/parameters.html> for an explanation
of this.
Nov 17 '05 #3
Elad Gutman <el********@hotmail.com> wrote:
Something is quite puzzled to me in using the 'ref' keyword, probably
due to some lack of basic understanding.
Whenever I wanna change the parameter passed to a function I will use
the 'ref' keyword, that's fine, and it even makes sense when sending a
Value Type parameter (int, float or a struct, for example). However, if
I'm sending a class object to the function, and this object is a
Reference Type, of course, isn't it being sent Byref as a default
behavior because it's a Reference value??? When sending a Reference
Type to a function is it being sent Byval and therefore I have to use
the 'ref' keyword?


The reference is sent by value by default - you can change the object
itself, but not *which* object is referred to.

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Thank you all for the quick respond. The reference( . . . -:) to the
article is just great!

Elad

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Ori | last post: by
17 posts views Thread by LP | last post: by
1 post views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
6 posts views Thread by semkaa | last post: by
3 posts views Thread by not_a_commie | last post: by
65 posts views Thread by Arjen | last post: by
reply views Thread by leo001 | last post: by

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.