M
Marcel Hug
Hi NG!
In my book I have the following code simple and I tested it:
public class Base : ICloneable
{
public int Age;
public string Name;
public Base(string myname)
{
Name = myname;
}
public Object Clone()
{
return MemberwiseClone();
}
public static void Main(String[] argv)
{
Base myTest = new Base("Joanna");
myTest.Age = 36;
Base clone1 = (Base) myTest.Clone();
Base clone2 = myTest;
bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns true
myBTest = Object.ReferenceEquals(myTest.Age, clone1.Age);
//Book says true
//My Test returns false
myTest.Name = "Julie";
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns false
myBTest = Object.ReferenceEquals(myTest.Name, clone2.Name);
//Book saya true
//my Test returns true
}
}
So I'm a little confused!
With the method MemberwiseClone I do a Shallow copy. That means all
value type would be copied and of the reference types just the pointer
in the variable. Or do i understand this wrong ?
Case 1:
A string is a reference type so it is clear for me, why it results as
true. The pointers points to the same string. Or not ?
Case 2:
Why does this return false ? I guess because a boxing of the to integers
is needed ?
Case 3:
Does this return wrong, because "Julie" could be written as new
String("Julie"); ?
Case 4:
It is clear, because they are two variable which points to the same
instance of the class Base.
Thanks for help!
Regards
Marcel
In my book I have the following code simple and I tested it:
public class Base : ICloneable
{
public int Age;
public string Name;
public Base(string myname)
{
Name = myname;
}
public Object Clone()
{
return MemberwiseClone();
}
public static void Main(String[] argv)
{
Base myTest = new Base("Joanna");
myTest.Age = 36;
Base clone1 = (Base) myTest.Clone();
Base clone2 = myTest;
bool myBTest = false;
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns true
myBTest = Object.ReferenceEquals(myTest.Age, clone1.Age);
//Book says true
//My Test returns false
myTest.Name = "Julie";
myBTest = Object.ReferenceEquals(myTest.Name, clone1.Name);
//Book says false
//My Test returns false
myBTest = Object.ReferenceEquals(myTest.Name, clone2.Name);
//Book saya true
//my Test returns true
}
}
So I'm a little confused!
With the method MemberwiseClone I do a Shallow copy. That means all
value type would be copied and of the reference types just the pointer
in the variable. Or do i understand this wrong ?
Case 1:
A string is a reference type so it is clear for me, why it results as
true. The pointers points to the same string. Or not ?
Case 2:
Why does this return false ? I guess because a boxing of the to integers
is needed ?
Case 3:
Does this return wrong, because "Julie" could be written as new
String("Julie"); ?
Case 4:
It is clear, because they are two variable which points to the same
instance of the class Base.
Thanks for help!
Regards
Marcel