Sign in to follow this  
Followers 0
ricekrispyw

Copying objects in C#

4 posts in this topic

Hey. Being a noob to C# (though not to programming), I need some help making a copy of an object. I have a new object, and I have a reference to an object. I'm looking to make a by-value (or memberwise, or whatever) copy of this object. Setting objA = objB, as we all know, just makes objA refer to the same object as objB, but I want to copy the object that objB is refering to and make objA refer to that object. I hope I've been clear. Thanks in advance.
0

Share this post


Link to post
Share on other sites
There's a couple of ways.

1.) If this is in the object's class definition, override the Clone() implementation to use the protected MemberwiseClone() method.

public class PlayerInformation : ICloneable
{
// ...

object ICloneable.Clone()
{
// make memberwise copy
return this.MemberwiseClone();
}
}


2.) If it's not or if you don't have control over the class definition, you'll have to use Clone(). If you don't know whether it's ICloneable or not, you'll have to test this first:

T t1 = new T();

// Make sure this can be cloned.
ICloneable original = t1 as ICloneable;

T copy = null;

if (original != null)
{
copy = original.Clone();
}

return copy;


If it's safe for you to assume that it's ICloneable, then you can just call Clone(), using:

T t1 = new T();
T t2 = t1.Clone();


or the more rigorous form,

T t1 = new T();
T t2 = ((ICloneable)t1).Clone();


Clone() returns a copy of the object but makes no guarantees about whether or not it's a deep copy or a shallow copy.
0

Share this post


Link to post
Share on other sites
Your real only solution is to either create a copy constructor or implementing a clone or copy method. I'm guessing the reasoning for not implementing copy constructors automatically is to limit boxing/unboxing, so just be sure that you actually need to make new objects.

If you don't have access to the class's definition (or interface definition if it has one) the above poster's solution is ideal. Otherwise I would make a custom copy constructor.
0

Share this post


Link to post
Share on other sites
OK. Thanks. I'll take a look at Clone(). I don't have control over the class definintion. (The class in question is actually KeyboardState from DirectInput.) Thanks again.

[Edited by - ricekrispyw on October 5, 2005 4:38:39 PM]
0

Share this post


Link to post
Share on other sites
OK, the class (it wasn't Key, it was KeyboardState...) isn't IClonable. Is there no other way?

And if it's not, how do I convert an enum to an int? For example:
public enum Example
{
val1;
val2;
}


If I have an instance of Example that's, say, equal to val1, how do I convert that to an int?

Thanks a bunch and thanks for putting up with my C# newbishness.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0