欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#9.0 新特性簡(jiǎn)介

 更新時(shí)間:2020年10月13日 10:39:34   作者:碼農(nóng)阿宇  
這篇文章主要介紹了C#9.0 新特性簡(jiǎn)介,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

CandidateFeaturesForCSharp9

看到標(biāo)題,是不是認(rèn)為我把標(biāo)題寫錯(cuò)了?是的,C# 8.0還未正式發(fā)布,在官網(wǎng)它的最新版本還是Preview 5,通往C#9的漫長(zhǎng)道路卻已經(jīng)開始.前寫天收到了活躍在C#一線的BASSAM ALUGILI給我分享C# 9.0新特性,我在他文章的基礎(chǔ)上進(jìn)行翻譯,希望能對(duì)大家有所幫助.

這是世界上第一篇關(guān)于C#9候選功能的文章。閱讀完本文后,你將會(huì)為未來可能遇到的C# 9.0新特性做好更充分的準(zhǔn)備。

這篇文章基于,

C# 9.0候選新特性

原生大小的數(shù)字類型

這次引入一組新類型(nint,nuint,nfloat等)'n'表示native(原生),該特性允許聲明一個(gè)32位或64位的數(shù)據(jù)類型,這取決于操作系統(tǒng)的平臺(tái)類型。

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中已存在類似的概念,

xamarin原生類型

Records and Pattern-based With-Expression

這個(gè)功能我等待了很長(zhǎng)時(shí)間,Records是一種輕量級(jí)的不可變類型,它可以是方法,屬性,運(yùn)算符等,它允許我們進(jìn)行結(jié)構(gòu)的比較, 此外,默認(rèn)情況下,Records屬性是只讀的。

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); 
 } 
} 

這些代碼會(huì)被編譯器轉(zhuǎn)化如下形式.

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迎合了基于表達(dá)式形式編程的特性,使得我們可以這樣使用它.

var newPoint3D = Point3D.With(x: 42); 

這樣我們創(chuàng)建的新Point(new Point3D)就像現(xiàn)有的一個(gè)(point3D)一樣并把X的值更改為42。

這個(gè)特性于基于pattern matching也非常有效,我會(huì)在我的下一篇文章中介紹這一點(diǎn).

那么我們?yōu)槭裁匆褂肦ecords而不是用結(jié)構(gòu)體呢?為了回答這些問題,我引用了了Reddit的一句話:

“結(jié)構(gòu)體是你必須要有一些約定來實(shí)現(xiàn)的東西。你不必手動(dòng)地去讓它只讀,你也不用去實(shí)現(xiàn)他們的比較邏輯,但如果你不這樣做,那你就失去了使用結(jié)構(gòu)體的意義,編譯器不會(huì)強(qiáng)制執(zhí)行這些約束"。

Records類型由是編譯器實(shí)現(xiàn),這意味著您必須滿足所有這些條件并且不能錯(cuò)誤, 因此,它們不僅可以減少重復(fù)代碼,還可以消除一大堆潛在的錯(cuò)誤。

此外,這個(gè)功能在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之前,我們要實(shí)現(xiàn)類似的功能,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!

Type Classes

此功能的靈感來自Haskell,它是我最喜歡的功能之一。正如我兩年前在我文章中所說,C#將實(shí)現(xiàn)更多的函數(shù)式編(FP)程概念,Type Classes就是FP概念之一。在函數(shù)式編程中,Type Classes允許您在類型上添加一組操作,但不實(shí)現(xiàn)它。由于實(shí)現(xiàn)是在其他地方完成的,這是一種多態(tài),它比面向?qū)ο缶幊陶Z言中的class更靈活。

Type Classes和C#接口具有相似的用途,但它們的工作方式有所不同,在某些情況下,由于處理固定類型而不是繼承層次結(jié)構(gòu),因此Type Classes更易于使用。

此這特性最初與“extending everything”功能一起引入,您可以將它們組合在一起,如Mads Torgersen給出的例子所示。

我引用了官方提案中的一些結(jié)論:

“一般來說,”shape“(shape是Type Classes的一個(gè)新的關(guān)鍵字)聲明非常類似于接口聲明,除了以下情況,

  • 它可以定義任何類型的成員(包括靜態(tài)成員)
  • 可以通過擴(kuò)展實(shí)現(xiàn)
  • 只能在指定的地方當(dāng)作一種類型使用(作用域)“

Haskell中 Type Classes示例。

class Eq a where   
(==) :: a -> a -> Bool  
(/=) :: a -> a -> Bool 

“Eq”是類名,而==,/ =是類中的操作。類型“a”是類“Eq”的實(shí)例。

