Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.04 KB

File metadata and controls

49 lines (36 loc) · 1.04 KB
layout page
title CC0039
tagline StringBuilderInLoopAnalyzer

|TypeName|StringBuilderInLoopAnalyzer | |Check Id | CC0039 | |Category | Performance | |Severity | Warning |

Cause

Do not concatenate a string on a loop. It will alocate a lot of memory. Use a StringBuilder instead. It will will require less allocation, less garbage collector work, less CPU cycles, and less overall time.

Example

{% highlight csharp %} var values = ""; foreach (var item in Enum.GetValues(typeof(LoaderOptimization))) { values += item.ToString(); } {% endhighlight %}

Code fix

A code fix will be presented to you that will transform the code, it will use StringBuilder to concatenate the string.

{% highlight csharp %} var values = ""; var builder = new StringBuilder(); builder.Append(values); foreach (var item in Enum.GetValues(typeof(LoaderOptimization))) { builder.Append(item.ToString()); } values = builder.ToString(); {% endhighlight %}

![Code fix]({{ BASE_PATH }}/assets/images/CC0039/codefix0.png)

Related rules

TBD

See also

TBD