Implementing Interfaces: ICloneable and IComparable

by Ira 4. March 2008 17:56

A friend of mine was showing me about some common interfaces and some good use cases for them the other day. A discussion arose of how to accurately create a deep copy of an object and the best way to go about it. Shortly after that discussion we got into the Sort() method and how to dictate how that method will sort your objects. This is when he spilled into telling me all about these common interfaces. So I decided to try to put together a good sample of these and the best way I could gather to implement them.

 

In this example we will create a Zoo object. Each Zoo object will contain a list of the Animal object. The Animal object will then be sub-classed into a Penguin and a Bear object. The overall goal will be to implement ICloneable class so that we can create either a deep or shallow copy of our Zoo object. Then we will implement IComparable on our Animal object so when we call the Sort() method on a list of Animal, it will be sorted by the class type name of the subclass and then by the name property of the Animal.

 

So first we will implement ICloneable on the Zoo class. Once setting it's inheritance, Visual Studio will show a smart tag when we mouse over the interface and give us the choice of implementing the interface or explicitly implementing the interface.


In this case, we are going to explicitly implement the ICloneable interface on the Zoo class. This is so that the ICloneable method implementation will be private to the Zoo object. The ICloneable.Clone() method returns a type of object, so we are going to box our Zoo object down to clone it and then return an unboxed, strongly typed Zoo object. By default we will return a deep copy of the Zoo object, but we will create an overload that will return a shallow copy just in case it is needed.


Before this can work, we will also have to implemented ICloneable on the Animal object.


Now we have Animal.Clone() and Zoo.Clone(). In the Zoo.Clone() method, to get a deep copy of all our objects we will create a new list of Animal and then clone each instance within our existing list of Animal and then add it to our new list. After that we can set that list of Animal to be the list contained within our new clone of Zoo. For a shallow copy of a Zoo object, we simply return a strongly typed MemberwiseClone.

 

Next we want to be able to sort our list of Animal by type name of its subclass and then by the name property of the Animal. To do this we will put a comma after the ICloneable inheritance call of the Animal class and then put IComparable after that. Now we will use our Visual Studio tab on mouseover of IComparable to implement this interface. Our implementation will look like this:


Next we will put a method in the Zoo object to sort our list of Animal.


Once put into a console program after creating a Zoo object, calling sort, creating a deep copy of it, using the deep copy to append the word "Copy" to each Animal's name property and the outputting the results, we get this:


That is all there is to it! The ability to create deep copies of objects and sort objects under your own terms is integral in programming. If you haven't used these before (like myself before a few days ago) keep these in mind when facing copy and sort problems in your programming.

 

Download the code: 

SampleInterfaces.zip (33.51 kb)

 

kick it on DotNetKicks.com

Tags: , , ,

C#

Comments

3/5/2008 7:05:20 PM #

Luke Foust

Another great interface is IEquatable<T> which helps with comparing if two objects are equal.

Luke Foust United States

3/6/2008 4:27:00 AM #

Heiko Hatzfeld

Hello...

There is another option to make deep copies of your objects. You can declare your class as [serializable]. This will enforce it by the compiler. Just serialize the it and then deserialize it into a new variable and you are done. But be aware that there are some stubble differences in the different serializers (XML wont serialize private fields, binary will)

And when mentioning IEquatable<T>, you cant forget IComparable<T>... But this interface is not used by methods like .Sort or in Controls (which use ICompareable, So to work you need to implement both)

Heiko Hatzfeld Germany

3/6/2008 7:47:04 AM #

pingback

Pingback from alvinashcraft.com

Daily Bits - March 6, 2008 | Alvin Ashcraft's Daily Geek Bits

alvinashcraft.com

3/7/2008 8:50:53 PM #

Sam

Very interesting and possibly useful to me--I am reading a book on C# Components, and this stuff seems quite extensible.

Sam United States

3/10/2008 11:58:43 PM #

pingback

Pingback from sitepoint.com

SitePoint Blogs » .NET on the NET March 2-9: MIX Hangover Edition

sitepoint.com

5/28/2008 1:56:25 AM #

pingback

Pingback from jeffgaroutte.com

Extending the ASP.Net Security model to use rights : Part Four - RightPermission

jeffgaroutte.com

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

IraIra
I'm just another developer from Florida.

 

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

All code samples on this website are free for download, use, and modification with no warranty nor any implied warranty. Liscensed by:

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 United States License.