Archive for March, 2008

A common function used in coding is to compare two strings, but to ignore whether the characters are upper or lower case. Right off the bat, a simple solution would be to simply convert both strings to either upper or lower case, and then execute the comparison (as shown in the example below). In the example below, the result is return true. However, without the ToUpper() function, the following would return false.

string a = “abcdefg”;
string b = “aBcDeFg”;
if (a.ToUpper() == b.ToUpper())
return true;
else
return false;

However, a more efficient method is to use a predefined C# function in the String class called Compare. Simply pass to this function two strings you wish to compare. The third input is a boolean variable stating whether to compare the cases. The function outputs zero if they are the same.

public static int Compare(
string strA,
string strB,
bool ignoreCase
)

Below is a sample of the Compare function in action on the first example. This example will return true.

string a = “abcdefg”;
string b = “aBcDeFg”;
if (String.Compare(a, b, true) == 0)
return true;
else
return false;

Finally, individually measuring the execution time of each method in repetitious manner indicates that String.Compare is more efficient than sets of strings to converted to upper or lower case before the comparison.

In a web application, there is sometimes a need to store a read only string accessible throughout the project without having to access a database. An easy way to achieve this end goal is to create a key in the web.config file.

An advantage to adding a key to the appSettings of the web.config file is that the key will be accessible to all files in your project. Secondly, if this key should ever change, you can edit the key’s value without the need to recompile! Therefore, the value is semi-constant in that it doesn’t change during execution, but can be easily altered between builds. The key is added within appSettings of configuration in the web.config file.

<configuration>
<appSettings>
</appSettings>
<configuration>

Next is a simple example of how this can be used. Below is the relevant code sample of the web.config file.

<configuration>
<appSettings>
<add key =”supportEmailGroup” value=”support@victorchen.info”/>
</appSettings>
<configuration>

To access the key named supportEmailGroup, use the following code below. In the example below, the supportEmailGroup value is accessed in the code behind.

string supportEmailAddress = ConfigurationSettings.AppSettings["supportEmailGroup"];

It is that easy. The web.config file can hold an arbitrary number of keys. I use the appSettings feature in every project. Some example of where this can be useful is pointing to file directory or connection strings.

As a rule of thumb, avoid using the += operation to concatenate or append to an existing string. The reasoning behind this is the System.String class is immutable (meaning unchanging). This means that when the += operation is used, instead of appending, C# is actually recreating the string. The example below is a string concatenation that should be avoided (in red):

string badExample = “This is an example of how not to append an existing string. “;
badExample += “Using += is not the way to go. “;
badExample += “Instead try the StringBuilder class!”;

An alternative to the += Operation in C# is using System.Text.StringBuilder. The StringBuilder class is mutable and allows setting an initial size to prevent recreating the entire string when appending additional strings. An example on a better performing string operation is shown below (in green):

StringBuilder goodExample = new StringBuilder(”This is an efficent way to build a string. “);
goodExample.Append(”By using a mutable class, it prevent recreation. “);
goodExample.Append(”Perfect!”);

It is important to note that the initial StringBuilder character buffer is a very important aspect to efficiency. Setting a size that is too small will require the StringBuilder class to constantly recreate the entire string, making the operation no better than a += operation. However, making the StringBuilder character buffer too large will be waste on memory. Set the StringBuilder initial size by stating the character buffer on the StringBuilder initialization as demonstrated below (in orange):

StringBuilder goodExample = new StringBuilder(”This is an efficent way to build a string. “, 200);

Finally, using the += operation vs StringBuilder in a single operation will not produce noticeable efficiency differences. However, it does become a more important coding practice when string concatenation operations occur in loops (such as while, for, foreach, etc). And remember to include “using System.Text;” to enable StringBuilder.