一、C#发展历程

下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的。

二、C#6.0新特性

1、字符串插值 (String Interpolation)

字符串拼接优化

Before:

var Name = "joye.net";
var Results = "Hello" + Name;//直接拼接
var results1 = string.Format("Hello {0}", Name);//Format拼接

After:

var results2 = $"Hello {Name}"; //$拼接
var results= $"Hello {Name}{new Program().GetCnblogsSite()}";//{}可以直接插入代码

2、null检查运算符【 ?.】 (Monadic null checking)

null优化

Before:

        public static string GetCnblogsSite()
{
return "http://www.cnblogs.com/yinrq";
}
Program pro = null;
if(pro!=null)
Console.WriteLine(GetCnblogsSite());

After:

Program pro = null;
Console.WriteLine(pro?.GetCnblogsSite());

3、   自动属性初始化器(Initializers for auto-properties)

可以直接给自动属性赋值了,不需要写在构造函数中。

Before:

    public class ClassA
{
private string Name{get;set;};
public ClassA()
{
Name = "joye.net";
}
}

After:

    public class ClassA
{
public string Name { get; set; } ="joye.net"; }

4、只读自动属性(Getter-only auto-properties)

只读自动属性可以直接初始化,或者在构造函数中初始化。

before

 //缩小自动属性的访问权限
public class ClassA
{
public string Name { get; private set; } }
//C#1.0实现
public class ClassA
{
private string Name = "joye.net"; public string Name
{
get { return Name; }
}
}

after:

    public class ClassA
{
public string Name { get; } = "joye.net";
}

5、表达式方法体(Property Expressions && Method Expressions)

只读属性,只读索引器和方法都可以使用Lambda表达式作为Body。

一句话的方法体可以直接写成箭头函数,而不再需要大括号(分页控件http://www.cnblogs.com/yinrq/p/5586841.html就用到了属性表达式Property Expressions)

    public class PagerInBase
{
/// <summary>
/// 当前页
/// </summary>
public int PageIndex { get; set; } /// <summary>
/// 页数
/// </summary>
public int PageSize { get; set; }
     
//以前的写法
     //public int Skip{get{return (PageIndex - 1) * PageSize}}

//跳过序列中指定数量的元素
public int Skip => (PageIndex - ) * PageSize; /// <summary>
/// 请求URL
/// </summary>
public string RequetUrl => System.Web.HttpContext.Current.Request.Url.OriginalString; /// <summary>
/// 构造函数给当前页和页数初始化
/// </summary>
public PagerInBase()
{
if (PageIndex == ) PageIndex = ;
if (PageSize == ) PageSize = ;
}
}

方法表达式(Method Expressions)

        //before 的完整方法
public int Skip()
{
return (PageIndex - ) * PageSize
}
//After C#6.0 方法表达式
public int Skip() => (PageIndex - ) * PageSize;

6、using静态类(Static type using statements)

using System;
using static System.Math;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Log10() + PI);
}
}
}

7、检查方法参数nameof表达式(nameof expressions)

这个很有用,原来写WPF中的ViewModel层的属性变化通知时,需要写字符串,或者使用MvvmLight等库中的帮助方法,可以直接传入属性,但由于是在运行时解析,会有少许性能损失。现在使用nameof运算符,保证重构安全和可读性,又提升了性能。

Before:

        public static void Add(Person person)
{
if (person == null)
{
throw new ArgumentNullException("person");
}
}

After:

        public static void Add(Person person)
{
if (person == null)
{
throw new ArgumentNullException(nameof(person));
}
}

8、带索引的对象初始化器(Index initializers )

直接通过索引进行对象的初始化

var dic = new Dictionary<int, string> { []="joye.net",[]= "http://yinrq.cnblogs.com/",[]= "Index initializers " };

9、catch和finally 中使用await (catch和finally 中的 await )

在C#5.0中,await关键字是不能出现在catch和finnaly块中的。而C#6.0可以

            try
{
res = await Resource.OpenAsync(…); // You could do this
}
catch (ResourceException e)
{
await Resource.LogAsync(res, e); // Now you can do this
}
finally
{
if (res != null)
await res.CloseAsync(); // finally and do this.
}

10、内联out参数(Inline declarations for out params)

before

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

after:

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

11、无参数的结构体构造函数(Parameterless constructors in structs)

    public struct MyStruct
{
public int A { get; }
public int B { get; }
public MyStruct(int a, int b) { A = a; B = b; }
public MyStruct(): this(, ) { } }
     WriteLine(new MyStruct().ToString());
WriteLine(default(MyStruct).ToString());

三、代码

