devxlogo

Crafting Strings in C#

Crafting Strings in C#

ecently, I came across an application that contained a binary + operator in which at least one of the operands was of type string.

While debugging this app, I found that the Microsoft C# compiler had compiled the the program so that the value was 158, while the maximum value allowed was 100.


Why is the value exceeding the maximum value allowed?


Using this small application, test your application’s summing of before compiling.

Recreating the Problem
Suppose you had the following logged string:

string s = a + b + c;

In the above string, a signifies the shown text, while the integer values of b and c represent the value of , after summing.

Therefore, the question you need to answer is: how is the summing being performed at run time?

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;	Console.WriteLine(s);

When you compile it and look at the IL code, you’ll find that the C# compiler has boxed b and c. Here’s an extract from the IL code:

	. . .	.locals init ([0] int32 b,	[1] int32 c,[2] string s)IL_0000:  ldc.i4.1  	IL_0001:  stloc.0  	IL_0002:  ldc.i4.2  	IL_0003:  stloc.1  	IL_0004:  ldstr      "ABC"  	IL_0009:  ldloc.0  	IL_000a:  box        [mscorlib]System.Int32  	IL_000f:  ldloc.1  	IL_0010:  box        [mscorlib]System.Int32	. . .

Next, concatenate the boxed values (reference type):

IL_0015:  call string [mscorlib]System.String::Concat(object,                                                            object,                                                            object)

Lessons Learned
What are the lessons learned here?

  1. 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);
  2. 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.

  3. 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:
    string s = a + (b + c).ToString();

    This code runs about 30 percent faster. Thus, the shortest code is not the fastest code!

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.

devx-admin

Share the Post: