Efficient C # Coding Optimization

Source: Internet
Author: User

There are many coding methods to achieve the same purpose as Rome. It would be better if we optimize it on the basis of implementation.
(1) foreach has better execution efficiency than for statements.
The average time of foreach is only 30% of that of for. If both of them can be used, we recommend that you use foreach. In addition, writing data with for takes about 10 times as long as reading data.
(2) Avoid using ArrayList.
Because any Object to be added to the ArrayList must be of the System. Object type and the data to be removed from the ArrayList must be of the actual type. We recommend that you use a generic set to avoid packing and unpacking, thus improving performance.
(3) HashTable can be used to store a small amount of data.
(4) Declare constants for string containers. Do not enclose characters in double quotation marks ("") directly.
(5) do not use UpperCase or Lowercase to convert strings and use String. Compare instead. It can be case-insensitive for comparison.
(6) Use StringBuilder to replace the string connector "+ ".
(7) avoid declaring variables in the circulating body. Declare variables in the circulating body and initialize them in the circulating body.
(8) catch the specified Exception. Do not use general System. Exception.
(9) do not use exceptions to control program processes.
Capture exceptions are well known for performance loss. If statements are recommended to control the program flow.
(10) use using or try/finally to clear resources.
At present, we have sorted out so much.

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.