C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Best practices to Improve your .net codes performance


Posted Date: 30 Jun 2009    Resource Type: Articles    Category: .NET Framework
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 15



The below mentioned practices can Improve your .net codes performance

1. DateTime.Now() method is commonly used to get the current time.
The value returned by the Now() method is in the current machine
time-zone context. A common practice is to convert time that is going
to be stored or sent between machines into Universal (UCT) time.
When daylight savings time is a possibility, this can introduce bugs.
For example –

DateTime date = DateTime.Now().ToUniversalTime();

The value that results from the above code will be off by an hour if
called during the extra hour that occurs during the daylight savings
time switch in the fall. To fi x this, a best practice is to call DateTime.
UtcNow()
instead of calling DateTime.Now(), and then converting it
to universal time.

DateTime date = DateTime.UtcNow();

This code will always have the 24-hour-day perspective, and may
then be safely converted to local time.


2. When using Add/Subtract functions with DateTime, take into account
the daylight savings time.

For example –

DateTime dt = DateTime.Parse(“Nov 4, 2007 12:00:00 AM”);
dt = dt.AddHours(3.0);


The expected result will be 03:00:00 AM. This result may seem correct
at fi rst; however, on November 04, 2007, at 02:00 AM PST, the daylight
savings time change took effect. So the correct answer should have
been Nov 4, 2007, 02:00:00 AM. To avoid this, the following syntax
can be used:

dt = dt.ToUniversalTime().AddHours(3.0).ToLocalTime();

This converts the local time to UTC, performs the calculation, and
then converts back to local time.


3. Do not compare strings by converting them to uppercase or lowercase, use
String.Compare instead, which can ignore the case and compare.


4 .Avoid the use of ArrayList - Objects added to the ArrayList are added
as System.Object and when retrieving values back from the ArrayList,
these objects have to be unboxed to return the actual value type. So it is
recommended to use the custom typed collections instead of ArrayList.

For example - string objects can be stored in StringCollection
(System.Collection.Specialized)


5. Always catch the specifi c exceptions instead of generic System
Exception.


6. While copying data from one DataTable to another, the simple way
is to clone an existing DataTable, loop through all rows of source

DataTable and copy data column by column and add row to the
destination DataTable. But this is not a feasible solution if there are
millions of records.

Hence, a better way is to use the DataTable.
ImportRow()
method.

The ImportRow() method of the DataTable
copies a row into a DataTable with all of the properties and data of the
row.
It actually calls NewRow() method on destination DataTable with
current table schema and sets DataRowState to "Added".

Benefits

* Performance of the application can be improved.

* Some of these tips will give more accurate results.

* Quality of coding can be improved.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Best practices to Improve your .net codes performance  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Guide To ASP.NET Cookies
Previous Resource: General considerations to improve Performance of .NET Applications
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use