C# Code Optimizing Tutorial with Examples
Today, We want to share with you C# Code Optimizing Tutorial with Examples.
In this post we will show you Optimizing C# Code, hear for Code Optimization Techniques we will give you demo and example for implement.In this post, we will learn about Tips to Improve Performance of C# Code with an example.
Optimizing C# code
In this post,I will list some of useful technique for Optimizing C# code.
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.
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 + “Astha”;
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.
return StudentList + “jaydeep”;
Now,In the above code there is no need to cast types with [php]List<>[/php]. 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 [php](&&)[/php] and/or the double-or notation (||), (in Visual Basic they are [php]AndAlso and OrElse)[/php].
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 [php]||[/php] 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 [php]obj[/php] is null, with the [php]&& operator, obj.getCount() [/php]will not execute. If the [php]&&[/php] operator is replaced with [php]&, obj.getCount()[/php] will run even if [php]obj[/php] is already known to be null, and it will result in an exception.
3. Knowing when to use StringBuilder over String
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#.
I hope you get an idea about Optimizing C# Code Tips.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.