How to maximize. net performance _ asp.net skills

Source: Internet
Author: User

From: http://social.microsoft.com/forums/zh-cn/2219/thread/A34FFC62-072F-441C-868C-ED1293A90697

1) Avoid using ArrayList.
Because any Object to be added to the ArrayList must be of the System. Object type. When retrieving data from the ArrayList, it must be split back to the actual type. We recommend that you replace ArrayList with a custom set type .. Net 2.0 provides a new type called generic, which is a strong type. Using a generic set can avoid binning and unboxing and improve performance.

2) use HashTale to replace other dictionary Collection types (such as StringDictionary, NameValueCollection, and HybridCollection). You can use HashTable to store a small amount of data.

3) Declare constants for string containers. Do not enclose the characters in double quotation marks.
// Avoid
//
MyObject obj = new MyObject ();
Obj. Status = "ACTIVE ";

// Recommended
Const string C_STATUS = "ACTIVE ";
MyObject obj = new MyObject ();
Obj. Status = C_STATUS;

4) do not use UpperCase or Lowercase to convert strings and use String. Compare instead. It can be case-insensitive for comparison.
Example:
Const string C_VALUE = "COMPARE ";
If (String. Compare (sVariable, C_VALUE, true) = 0)
{
Console. Write ("SAME ");
}

5) Use StringBuilder instead of the string connector "+ ",.

// 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.
If you only read data from an XML Object and use the read-only XPathDocument instead of XMLDocument, the performance can be improved.
// 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 variables in the cyclic body. Declare variables in the circulating body and initialize them in the cyclic body.

// 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) catch the specified Exception. Do not use general 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>
}

9) When Try... catch... finally is used, the occupied resources such as connections and file streams should be released in finally.
Otherwise, the resources occupied after an error is caught cannot be released.
Try
{
...
}
Catch
{...}
Finally
{
Conntion. close ()
}
10) Avoid using recursive calls and nested loops. Using them will seriously affect performance and will be used only when you have to use them.

11) Use appropriate Caching policies to improve performance

Technorati label: asp.net Performance Improvement
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.