FastCloner 3.3.2

dotnet add package FastCloner --version 3.3.2                
NuGet\Install-Package FastCloner -Version 3.3.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastCloner" Version="3.3.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FastCloner --version 3.3.2                
#r "nuget: FastCloner, 3.3.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FastCloner as a Cake Addin
#addin nuget:?package=FastCloner&version=3.3.2

// Install FastCloner as a Cake Tool
#tool nuget:?package=FastCloner&version=3.3.2                

FastCloner FastCloner.Contrib

FastCloner

<img align="left" width="128" height="128" alt="Te Reo Icon" src="https://github.com/user-attachments/assets/54f5be37-543a-411d-b6e6-90a77414926c" /> Fast deep cloning library for .NET 8+. Supports both deep and shallow cloning. Extensively tested, focused on performance and stability even on complicated object graphs. FastCloner is designed to work with as few gotchas as possible out of the box. The mapping is zero-config by default. Clone your objects and be done with it <em>fast</em>. FastCloner builds upon <a href="https://github.com/force-net/DeepCloner">DeepClone</a>.

<br/><br/>

Getting Started

Install the package via NuGet:

dotnet add package FastCloner
dotnet add package FastCloner.Contrib # only required for some special types, such as Fonts

Clone your objects:

using FastCloner.Code;
var clone = FastCloner.DeepClone(new { Hello = "world", MyList = new List<int> { 1 } });

That's it! Feel free to map this method to your extension so if you need to migrate in the future it's a matter of just switching that method. We intentionally don't ship our own .DeepClone() extension method.

Advanced usage

Sometimes you might want to exclude certain fields & properties from cloning:

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)

Apart from deep cloning, FastCloner supports shallow cloning and deep cloning to target:

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

Limitations

FastCloner uses caching by default which makes evaluating properties harder. Cloning unmanaged resources, such as IntPtrs may result in side-effects, as there is no metadata for the length of buffers such pointers often point to. ReadOnly collections are tested to behave well as long as they follow basic conventions. Many other features, such as cloning Dictionaryies properly while keeping hashcodes, INotifyPropertyChanged, delegates, events, HttpRequests / responses, and others are supported. If something doesn't work out of the box let me know in the issues, the repository is actively maintained.

Performance

FastCloner aims to work correctly and meet reasonable expectations by default while being fast. Benchmarking results are available here, check them out! By default, fast cloner relies on heavily cached reflection to work. An incremental source generator is currently in development as an opt-in alternative for performance-critical scenarios.

License

MIT, simple 💜

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on FastCloner:

Package Downloads
FastCloner.Contrib

Extends FastCloner with support for certain special types.

FastCloner.Benchmark

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.2 205 1/9/2025
3.3.1 37 1/9/2025
3.2.1 30 1/8/2025
3.1.9 45 1/8/2025
3.1.7 45 1/8/2025
3.1.6 39 1/8/2025
3.1.5 39 1/8/2025
3.1.4 55 1/8/2025
1.1.4 50 1/7/2025
0.1.2 33 1/8/2025