469,921 Members | 2,183 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,921 developers. It's quick & easy.

Send refrence types by value to a method

A.M
Hi,

Ho can I send a refrence type by value as a method parameter.

Thanks,
Alan
Nov 17 '05 #1
6 1309
A.M <no******@online.nospam> wrote:
Ho can I send a refrence type by value as a method parameter.


Well, the value of a reference type expression is a reference, and that
is passed by value by default.

You can't pass an actual *object* either by value or by reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Just by passing the reference type as parameter to a method without using
the ref or out keyword. You create a copy of the reference pointer on the
stack that points to the object on the heap..

Gabriel Lozano-Morán
Software Engineer
Sogeti

"A.M" <no******@online.nospam> wrote in message
news:u1*************@TK2MSFTNGP12.phx.gbl...
Hi,

Ho can I send a refrence type by value as a method parameter.

Thanks,
Alan

Nov 17 '05 #3
To add to Jon's and Gabriel's comments, if you want to pass a copy of
the object, it must be an object that implements ICloneable, and you
would clone it:

myObject.MyMethod(argumentObject.Clone());

Nov 17 '05 #4
Take in mind that this cloning is just a shallow copy

Gabriel Lozano-Morán
Software Engineer
Sogeti

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
To add to Jon's and Gabriel's comments, if you want to pass a copy of
the object, it must be an object that implements ICloneable, and you
would clone it:

myObject.MyMethod(argumentObject.Clone());

Nov 17 '05 #5
Yes, usually. Of course, it depends upon how the class designer
implemented ICloneable, which never does specifically say whether a
clone should be a shallow copy or a deep copy.

Regardless, yes, a clone usually copies only the fields that are part
of the object being cloned, and does not copy objects to which the
cloned object holds references. So, in essence, by cloning you get:

1. A new object.
2. New copies of any value types (ints, doubles, DateTimes, structs)
that the object holds.
3. References to the same reference type objects to which the original
object held references.

Again, this depends upon how the class designer implemented ICloneable.

Of course, if the class doesn't implement ICloneable, then you can't
copy it using Clone(), and you only option is to create a new one using
"new" and whatever fields you can get at. Some classes (for example
System.Drawing.Font) work this way.

Nov 17 '05 #6
OK... so System.Drawing.Font implements ICloneable. D-Oh! Should have
checked first.

Anyway, there _are_ some classes that don't implement ICloneable, and
in those cases your only option is to use "new" and construct a new
object passing the properties of the object you want to copy.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by malborg | last post: by
1 post views Thread by Rafael Veronezi | last post: by
7 posts views Thread by stephan querengaesser | last post: by
161 posts views Thread by KraftDiner | last post: by
2 posts views Thread by Luis Arvayo | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.