Skip to content

Gendarme.Rules.Performance.AvoidConcatenatingCharsRule(2.10)

Sebastien Pouliot edited this page Feb 9, 2011 · 3 revisions

AvoidConcatenatingCharsRule

Assembly: Gendarme.Rules.Performance
Version: 2.10

Description

This rule will warn you if boxing is used to concatenate a string since this will slow down your code and can be easily avoided. This often happen when concatenating System.String and System.Char values together. However the rule is not limited to check boxing on System.Char since compilers often transform a character into its integer value (e.g. 'a' == 61) and the same boxing issue exists on integers.

Examples

Bad example:

public string PrintIndex (string a, char b)
{
    // This is compiled into String.Concat(params object[])
    // and requires boxing the 4 characters
    Console.WriteLine ('[' + a + ',' + b + ']');
}

Good example:

public string PrintIndex (string a, char b)
{
    // This is compiled into String.Concat(params string[])
    // and no boxing is required
    Console.WriteLine ("[" + a + "," + b.ToString () + "]");
}

Notes

  • This rule is available since Gendarme 2.8
Clone this wiki locally