New .NET Library Does Deep Cloning Right

Fast deep cloning library for .NET 8+. Zero-config, works out of the box.


This content originally appeared on HackerNoon and was authored by lofcz

Deep cloning objects has been a major pain point for me for a long time. Recently, I’ve run into a case where after cloning a dictionary ContainsKey no longer worked for items cloned.

\ This inspired me to create a new library FastCloner for the modern .NET Core that would just work. Out of the box. Cyclic references, delegates, read-only collections… as many edge cases covered as possible.

\ There are still some scenarios, such as unmanaged memory, where cloning can go awry, but it should be a major improvement over the prior art. Feel free to check it out!

\ Using FastCloner is straightforward:

\

  1. Install the library:
dotnet add package FastCloner
  1. Clone your objects:
using FastCloner.Code;
var clone = FastCloner.DeepClone(new { Hello = "world", MyList = new List<int> { 1 } });

\n Excluding fields & properties from cloning is simple as well:

private class TestPropsWithIgnored
{
    [FastClonerIgnore] // <-- decorate such members with [FastClonerIgnore]
    public string B { get; set; } = "My string";

    public int A { get; set; } = 10;
}

TestPropsWithIgnored original = new TestPropsWithIgnored { A = 42, B = "Test value" };
TestPropsWithIgnored clone = original.DeepClone(); // clone.B is null (default value of a given type)

\n As a bonus, FastCloner also supports shallow cloning:

// the list is shared between the two instances
var clone = FastCloner.ShallowClone(new { Hello = "world", MyList = new List<int> { 1 } });

\ Intrigued? Try it out:

https://github.com/lofcz/FastCloner


This content originally appeared on HackerNoon and was authored by lofcz


Print Share Comment Cite Upload Translate Updates
APA

lofcz | Sciencx (2025-01-13T16:15:52+00:00) New .NET Library Does Deep Cloning Right. Retrieved from https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/

MLA
" » New .NET Library Does Deep Cloning Right." lofcz | Sciencx - Monday January 13, 2025, https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/
HARVARD
lofcz | Sciencx Monday January 13, 2025 » New .NET Library Does Deep Cloning Right., viewed ,<https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/>
VANCOUVER
lofcz | Sciencx - » New .NET Library Does Deep Cloning Right. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/
CHICAGO
" » New .NET Library Does Deep Cloning Right." lofcz | Sciencx - Accessed . https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/
IEEE
" » New .NET Library Does Deep Cloning Right." lofcz | Sciencx [Online]. Available: https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/. [Accessed: ]
rf:citation
» New .NET Library Does Deep Cloning Right | lofcz | Sciencx | https://www.scien.cx/2025/01/13/new-net-library-does-deep-cloning-right/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.