Here are some tips of best practices in .NET.
Best practices for .NET Performance
1) Avoid the use of ArrayList. Because any objects added into the Arraylist are added as System.Object and when retrieving values back from the arraylist, these objects are to be unboxed to return the actual valuetype. So it is recommended to use the custom typed collections instead of ArrayList. .NET provides a strongly typed collection class for String in System.Collection.Specialized, namely StringCollection. For other type of class, rename the _ClassType attribute in the attached file to your required type.
2) Reconsider the use of Hashtable instead try other dictionary such as StringDictionary, NameValueCollection, HybridCollection. Hashtable can be used if less number of values are stored.
3) Always declare constants for the string literals you use instead of enclosing them in "".
//AVOID
//
MyObject obj = new MyObject();
obj.Status = "ACTIVE";
//RECOMMENDED
const string C_STATUS = "ACTIVE";
MyObject obj = new MyObject();
obj.Status = C_STATUS;
4) Donot compare strings by converting them to uppercase or lowercase, use String.Compare instead, which can ignore the case and compare.
Ex:
const string C_VALUE = "COMPARE";
if (String.Compare(sVariable, C_VALUE, true) == 0)
{
Console.Write("SAME");
}
5) Avoid String concatenation using + operator, instead use StringBuilder for concatenation.
//AVOID
String sXML = "<parent>";
sXML += "<child>";
sXML += "Data";
sXML += "</child>";
sXML += "</parent>";
//RECOMMENDED
StringBuilder sbXML = new StringBuilder();
sbXML.Append("<parent>");
sbXML.Append("<child>");
sbXML.Append("Data");
sbXML.Append("</child>");
sbXML.Append("</parent>");
6) If you are only reading from the XML object, avoid using XMLDocumentt, instead use XPathDocument, which is readonly and so improves performance.
//AVOID
XmlDocument xmld = new XmlDocument();
xmld.LoadXml(sXML);
txtName.Text = xmld.SelectSingleNode("/packet/child").InnerText;
.
.
.
//RECOMMENDED
XPathDocument xmldContext = new XPathDocument(new StringReader(oContext.Value));
XPathNavigator xnav = xmldContext.CreateNavigator();
XPathNodeIterator xpNodeIter = xnav.Select("packet/child");
iCount = xpNodeIter.Count;
xpNodeIter = xnav.SelectDescendants(XPathNodeType.Element, false);
while(xpNodeIter.MoveNext())
{
sCurrValues += xpNodeIter.Current.Value+"~";
}
7) Avoid declaring objects/variables inside loops, instead declare the variable once outside the loop and initialize them inside.
//AVOID
for(int i=0; i<10; i++)
{
SomeClass objSC = new SomeClass();
.
.
.
}
//RECOMMENDED
SomeClass objSC = null;
for(int i=0; i<10; i++)
{
objSC = new SomeClass();
.
.
.
}
8) Always catch the Specific exceptions instead of generic System.Exception.
//AVOID
try
{
<some logic>
}
catch(Exception exc)
{
<Error handling>
}
//RECOMMENDED
try
{
<some logic>
}
catch(System.NullReferenceException exc)
{
<Error handling>
}
catch(System.ArgumentOutOfRangeException exc)
{
<Error handling>
}
catch(System.InvalidCastException exc)
{
<Error handling>
}

Sunder SinghPosted Feb 27, 2013, 2:06 AM
Nice Article
Sohel RanaPosted Feb 10, 2009, 10:35 PM
Hi, As I know declaring variable inside or outside loop does not affect performance. Compiler generates the same code for two cases. Rather it is recommended to declare variable inside loop either a reader can be confused thinking the variable may be used elsewhere. For details follow the msdn forum link: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/d43aaba5-a58b-4610-bea4-5bc5d6741f98
Gajendra kumarPosted Aug 17, 2007, 12:38 AM
Nice Article, Would you suggest me which one will give best performance in between Form_load Event and Constructor and Why ?? if i am doing intraction to sqlserver database where should i call function for best performance??
Saravanan 0Posted Aug 26, 2006, 8:04 AM
Hai I Want To Know How the Side By Side is possible in 2.0
Mahesh ChandPosted Aug 2, 2006, 11:03 PM
It would be nice to sample code side by side comparing two codes.