Testing the Summing
Use this small application to test the summing:
const string a = "ABC";
Int32 b = 1;
Int32 c = 2;
string s = a + b + c;
When you compile it and look at the IL
code, you'll find that the C# compiler has boxed b
. Here's an extract from the IL
. . .
.locals init ( int32 b,
 int32 c,
 string s)
IL_0004: ldstr "ABC"
IL_000a: box [mscorlib]System.Int32
IL_0010: box [mscorlib]System.Int32
. . .
Next, concatenate the boxed values (reference type):
IL_0015: call string [mscorlib]System.String::Concat(object,
What are the lessons learned here?
- State What You Want: In the above example, it's clear that the summing is performed as a concatenation of the string representations of the values of ABC, 1, and 58. This is why the value logged in at 158 instead of 59. Adding parentheses solves the problem:
string s = a + (b + c);
- Read the Language Specification: Looking at section 14.7.4 in the Standard ECMA-334 - C# Language Specification, you'll find this statement:
|"...any non-string operand is converted to its string representation by invoking the virtual ToString method inherited from type object."
This means that the ToString() method is called on each object by the Concat overload in the above IL code.
- Use Methods of Int32 Structure: So if ToString() is called on object, why not call it directly on (b+c)? Eliminate the expensive boxing used by the C# compiler and the code will be executed faster. The ToString() method of the Int32 structure converts the numeric value of the instance to its equivalent string representation. This way, the C# compiler uses the Concat(string, string) overload method. Here's what the code looks like:
This code runs about 30 percent faster. Thus, the shortest code is not the fastest code!
string s = a + (b + c).ToString();
|Author's Note: While in some cases, the Append() method of the StringBuilder class performs better than the Concat method, a discussion of these cases is outside the scope of this material.