C# new object but instead of copying an object, but referencing?
I have a class say ClassA with a string array inside class.
In my code I have ClassA as an object, then I want to create another new ClassA object, but it should copy the original object to the new class and do whatever it is supposed to do.
Bu开发者_如何学运维t strangely, when I declare that new object, whatever changed in the new object affects the original object.
Is there any reason why I am getting such behaviour?
It sounds like you're just doing this to copy it:
ClassA obj2 = obj1;
In this case then changes to obj2
would indeed be reflected in obj1
because the objects that you're using are just pointers to the same location in the memory heap. You're not actually copying it, you're just making another reference to it.
Take a look at the ICloneable
interface here. You'd want something like this:
public class ClassA : ICloneable
{
public string myString { get; set; }
public object Clone()
{
var obj = new ClassA();
obj.myString = myString;
return myObj;
}
}
Then you'd call it like this:
ClassA obj2 = (ClassA)obj1.Clone();
Keep in mind that this isn't very type-safe, however. That cast is a bit messy (and, honestly, I haven't tried it, so it might even be problematic). I don't think .NET has introduced a generic ICloneable
yet. But it shouldn't be too hard to write one. Something like:
public interface ICloneable<T>
{
public T Clone();
}
public class ClassA : ICloneable<ClassA>
{
public string myString { get; set; }
public ClassA Clone()
{
var obj = new ClassA();
obj.myString = myString;
return myObj;
}
}
This should be callable like this:
ClassA obj2 = obj1.Clone<ClassA>();
or possibly even (at least with a little tweaking):
ClassA obj2 = obj1.Clone();
An additional benefit to making a generic interface like this is that any given class can be "cloneable" to other types, not just itself. You can implement as many ICloneable<T>
interfaces as you want on a single class. So you could have something like:
SomeOtherClass obj3 = obj1.Clone<SomeOtherClass>();
This is all off the top of my head and I don't have a compiler handy to test it, but you get the idea.
I'm sure it is because you are just setting another reference to the one string array in the new object. If you want a separate string array you need to create a constructor that creates a new string array and copies the strings over.
You can make a constructor overload that takes a new ClassA object and copies it's parameters.
public class ClassA
{
public String SomeParam { get; set; }
public ClassA(ClassA myoldobject)
{
//Logic for initializing new object.
this.SomeParam = myoldobject.SomeParam;
}
public ClassA(String someparam)
{
this.SomeParam = someparam;
}
}
This enables you to say
ClassA one = new ClassA("Test");
ClassA two = new ClassA(one);
This is because class - is a reference type and you are trying to get the behaviour of a value type. See deeper explanation here. Also it is greatly explained in CLR via C# book by J. Richter.
If you want to copy a reference type, you might need to implement an IClonable interface and call Clone for that.
It seems that you've assigned the first object to the second ..
To copy an existed object you have to assign all of its properties values to the new object, not to assign the whole object because this way you create another reference to the same object.
To create a copy of the first instance of ClassA
, try the following:
ClassA = secondObject = new ClassA();
secondObject.Property1 = firstObject.Property1;
secondObject.Property2 = firstObject.Property2;
............
精彩评论