PC Review


Reply
Thread Tools Rate Thread

Object copy or clone

 
 
=?Utf-8?B?TWljaGFlbA==?=
Guest
Posts: n/a
 
      3rd Feb 2004
I am implementing object copying in C#. I can think of two ways: (1)Use the famous C++ way of copy constructor, (2)Use IClonable interface.
I have inherited classes that I would like to implement copying. Which is the best way in C#
Thanks for the help in advance

Michael
 
Reply With Quote
 
 
 
 
Chris Capel
Guest
Posts: n/a
 
      3rd Feb 2004
The only reason the ICloneable interface would be preferable would be if you
need to give your objects to some System.* class that uses that interface to
clone your objects. Some collections may do this. Since constructors aren't
a part of interfaces, you can't have an interface that says the class has a
copy constructor.

What I'm saying is that I think the difference between the two methods is
exactly the same as the difference between using interfaces or not using
interfaces in general.

Chris

"Michael" <(E-Mail Removed)> wrote in message
news:23DB0747-F0D7-4919-B740-(E-Mail Removed)...
> I am implementing object copying in C#. I can think of two ways: (1)Use

the famous C++ way of copy constructor, (2)Use IClonable interface.
> I have inherited classes that I would like to implement copying. Which is

the best way in C#?
> Thanks for the help in advance.
>
> Michael



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      3rd Feb 2004
Michael,
The .NET method is to implement the IClonable interface, as you can enable
it to behave polymorphically. IClonable is relatively easy to implement with
Object.MemberwiseClone, if your hierarchary supports shallow copies.

There was a long thread in microsoft.public.dotnet.general from about 3 Jan
04 titled "Copy constructors and clones" that discusses why IClonable is
"better" then copy constructors.

It also discusses why one may want to implement IClonable in terms of a
protected copy constructor (to support deep copy of Readonly fields).

You should be able to search http://groups.google.com for the thread.

In addition to copy constructors & cloning, you could also use serialization
to make a copy of an object. (Implementiong IClonable in terms of
serialization is an option, especially when you already implement
serialization).

Hope this helps
Jay

"Michael" <(E-Mail Removed)> wrote in message
news:23DB0747-F0D7-4919-B740-(E-Mail Removed)...
> I am implementing object copying in C#. I can think of two ways: (1)Use

the famous C++ way of copy constructor, (2)Use IClonable interface.
> I have inherited classes that I would like to implement copying. Which is

the best way in C#?
> Thanks for the help in advance.
>
> Michael



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy/Clone object tshad Microsoft C# .NET 1 26th Jan 2009 07:55 AM
How to clone an object ? =?Utf-8?B?QW5kcmV3?= Microsoft Dot NET Framework 2 6th Jun 2007 07:39 AM
How to clone an object? Rob R. Ainscough Microsoft VB .NET 2 17th Dec 2005 02:11 PM
Clone an object? Alfred Salton Microsoft ASP .NET 1 28th Jun 2004 07:53 PM
Re: clone a object Fergus Cooney Microsoft C# .NET 2 8th Sep 2003 11:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:39 PM.