首页 技术 正文
技术 2022年11月21日
0 收藏 953 点赞 3,478 浏览 1936 个字

微软昨天发布了新的VS 2015 ..随之而来的还有很多很多东西… .NET新版本 ASP.NET新版本…等等..太多..实在没消化..

分享一下也是昨天发布的新的C#6.0的部分新特性吧…

当然..我也没用过 – -,主要是参考国外某位的一篇文章..很详细,英文好的可以自行去看

https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6

首先

自动属性初始化增强

public class Customer
{
public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";
}
public class Customer
{
public string First { get; } = "Jane";
public string Last { get; } = "Doe";
}
public class Customer
{
//只读的属性
public string Name { get; };
//在初始化方法中赋值,可行~
public Customer(string first, string last)
{
Name = first + " " + last;
}
}

在C#5.0中是不可行的 如下图:

[干货来袭]C#6.0新特性

方法函数支持lambda写法 如下:

public void Print() => Console.WriteLine(First + " " + Last);

支持直接导入命名空间一样导入静态类,而不用在代码中使用静态类名 如下:

//静态导入Console
using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
static void Main()
{
//直接使用方法而不用Console.WriteLine
WriteLine(Sqrt(* + *));
WriteLine(Friday - Monday);
}
}

扩展方法,(这个不是很懂,解释不好请原谅)

在Main类中静态的导入你要扩展的类型,然后写你需要扩展的方法..?.. – -,不懂..

using static System.Linq.Enumerable; // The type, not the namespace
class Program
{
static void Main()
{
var range = Range(, ); // Ok: not extension
var odd = Where(range, i => i % == ); // Error, not in scope
var even = range.Where(i => i % == ); // Ok
}
}

非空的语法糖如下:

int? first = customers?[].Orders.Count();
//上面的写法等同于下面
int? first = (customers != null) ? customers[].Orders.Count() : null;

字符串格式化新玩法:

//原来的,我们需要这样..
var s = String.Format("{0} is {1} year{{s}} old", p.Name, p.Age);//C#6.0中,直接如下:
var s = $"{p.Name} is {p.Age} year{{s}} old";//在{}中甚至可以是任意的..(单词不会..表达式还是什么鬼.. - -,)反正如下:
var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";
var s = $"{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";

索引初始化:

var numbers = new Dictionary<int, string> {
[] = "seven",
[] = "nine",
[] = "thirteen"
};

异常过滤器:

如果When中用括号括起来的表达式计算结果为true,catch块中运行,否则异常持续。

( – -,然而我也并没有搞懂..求大神解释..)

try { … }
catch (MyException e) when (myfilter(e))
{

}

可异步等待的Catch块:

Resource res = null;
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(); // … and this.
}

基本到此就结束了, – -,水平有限.不好与不完善的地方请指出..免得误导大家..

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898