Skip to content

[Analyzer Proposal]: Don't use stringBuilder.Append(new string(c, repeatCount)) #117643

@Youssef1313

Description

@Youssef1313

Background and motivation

In microsoft/testfx, we have had an occurrence of this pattern (see microsoft/testfx#6013).

This pattern can be replaced by stringBuilder.Append(c, repeatCount) to avoid the allocation of an intermediate string.

API Proposal

A CAxxx rule shipped in .NET SDK (Category=Performance) that suggests the simplified, and more performant overload of StringBuilder.Append

API Usage

Warning on:

builder.Append(new string(c, repeatCount))

A codefix is provided to change the above to:

builder.Append(c, repeatCount)

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzerin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions