


























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.
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.
6) If you are only reading from the XML object, avoid using XMLDocumentt, instead use XPathDocument, which is readonly and so improves performance.
7) Avoid declaring objects/variables inside loops, instead declare the variable once outside the loop and initialize them inside.
8) Always catch the Specific exceptions instead of generic System.Exception.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。