自从 2000 年初期发布以来,c# 编程语言不断的得到改进,使我们能够更加清晰的编写代码,也更加容易维护我们的代码,增强的功能已经从 1.0 搞到啦 7.0 甚至 7.1,每一次改过都伴随着. NET Framework 库的相应支持,也不断的带给我们期待与惊喜。下面我们就对 c# 一路走到现在,做一个回顾与学习。
c# 的设计目标是有以下几点:
c# 版本
c# 版本中添加的功能:
参考文档:https://en.wikipedia.org/wiki/C_Sharp_(programming_language)#cite_note-roslyn_6-47
在 2016 年 Visual Studio "15" Preview 4 发布,c# 7.0 便开始浮现,现在 vs2017 已经发布,c#7.0 带给我们的惊喜,我们也该一探究竟啦。
C#7.0 增加许多新功能,重点是数据,代码简化和性能上。
目前在 C#中,使用 out 参数不像我们想要的那么流畅。在使用 out 参数调用一个方法之前,首先必须声明变量来传递给它。您也不能使用
它们来声明它们,但需要指定完整的类型。
- var
- public void PrintCoordinates(Point p)
- {
- int x, y; //必须声明
- p.GetCoordinates(out x, out y);
- WriteLine($"({x}, {y})");
- }
在 c#7 中再来看看
- public void PrintCoordinates(Point p)
- {
- p.GetCoordinates(out int x, out int y);
- WriteLine($"({x}, {y})");
- }
c#7 中
- public static void PrintStars(object o)
- {
- if (o is int i) Console.WriteLine(i + 12);
- }
以前版本需要转化
- public static void PrintStars(object o)
- {
- if (o is int) Console.WriteLine(Convert.ToInt32(o) + 12);
- }
- public static void PrintStars(object o) {
- switch (o) {
- case Print p:
- break;
- case int a:
- break;
- case String b when b == "123": break;
- }
- }
- }
- public class Print {
- public string PrintName {
- get;
- set;
- }
- public string MoBanPath {
- get;
- set;
- }
- public int Count {
- get;
- set;
- }
- }
注意:元组依赖于一组基础类型,不包括在预览 4 中。要使功能正常工作,您可以通过 NuGet 轻松获取它们:
- static void Main(string[] args)
- {
- var tuple = (a: 10, b: "123");
- Console.WriteLine($"a:{tuple.a},b:{tuple.b}");
- var result1 = GetS();
- var result = Get();
- Console.WriteLine($"Item1:{result1.Item1},Item2:{result1.Item2},Item3:{result1.Item3}");
- Console.WriteLine($"a:{result.a},b:{result.b},c:{result.c}");
- Console.ReadLine();
- }
- static (string, int, DateTime) GetS()
- {
- return ("abc", 123, DateTime.Now);
- }
- static (string a, int b, DateTime c) Get()
- {
- return (a: "abc", b: 123, c: DateTime.Now);
- }
简单的说,就是在方法里面写方法然后自己调用。
注意:在预览 4 中,本地函数必须在调用之前声明。这个限制将被松开,所以一旦他们读取的局部变量被明确地分配,就可以调用这个限制。
- static void Main(string[] args)
- {
- Console.WriteLine($"{Get(123)},{Get("abc")},{Get(null)}");
- Console.ReadLine();
- }
- public static string Get(object a)
- {
- return GetP();
- string GetP()
- {
- if (a is int v) return v + "";
- if (a is string b) return b;
- return "ccc";
- }
- }
C#7.0 允许在数字文字中
作为数字分隔符出现:
- _
- var d = 123_456;
- var x = 0xAB_CD_EF;
你可以把它们放在数字之间,以提高可读性。它们对价值没有影响。
另外,C#7.0 引入了二进制文字,因此您可以直接指定位模式,而不必以心脏知道十六进制符号。
- var b = 0b1010_1011_1100_1101_1110_1111;
到此,我就不在一个个的上演 c#7.0 的其他功能啦。如果你想深入其他,仔细阅读下面的文档:
请参考文档:https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/
以下新功能在 VS 2015 及 17 中实现并可用。
ADD: 新增,Exists: 已存在,N/A:对该语言没有意义,No:不适用此版本。
下面我简述几个新功能,剩下的我会把文档连接拿出来自己观摩。
上面的代码中都有体现。
- var s = $"{p.Name} is {p.Age} year{{s}} old";
- var numbers = new Dictionary<int, string> {
- [7] = "seven",
- [9] = "nine",
- [13] = "thirteen"
- };
- public class Customer {
- public string First {
- get;
- set;
- } = "Jane";
- public string Last {
- get;
- set;
- } = "Doe";
- }
- using static System.Console;
- using static System.Math;
- using static System.DayOfWeek;
- class Program
- {
- static void Main()
- {
- WriteLine(Sqrt(3*3 + 4*4));
- WriteLine(Friday - Monday);
- }
- }
线上代码可执行演示文档连接:http://www.volatileread.com/Wiki/Index?id=1075 效果如下
参考文档:https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C#-6
连接地址:https://blogs.msdn.microsoft.com/mvpawardprogram/2012/03/26/an-introduction-to-new-features-in-c-5-0/
连接地址:https://msdn.microsoft.com/en-us/magazine/ff796223.aspx
连接地址:https://msdn.microsoft.com/en-us/library/bb308966.aspx
连接地址:https://msdn.microsoft.com/en-us/library/7cz8t42e(v=vs.80).aspx
接下来是大家最喜欢的总结内容啦,内容有二,如下:
1、希望能关注我其他的文章。
2、博客里面有没有很清楚的说明白,或者你有更好的方式,那么欢迎加入左上方的 2 个交流群,我们一起学习探讨。
来源: http://www.cnblogs.com/knowledgesea/p/6694979.html