. NET heap and Stack 03, reference type Object copy, and memory allocation

Source: Internet
Author: User

In the. NET heap and Stack 01, basic concepts, value type memory allocation, the basic concepts of "heap" and "stack", and the memory allocation of value types. We know that when a method is executed, the value type instance allocates memory on the stack, and the reference type instance allocates memory on the heap, and when the method executes, the instance on the stack is automatically freed by the operating system, and the instance on the heap is reclaimed by the. NET Framework's GC.


In the. NET heap and Stack 02, value types and reference type parameter passing, and memory allocation, we learned about the memory allocations for value type parameters and reference type parameters when they are passed.


This article focuses on the following: Reference type Object copy and memory allocation.

Mainly include:
Reference type Object copy members are value types
Reference type Object copy contains reference type member

Reference type Object copy members are value types

 Public structshoe{ Public stringColor; }             Public classDude { Public stringName;  PublicShoe Rightshoe;  PublicShoe Leftshoe;  PublicDude copydude () {Dude Newperson=NewDude (); Newperson.name=Name; Newperson.leftshoe=Leftshoe; Newperson.rightshoe=Rightshoe; returnNewperson; }                  Public Override stringToString () {return(Name +": dude!, I have a"+ Rightshoe.color +"shoe on my right foot, and a"+Leftshoe.color+"On my left foot."); }            }            Public Static voidMain () {Dude Bill=NewDude (); Bill.name="Bill"; Bill.leftshoe=NewShoe (); Bill.rightshoe=NewShoe (); Bill.LeftShoe.Color= Bill.RightShoe.Color ="Blue"; Dude Ted=Bill.copydude (); Ted.name="Ted"; Ted.LeftShoe.Color= Ted.RightShoe.Color ="Red";                  Console.WriteLine (Bill.tostring ());                       Console.WriteLine (Ted.tostring ()); }

Output Result:
bill:dude!, I has a red shoe on my right foot, and a red in my left foot
ted:dude!, I has a red shoe on my right foot, and a red in my left foot

Above, the copy is a full copy when the property of the reference type, the member is the value type.

Reference type Object copy contains reference type member

Change shoe from struct value type to reference type class.

 Public class shoe{               publicstring  Color;           }

Run again, output result:
bill:dude!, I has a red shoe on my right foot, and a red in my left foot
ted:dude!, I has a red shoe on my right foot, and a red in my left foot

When the Dude class contains reference-type attribute shoe, this is the case on the managed heap:

After copying, the properties of the 2 dude shoe types point to shoe instances within the same managed heap, and changing the value of shoe affects 2 dude at the same time.


Obviously, this is not a complete copy of what we expect, if we do a full copy?
--Implement ICloneable interface

The Clone () method of the ICloneable interface allows us to make some custom settings while copying.

Let the reference class shoe implement the ICloneable interface.

 Public class shoe:icloneable             {                  publicstring  Color;                     Public Object Clone ()                  {                      new  Shoe ();                        as string ;                       return Newshoe;                  }             }

Above, the shoe string attribute color can use the Color.clone () method because string also implements the ICloneable interface, and because the Clone () return type is Object, After using the Color.clone () method, you need to convert the object to a string type.


Now, in the Copydude () method of the Dude class, when copying the shoe type attribute, you can use the shoe unique copy Method clone ().

 Public Dude copydude ()                {                    new  Dude ();                      = Name;                       as Shoe;                       as Shoe;                       return Newperson;                }

Client programs.

 Public Static voidMain () {Dude Bill=NewDude (); Bill.name="Bill"; Bill.leftshoe=NewShoe (); Bill.rightshoe=NewShoe (); Bill.LeftShoe.Color= Bill.RightShoe.Color ="Blue"; Dude Ted=Bill.copydude (); Ted.name="Ted"; Ted.LeftShoe.Color= Ted.RightShoe.Color ="Red";                  Console.WriteLine (Bill.tostring ());                        Console.WriteLine (Ted.tostring ()); }

Output Result:
bill:dude!, I has a blue shoe on my right foot, and a blue in my left foot
ted:dude!, I has a red shoe on my right foot, and a red in my left foot

This is exactly what we expect from a full copy!

Fully copied, the situation on the managed heap is this:

Of course, you can also include both value types and reference type members, and the class that needs to be copied implements the ICloneable interface.

 Public classdude:icloneable { Public stringName;  PublicShoe Rightshoe;  PublicShoe Leftshoe;  Public Override stringToString () {return(Name +": dude!, I have a"+ Rightshoe.color +"shoe on my right foot, and a"+Leftshoe.color+"On my left foot."); }                  #regionICloneable Members Public ObjectClone () {Dude Newperson=NewDude (); Newperson.name= Name.clone () as string; Newperson.leftshoe= Leftshoe.clone () asShoe; Newperson.rightshoe= Rightshoe.clone () asShoe; returnNewperson; }                   #endregion             }

Called by the client.

 Public Static voidMain () {Class1 PGM=NewClass1 (); Dude Bill .=NewDude (); Bill.name="Bill"; Bill.leftshoe=NewShoe (); Bill.rightshoe=NewShoe (); Bill.LeftShoe.Color= Bill.RightShoe.Color ="Blue"; Dude Ted= Bill.clone () asDude; Ted.name="Ted"; Ted.LeftShoe.Color= Ted.RightShoe.Color ="Red";                  Console.WriteLine (Bill.tostring ());                        Console.WriteLine (Ted.tostring ()); }

Output Result:
bill:dude!, I had a blue shoe on my right foot, and a blue in my left foot.
ted:dude!, I had a red shoe on my right foot, and a red on my left foot.

It is also a complete copy of what we expect!

Resources:
C # Heap (ing) Vs Stack (ing) in. Net:part III

". NET Heap and Stack "series includes:

. NET heap and Stack 01, basic concept, value type memory allocation. NET heap and Stack 02, value type and reference type parameter passing, and memory allocation. NET heap and Stack 03, reference type Object copy, and memory allocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.