C#发展历程以及C#6.0新特性
一、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新特性的更多相关文章
- C#与C++的发展历程第二 - C#4.0再接再厉
系列文章目录 1. C#与C++的发展历程第一 - 由C#3.0起 2. C#与C++的发展历程第二 - C#4.0再接再厉 开始本系列的第二篇,这篇文章中将介绍C#4.0中一些变化,如C++有类似功 ...
- 跨时代的MySQL8.0新特性解读
目录 MySQL发展历程 MySQL8.0新特性 秒级加列 性能提升 文档数据库 SQL增强 共用表表达式(CTEs) 不可见索引(Invisible Indexes) 降序索引(Descending ...
- 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 ...
- 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. 托管与本地 ...
- 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?
来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...
- Java基础和JDK5.0新特性
Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...
- Visual Studio 2015速递(1)——C#6.0新特性怎么用
系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...
- 背水一战 Windows 10 (1) - C# 6.0 新特性
[源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...
- C# 7.0 新特性2: 本地方法
本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
随机推荐
- Apache安装问题:configure: error: APR not found . Please read the documentation
Linux上安装Apache时,编译出现错误: checking for APR... no configure: error: APR not found . Please read the do ...
- cocos2d之json使用实例
前端使用: json管理器中函数解析: 对宠物技能map的定义: 宠物技能表单--数据的基础定义:
- Linux计时器
Linux中, 系统为每个系统都维护了三种计时器,分别为: 真实计数器, 虚拟计时器以及实用计时器, 一般情况下都使用真实计时器 getitimer()/setitimer() //读取/设置内部计时 ...
- Eclipse 工程Clear与build的作用
由于eclipse的编译是基于时间戳的判断机制的.因此当你按build all的时候有些eclipse认为时间戳没有改变的类不会被编译.因此你可以先clean一下再编译.这个时候eclipse会将所有 ...
- C++ 笔记(二) —— 不要在构造和析构函数中调用虚函数
ilocker:关注 Android 安全(新手) QQ: 2597294287 class Transaction { //所有交易的 base class public: Transaction( ...
- sql查询一天内的where写法,sql写法
sql查询一天内的写法: 1. where createtime BETWEEN (select date_format(now(),'%Y-%m-%d 00:00:00')) and (select ...
- KVM 介绍(6):Nova 通过 libvirt 管理 QEMU/KVM 虚机 [Nova Libvirt QEMU/KVM Domain]
学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...
- UVa11549计算器谜题[floyd判圈]
题意: 有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 白书上的题 set, ...
- 洛谷U4807抽水机[最小生成树]
题目背景 kkk被Farmer John和他的奶牛贝茜虐的很惨,然后她也想体验下一个Farmer的生活.但她又懒得种地,就选择养鱼. 题目描述 这些鱼都是热带鱼(废话),很娇贵(比kkk娇贵),要经常 ...
- Codeforces 549G Happy Line[问题转换 sort]
G. Happy Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...