How to identify duplicate code in Visual Studio 2022
Asked Answered
H

3

6

When I do code merge between branches sometimes the auto merge in Visual Studio 2022 may result in duplicated code segments or duplicated methods. So in such a scenario, I need to efficiently find and remove the duplicated code segments.

How can I find duplicated lines or duplicated code segments in Visual Studio 2022? Is there a method for it or are there any plugins available to download?

I am aware the for Visual Studio Code there's a plug-in by the name duplicate-lines-finder (https://marketplace.visualstudio.com/items?itemName=tlevesque2.duplicate-finder) but what I need is for Visual Studio 2022

Hypervitaminosis answered 15/9, 2023 at 14:53 Comment(1)
In the 'Analyze' menu you have the 'Analyze Solution For Code Clones' option. (Tool must be enabled in installer under 'individual components' - 'Code Tools' - 'Code Clone', may only be available in the Enterprise Edition).Method
M
6

In Visual Studio Enterprise, first install

Visual Studio Installer=>Individual Components => Code Tools => Code Clone

enter image description here

Then use Visual Studio Enterprise Edition to open your project and click

Analyze=> Analyze Solution For Code Clones to find duplicate code blocks

enter image description here

enter image description here

Motif answered 18/9, 2023 at 8:44 Comment(0)
R
1

CodeRush

If you use Visual Studio Community Edition, you can install CodeRush extension, from DevExpress (https://www.devexpress.com/products/coderush)

It's free (https://www.devexpress.com/coderush-free) and quite powerful.

See the documentation for Duplicate Code analysis: https://docs.devexpress.com/CodeRushForRoslyn/401720/static-code-analysis/duplicate-code

Rudie answered 14/12, 2023 at 16:52 Comment(0)
R
0

You can identify C# Code Duplicate with NDepend (free trial here).

The open-source command line power tool lists suspect code this way: C# Code Duplicate found

And then typing "o" will open the source code:

C# Code Duplicate More details about how it works here: Find C# Code Duplicate

Ricky answered 22/10, 2024 at 8:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.