# Duplicate lines in C# code

**URL:** <https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031>\
**Category:** SonarQube Server / Community Build\
**Tags:** csharp, sonarqube\
**Created:** [August 25, 2020, 11:18am UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031 "2020-08-25T11:18:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viktors\_Telle](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/viktors_telle/32/7102_2.png) [@Viktors\_Telle](https://community.sonarsource.com/u/Viktors_Telle)\
**Post date:** [August 25, 2020, 11:18am UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/1 "2020-08-25T11:18:13Z")

</div>

We are using 8.4.0.35506 of SonarQube developer edition.  
After running SQ analysis for the new feature, SQ started to show a lot of duplicate lines in C# code. Is this expected or a bug? For me, it looks like the lines are not the same.

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/b/bbe21bb1694a359350ade2c6dbb0c1ffdfe422cf.png)

---

<div class="post-metadata">

**Author:** ![bugbouncer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/bugbouncer/32/10396_2.png) [@bugbouncer](https://community.sonarsource.com/u/bugbouncer)\
**Post date:** [August 26, 2020, 1:45pm UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/2 "2020-08-26T13:45:49Z")

</div>

Hi @Viktors_Telle,  
There are no duplicates in the code of your screenshot, but in another class (or maybe further down in this class).  
When you click on the thin, grey, vertical bar on the very left, SonarQube will show the file, that contains the same code.

---

<div class="post-metadata">

**Author:** ![Viktors\_Telle](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/viktors_telle/32/7102_2.png) [@Viktors\_Telle](https://community.sonarsource.com/u/Viktors_Telle)\
**Post date:** [August 26, 2020, 3:35pm UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/3 "2020-08-26T15:35:17Z")

</div>

Hi @bugbouncer,

Thanks for your suggestion! I clicked on the grey line, and SQ shows that duplicates are in the same file.  
Then I clicked on line 10:

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/4/4d2cb1be850eb7955f5a0009be01819e4ed4476d.png)

And scrolled down to the suggested duplicate lines (824 - 869):

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/e/e3d9bd119ee586f79c840ab2e0ccdccc31131490.png)

I can’t understand why these are duplicate lines. I also checked few other places, and could not detect duplicate lines there as well.  
This massive class contains the list of all countries with appropriate translations.

---

<div class="post-metadata">

**Author:** ![Viktors\_Telle](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/viktors_telle/32/7102_2.png) [@Viktors\_Telle](https://community.sonarsource.com/u/Viktors_Telle)\
**Post date:** [August 31, 2020, 11:56am UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/4 "2020-08-31T11:56:52Z")

</div>

I have attached the full file.  
[CountryList.txt](https://community.sonarsource.com/uploads/short-url/uhMdUoG4LCsFXsMRCzSQQsl6EGY.txt) (79.0 KB)

---

<div class="post-metadata">

**Author:** ![bugbouncer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/bugbouncer/32/10396_2.png) [@bugbouncer](https://community.sonarsource.com/u/bugbouncer)\
**Post date:** [September 2, 2020, 7:48am UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/5 "2020-09-02T07:48:31Z")

</div>

Hi @Viktors_Telle,  
I have checked the SonarQube documentation regarding the duplication metric:

> **[metric definition](https://docs.sonarsource.com/sonarqube/latest/user-guide/metric-definitions/)**
>
> SonarQube analysis produces many types of code analysis metrics.

For Java it says:

> There should be at least 10 successive and duplicated statements whatever the number of tokens and lines. Differences in indentation and in string literals are ignored while detecting duplications.

The difference in you lines of code are “just” the string literals - the actual code is repeated over and over.  
That should be the explanation.

---

<div class="post-metadata">

**Author:** ![Viktors\_Telle](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/viktors_telle/32/7102_2.png) [@Viktors\_Telle](https://community.sonarsource.com/u/Viktors_Telle)\
**Post date:** [September 2, 2020, 10:09am UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/6 "2020-09-02T10:09:10Z")

</div>

Hi,

Thanks for finding the explanation in the documentation.  
Currently, it seems that there is no way to fix this particular duplicate code so that SonarQube is happy.  
Probably, I will add this file to `sonar.cpd.exclusions` parameter to ignore the duplicate checking during analysis.

---

<div class="post-metadata">

**Author:** ![bugbouncer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/bugbouncer/32/10396_2.png) [@bugbouncer](https://community.sonarsource.com/u/bugbouncer)\
**Post date:** [September 2, 2020, 1:01pm UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/7 "2020-09-02T13:01:12Z")

</div>

Why not move these data into a database table or a classic i18n-resource-bundle?

---

<div class="post-metadata">

**Author:** ![Viktors\_Telle](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/viktors_telle/32/7102_2.png) [@Viktors\_Telle](https://community.sonarsource.com/u/Viktors_Telle)\
**Post date:** [September 3, 2020, 10:02am UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/8 "2020-09-03T10:02:54Z")

</div>

Thanks for your suggestions! The database in this case would be overkill since we don’t use it in this application. We can consider storing this data in `.json` file, reading it on application startup, and “caching” data in static property of the static class.  
Thanks again for your help! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [September 23, 2020, 12:43pm UTC](https://community.sonarsource.com/t/duplicate-lines-in-c-code/30031/9 "2020-09-23T12:43:20Z")

</div>

Closed on September 23, 2020, 12:43pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
