Csharpessentials clone

From senrecep/CSharpEssentials · View interactive version

Csharpessentials clone is an AI agent skill from the GitHub repository senrecep/CSharpEssentials. Use when entities need deep-copy semantics. Implement ICloneable<T> on domain objects, then call .Clone() on IEnumerable<T> or IQueryable<T> collections to produce independent deep copies of every element.

How to use this skill

Skill file

CSharpEssentials.Clone

Typed copy contract for domain objects. ICloneable<out T>.Clone() returns T, unlike System.ICloneable, which returns object. The library only calls Clone(); your implementation decides how deep the copy goes.

dotnet add package CSharpEssentials.Clone
using CSharpEssentials.Clone;

Implement ICloneable\<T\>

public class Product : ICloneable<Product>
{
    public int Id       { get; init; }
    public string Name  { get; init; } = "";
    public List<Tag> Tags { get; init; } = new();

    public Product Clone() => new()
    {
        Id   = Id,
        Name = Name,
        Tags = Tags.Select(t => t.Clone()).ToList()  // deep-copy child collections too
    };
}

public class Tag : ICloneable<Tag>
{
    public string Value { get; init; } = "";
    public Tag Clone() => new() { Value = Value };
}

Clone collections

List<Product> products = [new() { Id = 1, Name = "Pen" }];

// IEnumerable<T> where T : ICloneable<T>: lazily clones each element
IEnumerable<Product> copies = products.Clone();

// IQueryable<T> where T : ICloneable<T>: clones in memory, not translated to SQL;
// marked [RequiresUnreferencedCode]/[RequiresDynamicCode], so use the IEnumerable<T> overload under AOT
IQueryable<Product> projected = products.AsQueryable().Clone();

Typical use

Snapshot EF Core results before applying in-memory transformations, without mutating tracked entities:

List<Product> snapshot = await db.Products
    .Where(p => p.Name.StartsWith("P"))
    .ToListAsync();

List<Product> working = snapshot.Clone().ToList();  // independent deep copies; mutations don't affect EF tracking

Guidance

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.