Write effective C # code (1)

Source: Internet
Author: User

Recently, when I was talking to a classmate, he asked me to check his code. After reading the code for a long time, I recorded it gradually. Now I will summarize it and share it with you, it is very important to compile effective C # code. Although we only implement the code when writing it, when it comes to large-scale projects or international open-source projects, effective C # problems are often encountered. I would like to list several items for your reference today.

First: tostring () is good, but is that true?

We often like to use tostring () for formatting and conversion, and express the object as a string, but how can we apply it effectively? For example, obtain the current time: system. datetime. Now. tostring (). This is a code we are too accustomed to, but there are at least two problems in it. 1) "system." is redundant code, because the using system is added to the namespace. 2) tostring () lacks the ability to obtain a culture-independent (fixed) cultueinfo. What do you think? Kiss...

Second: What are the three-object operators? More concise?

In the past, we used to use if... else... for judgment. Later, we used the three-object operator to simplify if... else... or even replace it. But is there a simpler operator than the three-object operator? Of course, in some cases, there are still some. For example:

If (A> B)
{Return true ;}
Else
{Return function () ;}// function () is a value of the bool type returned for execution.

Return (A> B? True: function)

Simpler Method: return B> = A & function ()

Do you think so? (Of course, the three items are the highest in terms of execution efficiency)

Use vs2010's measurement analysis: when using if... else:

When the preceding expressions are used:

Third: why not use LINQ? Still in foreach?

We all know that foreach is an effective improvement in the for loop, but can't we improve the appearance of LINQ? For example:

String result = "";
Foreach (string s in strlist)
{
Result + = s;
}
 

The above code is simple, but can we do this?

String PAT = strlist. Aggregate ("", (current, S) => current + S );

We often ignore a lot of things. We are not pursuing any perfection, but if the Code makes us feel disgusted, Are you still writing code like that? I will update more later...

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.