"Rain" <Ra**@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
Hi. Im a C# newbie, just want to ask how to implement the
ICloneable.Clone..
Dont know how it works, would really appreciate it if someone could show a
simple source sample of how to do this.. Thank you so much!
public class X : ICloneable
{
// a represents here whatever the internal state of the object is
private int a;
// you may want a private ctor specifically for this purpose
public X(int p) { a = p;}
// allows X x2 = x1.Clone();
// visible in intellisense
public X Clone() { return new X(a); }
// allows X x2 = (X)((ICloneable)x1).Clone();
// NOT visible in intellisense for an object of compile time type X
object ICloneable.Clone()
{
return Clone();
}
}
This is an explicit implementation of the interface with a typesafe method
as well - I recommend this approach be used always as it is much more user
friendly than just just implementing the interface "normally":
public class X : ICloneable
{
private int a;
public X(int p) { a = p;}
// allows X x2 = (X)x1.Clone();
// visible in intellisense
public object Clone() { return new X(a); } }