Find Duplicate Code in C# with Deslop

cfdevelop1 pts0 comments

Find Duplicate Code in C# with Deslop | Christian Findlay

Find Duplicate Code in C# with Deslop

Christian Findlay

July 30, 2026<br>9 min read

Edit on GitHub

Found an inaccuracy or want to improve this page? Clicking will prompt you to fork the repository and submit a pull request with your changes!

dotnet

csharp

code-quality

visual-studio

ai

deslop

Duplicate code looks harmless until you fix a bug in one copy and miss the other three. C# makes extracting shared code easy. Visual Studio can extract a method, Roslyn can enforce hundreds of rules, and mature tools can measure duplication across a repository. The hard part is finding the copies early enough.

This is the problem I built Deslop to solve. Deslop is a live duplicate-code analysis server with a C# parser, a VS Code extension, an LSP for editor feedback, an MCP server for AI coding agents, and a CLI for CI. It doesn’t just wait for a pull request. It updates after you save and lets an agent call find-similar before it writes another version of something that already exists.

If you’re looking for a C# code duplication tool, the short answer is to install Deslop for VS Code. The rest of this article explains why.

Key Takeaways : Deslop finds exact, renamed, and near-miss duplicate C# code with syntax-aware analysis. Optional local embeddings help find semantic matches. The VS Code extension gives you live feedback, while MCP lets coding agents check before they create a duplicate. For eligible exact clones in the same C# file, Deslop can offer a guarded Extract identical code to shared method action. It refuses cases it cannot rewrite safely, so you remain in control of the refactor.

What Is Duplicate Code?

Duplicate code repeats logic in more than one place. Sometimes somebody literally copied and pasted it. Sometimes two developers independently wrote the same thing. Increasingly, an AI agent produces a new implementation because it didn’t know the canonical one already existed.

Researchers usually split code clones into four types. The names sound academic, but the idea is simple:

Type<br>What It Means<br>C# Example

Type-1<br>The same code apart from whitespace or comments<br>A copied method with different formatting

Type-2<br>The same structure with renamed identifiers or changed literals<br>The same calculation with different variable names

Type-3<br>A near-miss with added, removed, or changed statements<br>A copied validation block with one extra check

Type-4<br>The same behaviour expressed with different code<br>A loop and a LINQ query that calculate the same result

A widely cited comparison of code clone detection techniques uses this taxonomy. It matters because plain exact-text search reliably finds only the easiest case. The expensive duplicates have often drifted so far apart that nobody remembers they began as one piece of logic.

Not all repetition is bad. Generated code, framework scaffolding, and test data may be fine. So may two pieces of code that will evolve independently. A detector should give you evidence, not make the architectural decision for you.

A C# Duplicate That Text Search Misses

These methods do the same calculation, but the names are different:

static Receipt CreateRetailReceipt(Order order)<br>var subtotal = order.Items.Sum(item => item.Price * item.Quantity);<br>var tax = Math.Round(subtotal * 0.1m, 2);<br>return new Receipt(subtotal, tax, subtotal + tax);

static Invoice CreateWholesaleInvoice(Purchase purchase)<br>var net = purchase.Lines.Sum(line => line.UnitPrice * line.Count);<br>var gst = Math.Round(net * 0.1m, 2);<br>return new Invoice(net, gst, net + gst);

Searching for subtotal won’t find the second copy. A raw text comparison won’t match it either. Structurally, however, these methods are almost identical. They both sum each price multiplied by its quantity, calculate ten percent tax, and return the three totals.

You could extract that calculation into a small, strongly typed function:

readonly record struct Totals(decimal Subtotal, decimal Tax, decimal Total);

static Totals CalculateTotalsTLine>(<br>IEnumerableTLine> lines,<br>FuncTLine, decimal> price,<br>FuncTLine, int> quantity)<br>var subtotal = lines.Sum(line => price(line) * quantity(line));<br>var tax = Math.Round(subtotal * 0.1m, 2);<br>return new(subtotal, tax, subtotal + tax);

Notice that detection and refactoring are separate decisions. A shared method works for this example. In a real codebase, the right answer might be an extension method, a domain service, a common type, a source generator, or no refactor at all. Deslop finds the relationship. You decide what the relationship means.

Why Roslyn Analyzers Aren’t Enough

I strongly recommend turning on C# code rules. I wrote a whole article about configuring Roslyn analyzers because warnings and style rules stop a codebase from degrading in many predictable ways. Microsoft’s .NET code analysis documentation explains how the SDK analyzers cover quality, security, performance, design, and style.

Repository-wide clone detection is...

code duplicate deslop subtotal find type

Related Articles