C # tips-2

Source: Internet
Author: User

I wrote some tips for summary in normal development last time. I will continue to add some more.

1. simplify writing with multiple using.

We often encounter the need to use multiple using objects, at this time we may writeCode:

  Using(Class1 C1= NewClass1 (someparam1 ))
{
Using(Class2 C2= NewClass2 (someparam2 ))
{
//Todo: Add function code here
}
}

In fact, we can simplify writing:

  Using(Class1 C1= NewClass1 (someparam1), class2 C2= NewClass2 (someparam2 ))
{
//Todo: Add function code here
}

2. In many cases, we may write the following code to determine whether a string is one of several specified strings:

   code highlighting produced by actipro codehighlighter (freeware) 
http://www.CodeHighlighter.com/
--> bool nameexists ( string name) {
return name = " zhangsan " || name = " Li Si " || name = " Wang Wu " || name = " Zhao Liu ";
}

However, there will actually be a simpler way of writing in the 3.0 s:

 
  BoolNameexists (StringName ){
Return New[] {"Zhang San", "Li Si", "Wang Wu", "Zhao Liu"}. Contains (name );
}

Or, if this judgment is frequent, we can also use the extension method:

  Public     Static    Bool  In (  This     String  Name,  Params     String  [] Names)
{
Foreach ( String Specialname In Names)
{
If (Name ! = Specialname) Return False ;
}
Return True ;
}

VaR name = " Test " ;
Console. Write (name. In ( " Zhang San " , "Li Si", "Wang Wu", "Zhao Liu ");

3. when comparing strings, we sometimes ignore case-sensitivity comparisons. You may write the code as follows: str1.toupper () = str2.toupper (). However, the string object provides many better methods, for example, str1.equals (str2, stringcomparison. ordinalignorecase)

4 .?? This feature is used to specify the default value when the object is null. We can use this feature to bind multiple default values.

String result = var1 ?? Var2 ?? Var3 ?? String. empty;

5. This usage is not recommended, but you can name classes or methods in non-English languages such as Chinese, such:

 
  Public StringMethod 1 (){
Return"Xxxxxxx ";
}

In addition, you can even use escape characters to name them:

 
  Public StringTest \ u9999 ha (){
Return "Aaa";
}

The actual method called is: Test xiangha ();

6. in C #, we can also create anonymous intra-row functions similar to JS, such:

  VaR var1= NewFunc<String>(()=>{
Return"Test ";
})();

I hope these summaries will also help readers.

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.