using System;
using System.Collections.Generic;
using static System.Console; namespace ConsoleApplication1
{
public class MyClass
{
public int A { get; set; } public int B { get; set; } = ; public string Separator { get; } = "/"; public string SeparatorSpaces { get; } = string.Empty; public double Value => (double)A / B; public int this[int index] => index == ? A : B; public int this[string index] => index == "A" ? A : B; public override string ToString() => "{A}{SeparatorSpaces}{Separator}{SeparatorSpaces}{B}"; public void Print() => WriteLine(ToString()); public MyClass()
{ } public MyClass(int a, int b)
{
A = a;
B = b;
} public MyClass(int a, int b, string separatorSpaces) : this(a, b)
{
SeparatorSpaces = separatorSpaces;
if (string.IsNullOrEmpty(separatorSpaces))
{
throw new ArgumentNullException(nameof(separatorSpaces));
}
} public static readonly Dictionary<string, MyClass> Dic =
new Dictionary<string, MyClass>
{
["zero"] = new MyClass(),
["one"] = new MyClass(, ),
["half"] = new MyClass(, ),
["quarter"] = new MyClass(, ),
["infinity"] = new MyClass(, ),
}; } public struct MyStruct
{
public int A { get; }
public int B { get; }
public MyStruct(int a, int b) { A = a; B = b; }
public MyStruct(): this(, ) { } public override string ToString() => "{A}{B}"; } class Program
{
static void Main(string[] args)
{
foreach (var f in MyClass.Dic)
{
WriteLine("{f.Key} : {f.Value.Value}");
} var fraction = new MyClass(, , " ");
fraction.Print(); try
{
fraction = new MyClass(, , null);
}
catch (ArgumentNullException e)
{
if (e.ParamName == "separatorSpaces")
WriteLine("separatorSpaces can not be null");
} MyClass v;
MyClass.Dic.TryGetValue("harf", out v);
v?.Print();
var a = v?.A;
WriteLine(a == null);
var b = v?["B"];
WriteLine(b == null);
WriteLine(v?.ToString() == null); WriteLine(new MyStruct().ToString());
WriteLine(default(MyStruct).ToString());
} }
}

C#发展历程以及C#6.0新特性的更多相关文章

  1. C#与C++的发展历程第二 - C#4.0再接再厉

    系列文章目录 1. C#与C++的发展历程第一 - 由C#3.0起 2. C#与C++的发展历程第二 - C#4.0再接再厉 开始本系列的第二篇,这篇文章中将介绍C#4.0中一些变化,如C++有类似功 ...

  2. 跨时代的MySQL8.0新特性解读

    目录 MySQL发展历程 MySQL8.0新特性 秒级加列 性能提升 文档数据库 SQL增强 共用表表达式(CTEs) 不可见索引(Invisible Indexes) 降序索引(Descending ...

  3. atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性

    atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性   1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...

  4. Atitit. C#.net clr 2.0  4.0新特性

    Atitit. C#.net clr 2.0  4.0新特性 1. CLR内部结构1 2. CLR 版本发展史3 3. CLR 2.0 3 4. CLR 4 新特性 概览4 4.1.1.  托管与本地 ...

  5. 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?

    来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...

  6. Java基础和JDK5.0新特性

    Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...

  7. Visual Studio 2015速递(1)——C#6.0新特性怎么用

    系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...

  8. 背水一战 Windows 10 (1) - C# 6.0 新特性

    [源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...

  9. C# 7.0 新特性2: 本地方法

    本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

随机推荐

  1. Spring源码阅读系列总结

    最近一段时间,粗略的查看了一下Spring源码,对Spring的两大核心和Spring的组件有了更深入的了解.同时在学习Spring源码时,得了解一些设计模式,不然阅读源码还是有一定难度的,所以一些重 ...

  2. mysql优化案例分析

    本文总结了一些工作常见的sql优化例子,虽然比较简单,但很实用,希望对大家有所帮助.sql优化一般分为两类,一类是sql本身的优化,如何走到合适的索引,如何减少排序,减少逻辑读:另一类是sql本身没有 ...

  3. iCalendar格式中关于RRule的解析和生成

    最近在做一个关于Calendar的项目,相当于Google Calendar或者Outlook中的Calendar.在Calendar的发布和共享中,使用到了iCalendar,是一种日历数据交换的标 ...

  4. Sybase PowerDesign 导入数据库结构formSqlserver

    采用Sybase PD 创建数据库设计是常见的方法,如果遇到链接数据源时,无法直接链接系统数据源,而且在Sybase PD中无法直接创建odbc数据源时, 可以到控制面板中创建数据源,一步步的网络上有 ...

  5. linux编程之内存映射

    一.概述                                                   内存映射是在调用进程的虚拟地址空间创建一个新的内存映射. 内存映射分为2种: 1.文件映射 ...

  6. cdoj 1489 老司机采花

    地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others)     M ...

  7. poj 2278 DNASequnce AC自动机

    地址:http://poj.org/problem?id=2778 题目: DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. 编写 Unity Editor 插件

    Editor Style Viewer 在开发过程中,我喜欢编写一些辅助的Editor插件,方便在游戏开发过程进行调试. 下面是摘自Asset Store的一个查看Unity 默认GUI样式的小工具 ...

  9. Android驱动入门-Led控制+app+ndk库+底层驱动

    硬件平台: FriendlyARM Tiny4412 Cortex-A9 操作系统: UBUNTU 14.04 LTS 时间:2016-09-20  21:56:48 本次实验使用的是 安卓APP + ...

  10. MVC 数据验证[转]

    前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...