Lädt...


🔧 Different ways to implement Shallow Copy in C#


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In C#, reference types are objects that are stored in the heap. When these types are assigned or passed, they share the same memory location. This behavior can lead to unintended consequences, as modifying one reference affects all other references pointing to the same object. In certain scenarios, it may be necessary to create a separate value copy from a reference type to avoid such side effects.

Consider the following class, which contains several value-type properties:

    public class Employee
    {
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
        public string Title { get; private set; }


        public Employee(string firstName, string lastName, string title)
        {
            FirstName = firstName;
            LastName = lastName;
            Title = title;
        }
        public void UpdateName(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }

        public void UpdateTitle(string title)
        {
            Title = title;
        }
    }

Suppose we create an instance of the Employee class:
Employee employeeInfo = new Employee("Ahmed", "Elmehalawi", "Software Engineer");

If we then create another Employee object by copying the employeeInfo data:
Employee copiedEmployeeInfo = employeeInfo;

Any changes made to employeeInfo will be reflected in copiedEmployeeInfo. For example, updating the title using the UpdateTitle method:
employeeInfo.UpdateTitle("Project Manager")

This update will change the title to "Project Manager" for both employeeInfo and copiedEmployeeInfo, as both variables reference the same memory location on the heap.
However, if the intention is to create a copy of the object rather than share the same reference, we need to perform a shallow copy. A shallow copy creates a new object but copies the original object’s properties, ensuring that changes made to one do not affect the other.

Ways to implement Shallow Copy in C#:

1. MemberwiseClone
The MemberwiseClone method creates a shallow copy of the object:

public Employee ShallowCopy()
{
    return (Employee)this.MemberwiseClone();
} 

Usage:-
Employee copiedEmployeeInfo = employeeInfo.ShallowCopy();
In this case, any changes made to one instance will not reflect on the other.

2. ICloneable interface
Implementing the Clone method using the ICloneable interface, which also utilizes MemberwiseClone:

public object Clone()
{
    return (Employee)this.MemberwiseClone();
}

3. Copy Constructor
A copy constructor explicitly creates a new instance by copying the values of the original object's properties:

public Employee(Employee employee)
{
    FirstName = employee.FirstName;
    LastName = employee.LastName;
    Title = employee.Title;
}

Usage:
Employee copiedEmployeeInfo = new Employee(employeeInfo);

4. JSON Serialization and Deserialization
Another approach is to use JSON serialization and deserialization to create a copy:

Employee copiedEmployeeInfo = JsonConvert.DeserializeObject<Employee>(JsonConvert.SerializeObject(employeeInfo));

5. Manual Property Copying
You can manually create a new object and assign values from the original object’s properties:

Employee copiedEmployeeInfo = new Employee(employeeInfo.FirstName, employeeInfo.LastName, employeeInfo.Title);
...

🔧 What are different between Normal Copy and Shallow Copy, Deep Copy in JavaScript Object


📈 63.75 Punkte
🔧 Programmierung

🔧 Different ways to implement Shallow Copy in C#


📈 60.57 Punkte
🔧 Programmierung

🔧 JavaScript Shallow Copy vs Deep Copy: Examples and Best Practices


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow Copy vs. Deep Copy in JavaScript


📈 43.22 Punkte
🔧 Programmierung

🔧 Deep Copy vs. Shallow Copy in C# – What’s the Difference?


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow Copy vs Deep Copy - ¿Qué son realmente? - Ejemplos con JavaScript y Python


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow Copy vs Deep Copy - ¿Qué son realmente? - Ejemplos con JavaScript y Python


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow Copy vs Deep Copy in JavaScript


📈 43.22 Punkte
🔧 Programmierung

🔧 What is the difference between a deep copy and a shallow copy in JavaScript?


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow Copy vs Deep Copy in JavaScript


📈 43.22 Punkte
🔧 Programmierung

🔧 JS - Shallow Copy and Deep Copy of an object


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow Copy v/s Deep Copy


📈 43.22 Punkte
🔧 Programmierung

🔧 Shallow vs Deep Copy of Objects in JavaScript


📈 32.41 Punkte
🔧 Programmierung

🔧 Javascript: Deep & Shallow Copy Complete Guide


📈 32.41 Punkte
🔧 Programmierung

🔧 Understanding Deep vs Shallow Copy in JavaScript: A Comprehensive Guide


📈 32.41 Punkte
🔧 Programmierung

🐧 [$] Zero-copy I/O for ublk, three different ways


📈 27.77 Punkte
🐧 Linux Tipps

🔧 Implement React v18 from Scratch Using WASM and Rust - [26] Implement React.lazy


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [22] Implement memo


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [20] Implement Context


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [18] Implement useRef, useCallback, useMemo


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [17] Implement Concurrent Mode


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust [16] Implement React Noop


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [14] Implement Scheduler


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [13] Implement Lane and Batch Update


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [12] Implement Update for Multi Node


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [11] Implement Event System


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [10] Implement Update for Single Node.


📈 22.41 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [27] Implement useTransition


📈 22.41 Punkte
🔧 Programmierung

📰 Windows 11: Aus Copy-Paste wird Copy-Advanced-Paste - Microsofts nächster Geniestreich mit KI?


📈 21.63 Punkte
📰 IT Nachrichten

📰 Windows 11: Aus Copy-Paste wird Copy-Advanced-Paste - Microsofts nächster Geniestreich mit KI?


📈 21.63 Punkte
📰 IT Nachrichten

📰 Windows 11: Aus Copy-Paste wird Copy-Advanced-Paste - Microsofts nächster Geniestreich mit KI?


📈 21.63 Punkte
📰 IT Nachrichten

🍏 Copy 'Em 3.0.0 - Powerful clipboard menulet for copy and paste.


📈 21.63 Punkte
🍏 iOS / Mac OS

🍏 AnyMP4 DVD Copy for Mac 3.1.32 - Copy and burn DVD-discs.


📈 21.63 Punkte
🍏 iOS / Mac OS

🐧 Copy-Item: Copy Files Like a Boss in PowerShell


📈 21.63 Punkte
🐧 Linux Tipps

matomo