S3063 — "StringBuilder" data should be used

Language
VB.NET
Type
Code smell
Severity
Major
Tags
performance

Why is this an issue?

StringBuilder instances that never build a string clutter the code and worse are a drag on performance. Either they should be removed, or the missing ToString() call should be added.

Noncompliant code example


Public Sub DoSomething(ByVal strings As List(Of String))
    Dim sb As StringBuilder = New StringBuilder() ' Noncompliant
    sb.Append("Got: ")

    For Each str As String In strings
        sb.Append(str).Append(", ")
    Next
End Sub

Compliant solution


Public Sub DoSomething(ByVal strings As List(Of String))
    For Each str As String In strings
    Next
End Sub

or


Public Sub DoSomething(ByVal strings As List(Of String))
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("Got: ")

    For Each str As String In strings
        sb.Append(str).Append(", ")
    Next

    My.Application.Log.WriteEntry(sb.ToString())
End Sub

Exceptions

No issue is reported when StringBuilder is:

↑ Back to top