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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing