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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS