RE: how to get a copy of ref type object

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ansil MCAD (AnsilMCAD_at_discussions.microsoft.com)
Date: 12/02/04


Date: Wed, 1 Dec 2004 21:13:06 -0800

hi
Take a look at
Object.MemberwiseClone Method

[MSDN]
This method cannot be overridden; a derived class should implement the
ICloneable interface if a shallow copy is not appropriate. MemberwiseClone is
protected and, therefore, is accessible only through this class or a derived
class.

A shallow copy creates a new instance of the same type as the original
object, and then copies the non-static fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object is
not; therefore, the reference in the original object and the reference in the
clone point to the same object. In contrast, a deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object.

For example, if X is an Object with references to the objects A and B, and
the object A also has a reference to an object M, a shallow copy of X is an
object Y, which also has references to objects A and B. In contrast, a deep
copy of X is an object Y with direct references to objects C and D, and an
indirect reference to object N, where C is a copy of A, D is a copy of B, and
N is a copy of M.

[/ MSDN]

this is the example in msdn

The following code example shows how to copy an instance of a class using
MemberwiseClone.

[C#]
using System;

class MyBaseClass {
   public static string CompanyName = "My Company";
   public int age;
   public string name;
}

class MyDerivedClass: MyBaseClass {

   static void Main() {
   
   // Creates an instance of MyDerivedClass and assign values to its fields.
   MyDerivedClass m1 = new MyDerivedClass();
   m1.age = 42;
   m1.name = "Sam";

   // Performs a shallow copy of m1 and assign it to m2.
   MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
   }
}

regards
Ansil

"lion" wrote:

> in .net, if you set annstance-A of a class equal to another instance-B, a
> pointer will add to B, but if i want to create a copy of B instead of
> pointer, how to operate?
>
> Note:serialization isn't permitted
>
> 3x



Relevant Pages

  • Re: InternetExplorer.Application methods and properties reference
    ... MSDN is available on CD with MS ... IE pages if you want to keep a good, quick reference at hand. ... Drazen Petrek wrote in message ... > (so the script does not create its own InternetExplorer.Application ...
    (microsoft.public.scripting.vbscript)
  • Re: SysTabControl32 class
    ... Where did I find a reference to the MK_LBUTTON constant? ... MSDN website, I lookup an API function, it requires the use of some ... most SDKs have never included constant definitions. ... Anyone here know of a web resource that lists API constants. ...
    (microsoft.public.vb.winapi)
  • Re: MSH Language Reference
    ... also I noticed the the quickstart guide there is still older then the Beta 3.0 ... TDM wrote: ... to find a Language Reference for MSH. ... I had already downloaded the docs you reference, but the MSDN ...
    (microsoft.public.windows.server.scripting)
  • Re: What is WebBrowser control called now?
    ... Anil Gupte ... local MSDN, ... WebBrowser Class ... reference before I could insert it. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: MSH Language Reference
    ... content on MSDN. ... other convenient print file format on a chapter by chapter or book basis ... I really like the guide, but I'm much better in consuming printed data ... to find a Language Reference for MSH. ...
    (microsoft.public.windows.server.scripting)