Thursday, April 9, 2009

8 Ways to Optimize Your C# Application 1/2

Ways to optimize C# code in .NET development

Stuck with your code? Need higher code performance?Code optimization is an important aspect of writing an efficient C# application for your .NET development. The following 8 tips will help you increase the efficiency of your C# applications.

1. StringBuilder: knowing when to use
The main difference between string and StringBuilder is that using the StringBuilder to modify a string without creating a new object can boost performance when concatenating many strings in a loop. If you have a loop that will make modification to a single string for many iterations, then a StringBuilder class is much faster than a string type. However, if you just want to append/insert something to a string, then a StringBuilder will be over power. In this case, a simple string type will work well.

2. Strings Compare: Non-Case Sensitive
When we compare two strings, usually we use the built-in equals() method for the comparison. In case of ignoring the cases, people use ToLower() or ToUpper() methods. However, ToLower() or ToUpper() are bottlenecks in performance. Here is a way to increase your applications: use string.Compare(str1, str2, true) == 0; for comparing two strings ignoring cases. If str1 and str2 are equal ignoring cases then result will return 0.

3. Strings Compare: "string.Empty "
It is normal to see people use .equals("") for comparing an empty string. A popular practice is that checking a string's length to be 0 is faster than comparing it to an empty string. Using string.Empty will not make anysignificant performance improvement, but bringing you the readable optimization.

4. Using "List<>" instead of using "ArrayList"
Both List<> and ArrayList can store objects within the same list. The difference is that ArrayList can store multiple types of objects while List<> has to specify one type of object. Also, when you extract the object from the list, the List<> does not need to cast the variable to a proper type while ArrayList has to. Therefore, if
you are keeping the same type of variables in one ArrayList, you can gain a performance boost by using List<> instead.

(to be continue...)
8 Ways to Optimize Your C# Application 2/2


2 Responses to "8 Ways to Optimize Your C# Application 1/2"
  1. Nabila Tamsin said...
    November 13, 2014 at 3:19 AM

    Thanks for your nice article. I have found a fine article also. I want to share it

    We should follow standard naming convention.
    The variables and methods/functions name should be relevant and small as far as possible.
    Use X++ instead of X=X+1. Both returns the same result but X++ use fewer characters.

    for more....

    Hope it will help us.

  2. sf said...
    June 9, 2016 at 2:29 PM

    I like article as it have lot of useful info, just want to share one article related to code optimization, the thing which I like about the article is that author give a sample code, by executing which we can clearly see the execution timing difference of stringbuilder append method and string concatenation. That's the link of the article

Post a Comment



Copyright 2009 All Rights Reserved Revolution Two Lifestyle theme by Brian Gardner | Blogger template converted & enhanced by eBlog Templates