Optimizing C# code

In this post,I will list some of useful technique for Optimizing C# code.


In my previous posts I have explained various topics like 10 Useful tools for web developers and designer, Sealed Class in C#, Continue keyword in C#.Net, Difference between string and StringBuilder in C#, Difference between a struct and a class in C#, Boxing & Unboxing in C# ,Dynamic Type In C#, Named Arguments In C#.

Now in this post, I will explain about Optimizing C# Application.  Optimization is the process of modifying a code to enhance its performance or efficiency. Code optimization is a truly required an aspect of writing an efficient C#.Net based application. The following tips will help you to enhance the speed and efficiency of your C# code and applications.

1. Replace ArrayList with List<>

ArrayList are benefial when storing multiple types of objects or data within the same list. But if you are storing the same type of variables in one ArrayList, you can gain a more performance by using List<> objects instead.

ArrayList StudentList = new ArrayList();
return (int)StudentList[0] + "Raj";

In the above code it only contains string(Student Name). Using the List<> class is much better. To convert it to a typed List, only the variable types need to be changed.

List<string> StudentList = new List<string>();
return StudentList[0] + "Adam";

Now,In the above code there is no need to cast types with List<>. The performance increase can be especially significant with primitive data types like integers,float,decimal etc.

2. Use && and || operators

When using condition statement like if statements, simply make use of the double-and notation (&&) and/or the double-or notation (||), (in Visual Basic they are AndAlso and OrElse).

Condition statement statements that use & and | must check every condition of the statement and then apply the “and” or “or”. On the other hand, && and || go thourgh the statements one at a time and stop as soon as the condition has either recognized as true or false.

consider the following C# code:

if (obj != null && obj.getCount())

In the above line of code, If obj is null, with the && operator, obj.getCount() will not execute. If the && operator is replaced with &, obj.getCount() will run even if obj is already known to be null, and it will result in an exception.

3. Knowing when to use StringBuilder over String

You may have heard before that a StringBuilder is faster at appending strings together than normal string types.

The fact is StringBuilder is faster mostly with big strings. This means if you have a loop that iterates and add to a single string for many iterations then a StringBuilder class is definitely much faster than a string type.

But if you just want to append some value to a string a single time then a StringBuilder class is overkill. A simple string type variable, in this case, improves on resources use and readability of the C# source code.

4. Comparing Non-Case-Sensitive Strings

Sometimes it is required to compare two string variables,by ignoring the cases. The traditional approach is to convert both strings to all lower case or all upper case and then compare them,

name1.ToLower() == name2.ToLower()

In the above code, calling the function ToLower() is a bottleneck in performace. By instead using the built-in string.Compare() function you can increase the speed of your applications.

You can check if two strings are equal ignoring case would look like below code:

string.Compare(name1, name2, true) == 0 //Ignoring cases

The C# string.Compare function returns an integer that is equal to 0 when the two strings are equal.

5. Smart Try-Catch

Try-Catch statements are useful to catch an exceptions that are beyond the programmers control, such as connecting to the web or a device for example. Using a try statement to keep code “simple” instead of using if statements to avoid error-prone calls makes code incredibly slower. Restructure your source code to require less try statements.

6. Avoid Divisions

C#.Net is comparatevely slow when it comes to division operations.Another way is to replace divisions with a multiplication-shift operation to further optimize C#.


You can also read about ASP.NET,C#.NET, JQUERY, AJAX. I hope this Optimizing C# code technique will save your time and makes coding  easy.
I would like to have feedback for my blog.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *