首页 > 其他 > 详细

C# 6.0可能的新特性及C#发展历程

时间:2014-04-13 12:16:38      阅读:469      评论:0      收藏:0      [点我收藏+]

        据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework 4.5.1。

从Visual Studio的更新规律上来看,微软2或者3年,更新增加的东西会比较多,所以对于C# 6.0,还是有一些期待的。

下面这张图列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的。其中图的最后一行C#6.0是根据一些博客整理的,如有错误,随时改正。

bubuko.com,布布扣

C# 6.0可能的新特性

1、主构造函数(Primary Constructors)

  主构造函数给类中的变量赋值

Before

bubuko.com,布布扣
public class Point {
    private int x, y;

    public Point(int x, int y)
        this.x = x;
        this.y = y;
    }
}
bubuko.com,布布扣

 After

bubuko.com,布布扣
public class Point(int x, int y) {
    private int x, y;
}
bubuko.com,布布扣

2、自动属性赋值(Auto Properties)

Before

bubuko.com,布布扣
  class Point
    {
        public int X { get; private set; }
        public int Y { get; private set; }

        public Point()
        {
            this.X = 100;
            this.Y = 100;
        }
    } 
bubuko.com,布布扣

After

bubuko.com,布布扣
  class Point
    {
        public int X { get; private set; } = 100;
        public int Y { get; private set; } = 100;
    } 
bubuko.com,布布扣

3、using静态类(Static type using statements;)

using会把引用类的所有静态方法导入到当前命名空间

Before

bubuko.com,布布扣
public double A { get { return Math.Sqrt(Math.Round(5.142)); } }
bubuko.com,布布扣

After

bubuko.com,布布扣
using System.Math;
...
public double A { get { return Sqrt(Round(5.142)); } }
bubuko.com,布布扣

4、Property Expressions

Before

bubuko.com,布布扣
public double Distance {
    get { return Math.Sqrt((X * X) + (Y * Y)); }
}
bubuko.com,布布扣

After

bubuko.com,布布扣
public double Distance => Math.Sqrt((X * X) + (Y * Y));
bubuko.com,布布扣

 初看起来像Lambada表达式,其实和Lambada无关系。

5. Method Expressions

Before

bubuko.com,布布扣
public Point Move(int dx, int dy) {
    return new Point(X + dx1, Y + dy1);
}
bubuko.com,布布扣

After

bubuko.com,布布扣
public Point Move(int dx, int dy) => new Point(X + dx, Y + dy);
bubuko.com,布布扣

这个和Property Expressions类似

6、Params for enumerables

Before

bubuko.com,布布扣
Do(someEnum.ToArray());
...
public void Do(params int[] values) { ... }
bubuko.com,布布扣

After

bubuko.com,布布扣
Do(someEnum);
public void Do(params IEnumerable<Point> points) { ... }
bubuko.com,布布扣

 以前params是只可以修饰array类型的参数,现在多了一些类型

7、Monadic null checking(null检查运算符)

Before

bubuko.com,布布扣
if (points != null) {
    var next = points.FirstOrDefault();
    if (next != null && next.X != null) return next.X;
}   
return -1;
bubuko.com,布布扣

After

bubuko.com,布布扣
var bestValue = points?.FirstOrDefault()?.X ?? -1;
bubuko.com,布布扣

这个少了好几行代码,赞啊 

8、Constructor type parameter inference

Before

bubuko.com,布布扣
var x = MyClass.Create(1, "X");
...
public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) {
    return new MyClass<T1, T2>(a, b);
}
bubuko.com,布布扣

After

var x = new MyClass(1, "X");

9、 Inline declarations for out params(内联out参数定义)

Before

int x;
int.TryParse("123", out x);

After

int.TryParse("123", out int x);

说实话,这个很常用。

 

当然,C# 6.0的全部新特性不止这么多,限于篇幅,就整理这些,若想了解更多,请参考下面的链接。

参考资料:

http://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Documentation

http://en.wikipedia.org/wiki/.NET_Framework_version_history

http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

http://www.kunal-chowdhury.com/2012/07/evolution-of-c-10-50-what-are-new.html

http://www.cnblogs.com/TianFang/p/3647144.html

C# 6.0可能的新特性及C#发展历程,布布扣,bubuko.com

C# 6.0可能的新特性及C#发展历程

原文:http://www.cnblogs.com/lhking/p/3660182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!