Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 126: ICloneable

Section 126.1: Implementing ICloneable in a class

Implement ICloneable in a class with a twist. Expose a public type safe Clone() and implement object Clone() privately.

public class Person : ICloneable

{

// Contents of class

public string Name { get; set; } public int Age { get; set; }

// Constructor

public Person(string name, int age)

{

this.Name=name; this.Age=age;

}

// Copy Constructor

public Person(Person other)

{

this.Name=other.Name; this.Age=other.Age;

}

#region ICloneable Members

// Type safe Clone

public Person Clone() { return new Person(this); }

// ICloneable implementation object ICloneable.Clone()

{

return Clone();

}

#endregion

}

Later to be used as follows:

{

Person bob=new Person("Bob", 25); Person bob_clone=bob.Clone(); Debug.Assert(bob_clone.Name==bob.Name);

bob.Age=56; Debug.Assert(bob.Age!=bob.Age);

}

Notice that changing the age of bob does not change the age of bob_clone. This is because the design uses cloning instead of assigning of (reference) variables.

Section 126.2: Implementing ICloneable in a struct

The implementation of ICloneable for a struct is not generally needed because structs do a memberwise copy with the assignment operator =. But the design might require the implementation of another interface that inherits from

ICloneable.

Another reason would be if the struct contains a reference type (or an array) which would need copying also.

GoalKicker.com – C# Notes for Professionals

640

// Structs are recommended to be immutable objects

[ImmutableObject(true)]

public struct Person : ICloneable

{

// Contents of class

public string Name { get; private set; } public int Age { get; private set; }

// Constructor

public Person(string name, int age)

{

this.Name=name; this.Age=age;

}

// Copy Constructor

public Person(Person other)

{

// The assignment operator copies all members this=other;

}

#region ICloneable Members

// Type safe Clone

public Person Clone() { return new Person(this); }

// ICloneable implementation object ICloneable.Clone()

{

return Clone();

}

#endregion

}

Later to be used as follows:

static void Main(string[] args)

{

Person bob=new Person("Bob", 25); Person bob_clone=bob.Clone(); Debug.Assert(bob_clone.Name==bob.Name);

}

GoalKicker.com – C# Notes for Professionals

641