ALERT

[FREE DEMO] Deploy Your Enterprise Cloud in Minutes

Shallow Copy

Definition - What does Shallow Copy mean?

Shallow copy, in C#, is the process of creating a clone of an object by instantiating a new instance of the same type as original object and copying the non-static members of the existing object to the clone. The members of the value type are copied bit by bit while the members of the reference type are copied such that the referred object and its clone refer to the same object.

In general, shallow copy is used when performance is one of the requirements along with the condition that the object will not be mutated throughout the application. By passing the clone containing immutable data, the possibility of corruption by any code is eliminated. Shallow copy is found to be efficient where object references allow objects to be passed around by memory address so that the entire object need not be copied.

Shallow copy is also known as memberwise copy.

Techopedia explains Shallow Copy

Shallow copy is similar to deep copy in the assignment of each member of an object to that of another object, but it differs in the manner in which the field of reference type is copied. Unlike in shallow copy where the reference is only copied, in deep copy, a new copy of the referred object is created.

For example, consider an employee object that contains details of personal information, including a list of address objects that store multiple employee addresses. By performing a shallow copy of an employee object, a clone of the employee object can be created with references to the same list of address objects that are owned by the original employee object.

The methods to perform shallow copy include:
  • Call the MemberwiseClone method of the object
  • Create a clone manually through a custom method that is tedious but easy to control
  • Use a reflection technique that provides automated facility to perform shallow copy, but with performance overhead
  • Use a serialization method that is slower than reflection but automated and simple
Shallow copy cannot be used where the object has members of reference type that are modified frequently.
This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.