看到標題,是否是認爲我把標題寫錯了?是的,C# 8.0還未正式發佈,在官網它的最新版本仍是Preview 5,通往C#9的漫長道路卻已經開始.前寫天收到了活躍在C#一線的BASSAM ALUGILI
給我分享C# 9.0新特性,我在他文章的基礎上進行翻譯,但願能對你們有所幫助.ios
這是世界上第一篇關於C#9候選功能的文章。閱讀完本文後,你將會爲將來可能遇到的C# 9.0新特性作好更充分的準備。git
這篇文章基於,github
此次引入一組新類型(nint,nuint,nfloat等)'n'表示native(原生)
,該特性容許聲明一個32位或64位的數據類型,這取決於操做系統的平臺類型。express
nint nativeInt = 55; take 4 bytes when I compile in 32 Bit host. nint nativeInt = 55; take 8 bytes when I compile in 64 Bit host with x64 compilation settings.
xamarin中已存在相似的概念,編程
這個功能我等待了很長時間,Records是一種輕量級的不可變類型,它能夠是方法,屬性,運算符等,它容許咱們進行結構的比較, 此外,默認狀況下,Records屬性是隻讀的。c#
Records能夠是值類型或引用類型。數組
Example安全
public class Point3D(double X, double Y, double Z); public class Demo { public void CreatePoint() { var p = new Point3D(1.0, 1.0, 1.0); } }
這些代碼會被編譯器轉化以下形式.編程語言
public class Point3D { private readonly double <X>k__BackingField; private readonly double <Y>k__BackingField; private readonly double <Z>k__BackingField; public double X {get {return <X>k__BackingField;}} public double Y{get{return <Y>k__BackingField;}} public double Z{get{return <Z>k__BackingField;}} public Point3D(double X, double Y, double Z) { <X>k__BackingField = X; <Y>k__BackingField = Y; <Z>k__BackingField = Z; } public bool Equals(Point3D value) { return X == value.X && Y == value.Y && Z == value.Z; } public override bool Equals(object value) { Point3D value2; return (value2 = (value as Point3D)) != null && Equals(value2); } public override int GetHashCode() { return ((1717635750 * -1521134295 + EqualityComparer<double>.Default.GetHashCode(X)) * -1521134295 + EqualityComparer<double>.Default.GetHashCode(Y)) * -1521134295 + EqualityComparer<double>.Default.GetHashCode(Z); } } Using Records: public class Demo { public void CreatePoint() { Point3D point3D = new Point3D(1.0, 1.0, 1.0); } }
Records迎合了基於表達式形式編程的特性,使得咱們能夠這樣使用它.ide
var newPoint3D = Point3D.With(x: 42);
這樣咱們建立的新Point(new Point3D)就像現有的一個(point3D)同樣並把X的值更改成42。
這個特性於基於pattern matching
也很是有效,我會在個人下一篇文章中介紹這一點.
那麼咱們爲何要使用Records而不是用結構體呢?爲了回答這些問題,我引用了了Reddit的一句話:
「結構體是你必需要有一些約定來實現的東西。你沒必要手動地去讓它只讀,你也不用去實現他們的比較邏輯,但若是你不這樣作,那你就失去了使用結構體的意義,編譯器不會強制執行這些約束"。
Records類型由是編譯器實現,這意味着您必須知足全部這些條件而且不能錯誤, 所以,它們不只能夠減小重複代碼,還能夠消除一大堆潛在的錯誤。
此外,這個功能在F#中存在了十多年,其餘語言如(Scala,Kotlin)也有相似的概念。
F#
type Greeter(name: string) = member this.SayHi() = printfn "Hi, %s" name
Scala
class Greeter(name: String) { def SayHi() = println("Hi, " + name) }
Kotlin
class Greeter(val name: String) { fun sayhi() { println("Hi, ${name}"); } }
在沒有Records以前,咱們要實現相似的功能,C#代碼要這麼寫
C#
public class Greeter { private readonly string _name; public Greeter(string name) { _name = name; } public void Greet() { Console.WriteLine($ "Hello, {_name}"); } }
有了Records以後,咱們能夠將C#代碼大大地減小了,
ublic class Greeter(name: string) { public void Greet() { Console.WriteLine($ "Hello, {_name}"); } }
Less code! = I love it!
此功能的靈感來自Haskell,它是我最喜歡的功能之一。正如我兩年前在我文章中所說,C#將實現更多的函數式編(FP)程概念,Type Classes就是FP概念之一。在函數式編程中,Type Classes容許您在類型上添加一組操做,但不實現它。因爲實現是在其餘地方完成的,這是一種多態,它比面向對象編程語言中的class更靈活。
Type Classes和C#接口具備類似的用途,但它們的工做方式有所不一樣,在某些狀況下,因爲處理固定類型而不是繼承層次結構,所以Type Classes更易於使用。
此這特性最初與「extending everything」功能一塊兒引入,您能夠將它們組合在一塊兒,如Mads Torgersen給出的例子所示。
我引用了官方提案中的一些結論:
「通常來講,」shape「(shape是Type Classes的一個新的關鍵字)聲明很是相似於接口聲明,除了如下狀況,
Haskell中 Type Classes示例。
class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool
「Eq」是類名,而==,/ =是類中的操做。類型「a」是類「Eq」的實例。
若是咱們將上述例子用C#接口實現將會是這樣.
interface Num<A> { A Add(A a, A b); A Mult(A a, A b); A Neg(A a); } struct NumInt : Num<int> { public int Add(int a, int b) => a + b; public int Mult(int a, int b) => a * b; public int Neg(int a) => -a; }
若是咱們用Type Classes實現C# 功能會是這樣
shape Num<A> { A Add(A a, A b); A Mult(A a, A b); A Neg(A a); } instance NumInt : Num<int> { int Add(int a, int b) => a + b; int Mult(int a, int b) => a * b; int Neg(int a) => -a; }
經過上面例子,能夠看到接口和shape的語法相似 ,那咱們再來看看Mads Torgersen給出的例子
Note:shape不是一種類型。相反,shape的主要目的是用做通用約束,限制類型參數以具備正確的形狀,同時容許通用聲明的主體使用該形狀,
public shape SGroup<T> { static T operator +(T t1, T t2); static T Zero {get;} }
這個聲明說若是一個類型在T上實現了一個+運算符而且具備0靜態屬性,那麼它能夠是一個SGroup
給int添加靜態成員Zero
public extension IntGroup of int: SGroup<int> { public static int Zero => 0; }
定義一個AddAll方法
public static AddAll<T>(T[] ts) where T: SGroup<T> // shape used as constraint { var result = T.Zero; // Making use of the shape's Zero property foreach (var t in ts) { result += t; } // Making use of the shape's + operator return result; }
讓咱們用一些整數調用AddAll方法,
int[] numbers = { 5, 1, 9, 2, 3, 10, 8, 4, 7, 6 }; WriteLine(AddAll(numbers)); // infers T = int
這就是Type class 的妙處,慢慢消化感覺一下??
引入更簡單的語法來建立初始化的Dictionary <TKey,TValue>對象,而無需指定Dictionary類型名稱或類型參數。使用用於數組類型推斷的現有規則推斷字典的類型參數。
// C# 1..8 var x = new Dictionary <string,int> () { { "foo", 4 }, { "bar", 5 }}; // C# 9 var x = ["foo":4, "bar": 5];
該特性使C#中的字典工做更簡單,並刪除冗餘代碼。此外,值得一提的是,在F#和Swift等其餘編程語言中也使用了相似的字典語法。
容許params語法使用Span
新的語法以下,
void Foo(params Span<int> values);
到目前爲止,在C#中不容許在結構體聲明中使用無參構造函數,在C#9中,將刪除此限制。
StackOverflow示例
public struct Rational { private long numerator; private long denominator; public Rational(long num, long denom) { /* Todo: Find GCD etc. */ } public Rational(long num) { numerator = num; denominator = 1; } public Rational() // This is not allowed { numerator = 0; denominator = 1; } }
其實CLR已經容許值類型數據具備無參構造函數,只是C# 對這個功能進行了限制,在C# 9.0中可能會消除這種限制.
這些提供了一種通用且安全的機制,用於向C#語言聲明固定大小的緩衝區。
目前,用戶能夠在不安全的環境中建立固定大小的緩衝區。可是,這須要用戶處理指針,手動執行邊界檢查,而且只支持一組有限的類型(bool,byte,char,short,int,long,sbyte,ushort,uint,ulong,float和double)。該特性引入後將使固定大小的緩衝區變得安全安全,以下例所示。
能夠經過如下方式聲明一個安全的固定大小的緩衝區,
public fixed DXGI_RGB GammaCurve[1025];
該聲明將由編譯器轉換爲內部表示,相似於如下內容,
[FixedBuffer(typeof(DXGI_RGB), 1024)] public ConsoleApp1.<Buffer>e__FixedBuffer_1024<DXGI_RGB> GammaCurve; // Pack = 0 is the default packing and should result in indexable layout. [CompilerGenerated, UnsafeValueType, StructLayout(LayoutKind.Sequential, Pack = 0)] struct <Buffer>e__FixedBuffer_1024<T> { private T _e0; private T _e1; // _e2 ... _e1023 private T _e1024; public ref T this[int index] => ref (uint)index <= 1024u ? ref RefAdd<T>(ref _e0, index): throw new IndexOutOfRange(); }
它是關於定義一種新的字符串類型UTF8String,它將是,
System.UTF8String myUTF8string ="Test String";
此功能用於解決默認接口方法中的覆蓋衝突問題:
interface I1 { void M(int) { } } interface I2 { void M(short) { } } interface I3 { override void I1.M(int) { } } interface I4 : I3 { void M2() { base(I3).M(0) // Which M should be used here? What does this do? } }
更多信息,
您已經閱讀了第一個C#9候選特性。正如您所看到的,許多新功能受到其餘編程語言或編程範例的啓發,而不是自我創新,這些特性大部分在在社區中獲得了普遍承認,因此引入C# 後應該也會給你們帶來不錯的體驗.
原文 : https://www.c-sharpcorner.com/article/candidate-features-for-c-sharp-9/