1、主建構函式(primary constructors)
主建構函式給類中的變數賦值
before
public class pointafter}
public class point(int x, int y)2、自動屬性賦值(auto properties)
before
class pointafterpublic int y
public point()
}
class pointusing會把引用類的所有靜態方法匯入到當前命名空間= 100;
public int y = 100;
}
before
public double a }after
using system.math;before...public double a }
public double distanceafter}
public double distance => math.sqrt((x * x) + (y * y));初看起來像lambda表示式,其實和lambda無關係。
before
public point move(int dx, int dy)after
public point move(int dx, int dy) => new point(x + dx, y + dy);這個和property expressions類似
before
do(someenum.toarray());after...public void do(params int values)
do(someenum);以前params是只可以修飾array型別的引數,現在多了一些型別public void do(params ienumerablepoints)
before
if (points != null)afterreturn -1;
var bestvalue = points?.firstordefault()?.x ?? -1;這個少了好幾行**,贊啊
before
var x = myclass.create(1, "x");after...public myclasscreate(t1 a, t2 b)
var x = new myclass(1, "x");before
int x;afterint.tryparse("123", out x);
int.tryparse("123", out int x);說實話,這個很常用。
原文:
c 6 0新特性(一)
接近年底了,基本上沒什麼活了,就學點新東西,就想著了解下c 6.0的新特性。在code project上看到了一篇不錯的文章,就準備翻譯一下,順便照著學習學習。廢話不多說,直奔主題。眾所周知,c 6.0 是在visual studio 2015中引入的。在其他的幾個版本中同樣引入一些特性,比如在c ...
c 6 0新特性(一)
接近年底了,基本上沒什麼活了,就學點新東西,就想著了解下c 6.0的新特性。在code project上看到了一篇不錯的文章,就準備翻譯一下,順便照著學習學習。廢話不多說,直奔主題。眾所周知,c 6.0 是在visual studio 2015中引入的。在其他的幾個版本中同樣引入一些特性,比如在c ...
C 6 0 新特性 二
有過正確實現結構經驗的所有 net 開發人員無疑都為乙個問題所困擾 需要使用多少語法才能使型別固定不變 為 net 標準建議的型別 此問題實際上是唯讀屬性存在的問題 定義為唯讀的支援字段 建構函式內支援欄位的初始化 屬性的顯式實現 而非使用自動屬性 返回支援欄位的顯式 getter 實現 所有這一切...