欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > c# >内容正文

c#

c# 6.0 (c# vnext) 的新功能:expression bodied functions and properties -凯发k8官方网

发布时间:2023/11/29 c# 17 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 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);






凯发k8官方网的版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4915100.html

总结

以上是凯发k8官方网为你收集整理的c# 6.0 (c# vnext) 的新功能:expression bodied functions and properties的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图