如果我們將上述例子用C#接口實(shí)現(xiàn)將會(huì)是這樣.

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實(shí)現(xiàn)C# 功能會(huì)是這樣

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的語法類似 ,那我們?cè)賮砜纯碝ads Torgersen給出的例子

Note:shape不是一種類型。相反,shape的主要目的是用作通用約束,限制類型參數(shù)以具有正確的形狀,同時(shí)允許通用聲明的主體使用該形狀,

原始來源

public shape SGroup<T>   
{   
 static T operator +(T t1, T t2);   
 static T Zero {get;}    
}

這個(gè)聲明說如果一個(gè)類型在T上實(shí)現(xiàn)了一個(gè)+運(yùn)算符并且具有0靜態(tài)屬性,那么它可以是一個(gè)SGroup

給int添加靜態(tài)成員Zero

public extension IntGroup of int: SGroup<int>  
{  
 public static int Zero => 0;  
} 

定義一個(gè)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;  
} 

讓我們用一些整數(shù)調(diào)用AddAll方法,

int[] numbers = { 5, 1, 9, 2, 3, 10, 8, 4, 7, 6 };    
WriteLine(AddAll(numbers)); // infers T = int 

這就是Type class 的妙處,慢慢消化感受一下??

Dictionary Literals

引入更簡(jiǎn)單的語法來創(chuàng)建初始化的Dictionary <TKey,TValue>對(duì)象,而無需指定Dictionary類型名稱或類型參數(shù)。使用用于數(shù)組類型推斷的現(xiàn)有規(guī)則推斷字典的類型參數(shù)。

// C# 1..8  
var x = new Dictionary <string,int> () { { "foo", 4 }, { "bar", 5 }};  
// C# 9  
var x = ["foo":4, "bar": 5]; 

該特性使C#中的字典工作更簡(jiǎn)單,并刪除冗余代碼。此外,值得一提的是,在F#和Swift等其他編程語言中也使用了類似的字典語法。

Params Span

允許params語法使用Span 這個(gè)幫助來實(shí)現(xiàn)沒有任何堆分配的params參數(shù)傳遞。此功能可以使params方法的使用更加高效。

新的語法如下,

void Foo(params Span<int> values); 

struct允許使用無參構(gòu)造函數(shù)

到目前為止,在C#中不允許在結(jié)構(gòu)體聲明中使用無參構(gòu)造函數(shù),在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;  
 }  
} 

鏈接到StackOverflow示例

其實(shí)CLR已經(jīng)允許值類型數(shù)據(jù)具有無參構(gòu)造函數(shù),只是C# 對(duì)這個(gè)功能進(jìn)行了限制,在C# 9.0中可能會(huì)消除這種限制.

固定大小的緩沖區(qū)

這些提供了一種通用且安全的機(jī)制,用于向C#語言聲明固定大小的緩沖區(qū)。

目前,用戶可以在不安全的環(huán)境中創(chuàng)建固定大小的緩沖區(qū)。但是,這需要用戶處理指針,手動(dòng)執(zhí)行邊界檢查,并且只支持一組有限的類型(bool,byte,char,short,int,long,sbyte,ushort,uint,ulong,float和double)。該特性引入后將使固定大小的緩沖區(qū)變得安全安全,如下例所示。

可以通過以下方式聲明一個(gè)安全的固定大小的緩沖區(qū),

public fixed DXGI_RGB GammaCurve[1025]; 

該聲明將由編譯器轉(zhuǎn)換為內(nèi)部表示,類似于以下內(nèi)容,

[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();  
} 

Uft8字符串文字

它是關(guān)于定義一種新的字符串類型UTF8String,它將是,

System.UTF8String myUTF8string ="Test String"; 

Base(T)

此功能用于解決默認(rèn)接口方法中的覆蓋沖突問題:

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?
  }
}

更多信息,

https://github.com/dotnet/csharplang/issues/2337

https://github.com/dotnet/csharplang/blob/master/meetings/2019/LDM-2019-02-27.md

摘要

您已經(jīng)閱讀了第一個(gè)C#9候選特性。正如您所看到的,許多新功能受到其他編程語言或編程范例的啟發(fā),而不是自我創(chuàng)新,這些特性大部分在在社區(qū)中得到了廣泛認(rèn)可,所以引入C# 后應(yīng)該也會(huì)給大家?guī)聿诲e(cuò)的體驗(yàn).

原文 : https://www.c-sharpcorner.com/article/candidate-features-for-c-sharp-9/

到此這篇關(guān)于C#9.0 新特性簡(jiǎn)介的文章就介紹到這了,更多相關(guān)C#9.0 新特性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論