C#6.0和7.0的部分新特性介绍
语言版本 | 发布时间 | .NET Framework要求 |
C# 1.0 | 2002.1 | .NET Framework 1.0 |
C# 1.1\1.2 | 2003.4 | .NET Framework 1.1 |
C# 2.0 | 2005.11 | .NET Framework 2.0 |
C# 3.0 | 2007.11 | .NET Framework 2.0\3.0\3.5 |
C# 4.0 | 2010.4 | .NET Framework 4.0 |
C# 5.0 | 2012.8 | .NET Framework 4.5 |
C# 6.0 | 2015.7 | .NET Framework 4.6 |
C# 7.0 | 2017.3 | .NET Framework 4.6.2 |
C# 7.1 | 2017.6 | .NET Framework 4.7 |
C# 7.2 | 2017.11 | .NET Framework 4.7.1 |
C# 7.3 | 2018.5 | .NET Framework 4.7.2 |
C# 8.0 | 2019.4 | .NET Framework 4.8 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using static System.Math; //使用静态类,代码中可以直接调用方法 6.0
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
User user2 = null;
string uName = user2?.FirstName; //null类型判断,为null返回null 6.0
int? age = user2?.Age;
string myName = $"FirstName is {Name}"; //对字符串格式化简单化 6.0
string uOfName = nameof(user2); //获取字段名称 6.0
Dictionary<int, string> dict1 = new Dictionary<int, string>() { [2] = "re", [43] = "rt" }; //字典初始化 6.0
Dictionary<int, string> dict2 = new Dictionary<int, string>() { { 2, "re" }, { 43, "re" } }; //4.0Ago
get(out int a, out int b); //针对out可以合并 6.0
Console.WriteLine(a);
object obj = "gg";
if (obj is int iObj) //模式匹配 7.0
{
Console.WriteLine(iObj + 9);
}
var tuple = (a: 10, b: "123"); //元组(Tuples)
Console.WriteLine($"a:{tuple.a},b:{tuple.b}");
//解构元组 7.0
var result1 = Get1();
Console.WriteLine($"Item1:{result1.Item1},Item2:{result1.Item2},Item3:{result1.Item3}");
var (str1, int1, dt1) = Get1();
var result2 = Get2();
Console.WriteLine($"Item1:{result2.a},Item2:{result2.b},Item3:{result2.c}");
var (str2, int2, dt2) = Get2();
int num = 123_456; //允许在数字文字中_作为数字分隔符出现 7.0
Console.ReadKey();
}
//解构元组
static (string, int, DateTime) Get1() //7.0
{
return ("abc", 123, DateTime.Now);
}
static (string a, int b, DateTime c) Get2() //7.0
{
return (a: "abc", b: 123, c: DateTime.Now);
}
public static string Get(object a)
{
return GetP();
string GetP() //局部函数,方法中的方法 7.0
{
if (a is int v) return v + "";
if (a is string b) return b;
return "ccc";
}
}
public static void PrintStars(object o)
{
switch (o) //加强版switch 7.0
{
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; }
}
public static int get(out int a, out int b)
{
a = 8; b = 9;
return a + b;
}
public static string Name { get; set; } = "BB"; //直接对get属性赋初始值 6.0
public static int Age { get; } = 100;
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString() => string.Format("{0}——{1}", FirstName, LastName); //Lambda简写方法 6.0
public void S() => string.Format("{0}——{1}", FirstName, LastName);
public string FullName => FirstName + " " + LastName;
public int Age { get; set; } = 35;
public int ID
{
get => 0;
set => Age = value;
}
}
}
}