日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 6.0 (C# vNext) 的新功能:Expression Bodied Functions and Properties

發布時間:2023/11/29 C# 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 6.0 (C# vNext) 的新功能:Expression Bodied Functions and Properties 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Expression Bodied Function 它可以用在:
  • methods
  • user-defined operators
  • type conversions
  • read-only properties?
  • indexers
看下面的樣例:
public class RgbColor(int r, int g, int b) {public int Red { get; } = r;public int Green { get; } = g;public int Blue { get; } = b;public string ToHex() =>string.Format("#{0:X2}{1:X2}{2:X2}", Red, Green, Blue);public static RgbColor operator - (RgbColor color) =>new RgbColor(color.Red ^ 0xFF,color.Green ^ 0xFF,color.Blue ^ 0xFF); }
用於「方法」的樣例:
public string ToHex() => string.Format("#{0:X2}{1:X2}{2:X2}", Red, Green, Blue); public override string ToString() => this.Name;
用於「user-defined operators」的樣例:
public static RgbColor operator - (RgbColor color) =>new RgbColor(color.Red ^ 0xFF,color.Green ^ 0xFF,color.Blue ^ 0xFF);
public static Complex operator +(Complex a, Complex b) => a.Add(b);
用於「read-only property」的樣例:
特別注意,這是 Read Only Property 而不是 Field?
public string ID => Guid.NewGuid().ToString();又一個樣例:
public class Point(int x, int y) {public int X => x;public int Y => y;public double Dist => Math.Sqrt(x * x + y * y);public Point Move(int dx, int dy) => new Point(x + dx, y + dy); }
用於「type conversions」的樣例:實現 string 和 Name 之間的隱性轉換
public static implicit operator string(Name n) => n.First + " " + n.Last;


用於「indexers」的樣例:

public Customer this[Id id] => store.LookupCustomer(id);






版權聲明:本文博主原創文章。博客,未經同意不得轉載。

轉載于:https://www.cnblogs.com/zfyouxi/p/4915100.html

總結

以上是生活随笔為你收集整理的C# 6.0 (C# vNext) 的新功能:Expression Bodied Functions and Properties的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。