C# 新語法收集
內聯變量
使用int.tryparst時,先要申明變量,用於out參數
int d;
int.tryparse(s,out d);
使用內聯變量寫法可以如下.功能一樣簡化了寫化
int.tryparse(s,out int d)
$"{val1}-{val2}"
作用如同string.format("{0}-{1}",val1,val2) , $開頭的字符串中的{}內將當做變量解析.
方法內寫方法
可以在方法內部寫一些方法.類似JS的這種在函數中寫一個函數
function showmsg(msg)
{
function validate(){}
}
/*
比如處理一個數字字符串排序,如果只是臨時用一下,可以不必再加個方法,直接在方法內寫個處理方法就行了
*/
// 將一個數字組成的串排序
private string orderlist(string strnum)
{
}
// 給定字符串,返回排序後結果
public string orderstringnum(string str,int ordertype)
{
return this.orderlist(str);
}
// 直接寫在方法內
// 給定字符串,返回排序後結果
public string orderstringnum(string str,int ordertype)
{
string orderlist(string strnum)
{
}
return orderlist(str);
}
// 註意事項:方法前面不能加private public之類,它只能在聲明方法內部使用.和LAMBDA的作用相似,但是它本質是個方法,並不會像LAMBDA那樣寫起來別扭.
C# 新語法收集