一、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. 0003 64位Oracle11gR2不能运行SQL Developer的解决方法

    "应用程序开发"下的"SQL Developer"双击不可用,出现“Windows正在查找SQLDEVELOPER.BAT"的提示,如下图: 搜索博客 ...

  2. JS实现别踩白块小游戏

    最近有朋友找我用JS帮忙仿做一个别踩白块的小游戏程序,但他给的源代码较麻烦,而且没有注释,理解起来很无力,我就以自己的想法自己做了这个小游戏,主要是应用JS对DOM和数组的操作. 程序思路:如图:将游 ...

  3. 烂泥:Linux源码包制作RPM包之Apache

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 公司服务器比较多,需要把apache源码包制作成rpm包,然后放到公司内网yum源上进行下载安装.apache的rpm包安装方式比源码安装方式比较快,这能 ...

  4. linux tomcat 用/etc/init.d/tomcat start启动报错

    line 13: [ 0: unary operator expected please use "sudo service tomcat stop|start|restart" ...

  5. css3中变形与动画(三)

    transform可以实现矩阵变换,transition实现属性的平滑过渡,animation意思是动画,动漫,这个属性才和真正意义的一帧一帧的动画相关.本文就介绍animation属性. anima ...

  6. 买错的电影票,含着泪也得看-LAMP搭建&Linux基础

    hi 没说过,上周五室友过生请客,在龙湖里吃嗨了喝爽了,回去的路上侃侃而谈.说好的这周一起去看年内最后的大片,火星救援的,谁知道老子眼神不好,买错了电影的时间...把周六的约定提前到了今儿个下午,ma ...

  7. MMORPG大型游戏设计与开发(客户端架构 part2 of vegine)

    一个好的接口是尽可能让更多实用的方法进行整理封装,要记住的是不常用的方法和类最好不好封装到接口中,因为那样会造成本身的困惑.基础模块中并没有太多封装,甚至连一个类的封装也没有,而是一些很常用的工具方法 ...

  8. BZOJ2763[JLOI2011]飞行路线 [分层图最短路]

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2523  Solved: 946[Submit][Statu ...

  9. POJ1160 Post Office[序列DP]

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18680   Accepted: 10075 Des ...

  10. 分页查询的两种方法(双top 双order 和 row_number() over ())

    --跳过10条取2条 也叫分页select top 2 * from studentwhere studentno not in (select top 2 studentno from studen ...