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.