Several minor abuses should be avoided in the development of C-SHARP

Source: Internet
Author: User

  A string and StringBuilder

A small number of string operations should not be used StringBuilder.

Because a string is not an object, each operation generates a new string object for the superposition of strings. So for a lot of string operations, we use Stringbuilde. But it seems that a lot of people know this, so as long as the strings are added, no matter 3721 is StringBuilder. In fact, this is a misunderstanding, here to pay attention to the word "mass", a small number of string operations with StringBuilder rather than outweigh the gains.

For example, the following two ways:

String sql = "INSERT into table (,,,)";
SQL + = "VALUES (')";
StringBuilder sb = new StringBuilder ();
Sb. Append ("INSERT into table (,,,)");
Sb. Append ("VALUES (')");
string s = sb. ToString ();

It is advisable to adopt the first formulation. Because there are only two strings added, not much. If you use StringBuilder, you end up with a string, which results in a poor performance.

  Second caution with abnormal

Because the program throws an exception, an exception object is created, and then the application catches the exception in the catch block, resulting in a very high performance impact.

Like what:

int OrderID;
Try
{
OrderID = Convert.ToInt32 (This.txtOrderID.Text);
}
catch (Exception e)
{
OrderID =.
}

Such code is not appropriate, resulting in a very high performance overhead. You should write the JS code on the client first to verify that the Txtorderid meets the requirements.

  Three about session, Request

Session:

Many people like to use the session to save the values of persistent objects. However, when your program is deployed on multiple servers to do load balancing, you may not get the value of the variables saved by the session. In addition, since the session is a server-side built-in object, every session object is opened, and resources are occupied on the server.

Request:

When you pass values between pages, you see a lot of people writing code like this:

String CustomerName = this. request["Txtcustomername"]. ToString ();

In fact, the back of ToString () superfluous. This. Request["..."] is a string type, no need to go to ToString () again.

Write so much today, and have other questions to add.



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.