Best practices for .Net Performance - I


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>
}


Similar Articles