标签:c#
Expression Bodied Function 可以用在: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;
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);
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); }
public static implicit operator string(Name n) => n.First + " " + n.Last;
用於「indexers」的例子:
public Customer this[Id id] => store.LookupCustomer(id);
C# 6.0 (C# vNext) 新功能之:Expression Bodied Functions and Properties
标签:c#
原文地址:http://blog.csdn.net/kendo3065/article/details/40075907