Raj Aththanayake's Blog Raj Aththanayake's Blog | May 2010

Working with Strings in C#.NET

30. May 2010

Strings, strings and strings. They are all over in your application. You cannot avoid them at all. This article is about some tips on how you should use strings within your application. Most developers know these tips, but I still see some developers write inefficient code.


 Before we drill into much deeper, would you recommend using String over string? For example…

String apple;
string apple;

Not really, string is simply an alias (shorthand) for the Common Type System (CTS) System.String class, which represents a sequence of characters. You can use them interchangeably in your code. For example…

String x = string.Copy("x");
string y = String.Copy("y");


String concaternations

Often developer concatenate string using + operator. Some developer use StringBuilder class in System.Text namespace.

string concat = "Apple" + ", " + "Orange";
StringBuilder sb = new StringBuilder();
sb.Append(", ");

So which one you should be using? It depends. For example if you have few strings that need to concatenate, then ‘+’ operator would just be fine. However if you have so many of them, it is better to use the StringBuilder class. The reason is that direct string concatenation will always consume more memory as it creates String objects for every instance that it creates. On the other hand StringBuilder is a complex type, which require instantiating once (also take up a negligible start-up time), and Append method is much faster, better CPU and efficient memory usage. Because of the tiny start up cost of instantiating StringBuilder class, it is preferable to use ‘+’ operators for when concatenating only few strings.

Below is an excellent article on string concatenations performances

The conclusion to be drawn from these test results is really very straightforward. You should be using the StringBuilder class for all but the most trivial string concatenation (or replace) operations. The extra effort required to use the StringBuilder class is negligible and is far outweighed by the potential performance and scalability benefits to be gained.

More…. http://msdn.microsoft.com/en-us/library/aa302329.aspx

Also avoid String.Format() for string concatenation. They are very useful for globalization and other tasks, however not meant to be used for appending strings to each other.


Empty Strings

string a = "";
string c = String.Empty;

There is no much performance differences on any of these. If you want the maximum efficiency and better use of CPU memory use String.Empty. String.Empty is a static and read only field. It does not create any object. Because when you use an empty string “”, it still creates a new object. If you are performing a string comparison with an empty string, for example...

if (testString == String.Empty)
     testString = "Test string is empty";

if (StringA.Equals(String.Empty))
     testString = "Test string is empty"; 

if (testString.Length == 0)
      testString = " Test string is empty";

Some of you would argue the lack of readabilty on Legth == 0, but the truth is the Length comparison is the most efficient. If you need to check for null or empty string then String.IsNullOrEmpty() is also better on performances.


Securing your strings

Common string classes in .NET framework store string in memory. If you are dealing with very sensitive data, then storing in memory is not the most secure way. For this you need to use SecureString class which is System.Secure name space.

System.Security.SecureString secureString = new System.Security.SecureString();

Unfortunately you cannot assign a string to the secure class. Please find more information on Secure String below.



Compare Strings

Many developers tend to use the case insensitivity to compare two strings. This is not really efficient when comparing two strings.

if (testStringA.ToUpper() == testStringB.ToUpper())
     // Your code goes here

The regular expression engine in .NET is extremely good. but is not the most efficient method of simply comparing two strings. The below code is bit of a overkill.

var pattern = "Regex pattern.";
var string_value = "Hi! This is a test.";
var b = Regex.IsMatch(string_value, pattern);
Console.WriteLine("{0}", b);

However there is a much efficient way to compare strings using String.Compare method.

if (String.Compare(testStringA, testStringB, true) == 0)
      // Your code goes here

The below comparison does the same thing, but it is the most efficient.

if (String.Equals(StringA, StringB, StringComparison.OrdinalIgnoreCase))
	// Your code goes here                

More information is on ordinal string comparison is below.



String Literals and String Constants

I mentioned previously that string literals create a new string object. It doesn’t mean that you should not be using string literals at all. For example using string literals directly in your code assist readability of your code and have better performance than using string constants. Having said that if you have number of strings being used in you code over and over again, it is still better to define the string constants as the performance gain I mentioned above is not really significant. As you know changing strings in many places is often painful than declare it once, and use it wherever and wherever you want is lot easier.

private const string testString = "sampleString";
StringA = testString;

StringA = “sampleString” //efficient




The above are some of the examples that I tried by my self to prove better and efficient way of string manipulations. However there are many other string manipulations techniques which I haven’t mentioned here and worth trying. For example splitting strings, replacing strings, removing strings etc. It is very easy to check these operations, and all you need is micro-benchmarking fascilty. I use System.Diagnostics.Stopwatch class for benchmarking but there are other techniqes as well.

As a final note always be mindful when perform codding, and use these techinques so you would write efficient C# code.


Tips for converting VB.NET to C#

29. May 2010

As soon as anyone sees this title, most of you would think, ‘Ah..that’s easy, use a tool and it will do the job for you’. Recently I have been given a small task to convert some VB.NET code to C#. I’m not good at writing VB.NET so this was a challenge. 


As any other C# developer, I decided to use a conversion tool. I copied and paste the VB.NET code I needed to translate into C#.NET. Another approach would be to use the Refactor. If you can include this function in an assembly you can easily view the C# equivalent of the code using Refactor. I got it translated using a conversion tool. However I saw the translated code was bit confusing. Which also bring me to the next topic that you should not blindly translate your VB.NET code to C#. 


Take a look at this example.

Public Function GetAppGUID(ByVal sectionId As String) As String 
    Dim hexString As String = Nothing 
    Dim i As Integer 
    Dim guidlen As Integer 
    guidlen = 16 
    If sectionId.Length < guidlen Then 
        sectionId = sectionId & New String(" ".Chars(0), guidlen - sectionId.Length) 
    End If 
    For i = 1 To guidlen 
        hexString = hexString & Hex(Asc(Mid(sectionId, i, 1))) 
    GetAppGUID = hexString 
End Function
public string GetAppGUID(string sectionId) 
	string hexString = null; 
	int i = 0; 
	int guidlen = 0; 
	guidlen = 16; 
	if (sectionId.Length < guidlen) { 
	        sectionId = sectionId + new string(" ".Chars(0), guidlen - sectionId.Length); 
	for (i = 1; i <= guidlen; i++) { 
	     hexString = hexString + Conversion.Hex(Strings.Asc(Strings.Mid(sectionId, i, 1))); 
	return hexString; 

As you can see the converted function require Microsoft.VisualBasic.dll for Conversion and Asc functions. In some cases yes you would use this dll but most of the time you don’t have to. You can easily write this function in C# using same line of code but not using Microsoft.VisualBasic.dll

private string GetAppGUID(string sectionId) 
        string hexString = null; 
        int i = 0; 
        int guidLength = 0; 
        guidLength = 16; 
        if (sectionId.Length < guidLength) 
            sectionId = sectionId + new string(" "[0], guidLength - sectionId.Length); 
        foreach (char c in sectionId) 
            int tmp = c; 
            hexString += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString())) 
        return hexString; 

So this is my opinion.

a. If you ever have to convert VB.NET to C#, you may use a conversion tool or Reflector but make sure you are not blindly converting the code.

b. If the converted code require Microsoft.VisualBasic.dll, then try to avoid this dll. Refactor your code to use the equivalent C# syntax. To include a completely different dll in your C# project for just a small method/syntax change is not really worth it.

c. Don’t take your task as “hell this is not the language I’m familiar with”. See this opportunity is an excellent way to learn another language. It is quite common in the industry that you get to do these types of tasks often. Which also prove you are an all-rounded developer,