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

版权声明:本文博主原创文章。博客,未经同意不得转载。

C# 6.0 (C# vNext) 的新功能:Expression Bodied Functions and Properties的更多相关文章

  1. C# 6.0 (C# vNext) 的新功能:Exception-Handling Improvements

    于 C# 6.0 包裹在异常处理的新功能,有两个方面的改进: 异步处理(async and await)能力 catch block 总结使用.于 C# 5.0 释放 async and await, ...

  2. 微信小程序0.11.122100版本新功能解析

    微信小程序0.11.122100版本新功能解析   新版本就不再吐槽了,整的自己跟个愤青似的.人老了,喷不动了,把机会留给年轻人吧.下午随着新版本开放,微信居然破天荒的开放了开发者论坛.我很是担心官方 ...

  3. Docker 1.12.0将要发布的新功能

    Docker 1.12.0将要发布的新功能 导读 按计划,6/14 是1.12.0版本的 feature冻结 的日子,再有两个星期Docker 1.12.0也该发布了.这里列出来的新功能,都是已经合并 ...

  4. 【翻译】Ext JS 5.0.1 中的新功能

    原文:What's New in Ext JS 5.0.1 今天,我们很高兴的宣布Ext JS 5.0.1发布了!此维护版本基于Sencha社区的反馈做了一些改进.下面让我们来了解一下这些改变. 可访 ...

  5. What's new in XAML of .NET 4.0( .NET 4.0中XAML的新功能 )

    原文 What's new in XAML of .NET 4.0 What's new in XAML of .NET 4.0 Easy Object References with {x:Refe ...

  6. ML.NET 发布0.11版本:.NET中的机器学习,为TensorFlow和ONNX添加了新功能

    微软发布了其最新版本的机器学习框架:ML.NET 0.11带来了新功能和突破性变化. 新版本的机器学习开源框架为TensorFlow和ONNX添加了新功能,但也包括一些重大变化, 这也是发布RC版本之 ...

  7. Android O 正式版新功能

    ref: Android O新特性和行为变更总结zzhttp://www.cnblogs.com/bluestorm/p/7148134.html Android O正式版带来了诸多新功能,如Tens ...

  8. 【开源】OSharp3.0框架解说系列:新版本说明及新功能规划预览

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  9. 一张图看懂ANSYS17.0 流体 新功能与改进

    一张图看懂ANSYS17.0 流体 新功能与改进   提交 我的留言 加载中 已留言   一张图看懂ANSYS17.0 流体 新功能与改进 原创2016-02-03ANSYS模拟在线模拟在线 模拟在线 ...

随机推荐

  1. AIR

    There is a meaning for wings that cannot fly,it's a previous memory of when you once flew through th ...

  2. hdu4055 Number String

    Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. linux-sfdisk 使用方法

    功能说明:硬盘分区工具程序. 语 法:sfdisk [-?Tvx][-d <硬盘>][-g <硬盘>][-l <硬盘>][-s <分区>][-V < ...

  4. 小心LinkedHashMap的get()方法(转)

    这是一个来自实际项目的例子,在这个案例中,有同事基于jdk中的LinkedHashMap设计了一个LRUCache,为了提高性能,使用了 ReentrantReadWriteLock 读写锁:写锁对应 ...

  5. CS0433: 类型“BasePage”同一时候存在于“c:\Windows\Microsoft.NETxxxxxxxxxxxxxxxx

    网上常见的我就不说了. 假设其他地址的方法解决不了你的问题,那么请往下看. 该类是否存放于 App_Code 下,假设是把该类从App_Code中拉出来,然后再次执行试试.

  6. jconsole 连接 eclipse启动项目

    eclipse 启动java项目默认没有开启jmx远程查看功能,假设须要看项目执行的线程内存使用量等信息,能够在eclipse启动參数中添加: -Dcom.sun.management.jmxremo ...

  7. The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar

    出现 The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the j ...

  8. 从零开始,使用python快速开发web站点(2)

    书接上文.http://blog.csdn.net/i7788/article/details/10306595 首先是数据库的搭建,这里的django的数据模型搭建十分easy. no sql.ju ...

  9. Android笔记二十七.Service组件入门(一).什么是Service?

    转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 一.Service 1.Service简单介绍     Service为Android四大组件之中 ...

  10. 公布一个软件,轻新视频录播程序,H264/AAC录制视音频,保存FLV,支持RTMP直播

    已经上传到CSDN,下载地址:http://download.csdn.net/detail/avsuper/7421647,不要钱滴,嘿嘿... 本程序能够把摄像头视频和麦克风音频,录制为FLV文件 ...