Eleven General usage is the core to improve the performance of the Net Program

Source: Internet
Author: User
For the features of the net program, how can we provide its performance? There are no skills here. Regular operations are the core of improving performance!
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 set types
(Such as stringdictionary, namevaluecollection, and hybridcollection). hashtable can be used 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 for comparison. 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 = "";
Sxml + = "";
Sxml + = "data ";
Sxml + = "";
Sxml + = "";
// Recommended
Stringbuilder sbxml = new stringbuilder ();
Sbxml. append ("");
Sbxml. append ("");
Sbxml. append ("data ");
Sbxml. append ("");
Sbxml. append ("");

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: capture the specified exception. Do not use general system. Exception.
// Avoid
Try
{
}
Catch (exception exc)
{
}
// Recommended
Try
{
}
Catch (system. nullreferenceexception exc)
{
}
Catch (system. argumentoutofrangeexception exc)
{
}
Catch (system. invalidcastexception exc)
{
}
9: When try... catch... finally is used, the resources occupied, such as connections and file streams, must 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

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.