C# 与 .NET Framework 对应关系
下表中列出 C#、.NET、VS版本对应关系
| C#版本 | 发布时间 | .NET 版本 | VS版本 | CLR版本 |
| C#1.0 | 2002-02-13 | .NET Framework 1.0 | VS.NET 2002 | 1.0 |
| C#1.1 C#1.2 | 2003-04-24 | .NET Framework 1.1 | VS.NET 2003 | 1.1 |
| C#2.0 C#3.0(除Linq) | 2005-11-07 | .NET Framework 2.0 | VS2005 | 2.0 |
| C#3.0(除Linq) | 2006-11 | .NET Framework 3.0 | VS2008 | 2.0 |
| C#3.0 | 2007-11-19 | .NET Framework 3.5 | VS2008 | 2.0 |
| C#4.0 | 2010-4-12 | .NET Framework 4.0 | VS2010 | 4 |
| C#5.0 | 2012-02-20 | .NET Framework 4.5 | VS2012 | 4 |
| C#5.0 | 2013-10-17 | .NET Framework 4.5.1 | VS2013 | 4 |
| C#5.0 | 2014-05-05 | .NET Framework 4.5.2 | VS2013 | 4 |
| C#6.0 | 2015-07-26 | .NET Framework 4.6 | VS2015(v14) | 4 |
| C#6.0 | 2015-11-30 | .NET Framework 4.6.1 | VS2015(v14) | 4 |
| C#7.0 | 2016-08-02 | .NET Framework 4.6.2 | VS2017(v15) | 4 |
| C#7.1 | 2017-04-05 | .NET Framework 4.7 | VS2017(v15.3) | 4 |
| C#7.2 | 2017-10-17 | .NET Framework 4.7.1 | VS2017(v15.5) | 4 |
| C#7.3 | 2018-04-30 | .NET Framework 4.7.2 | VS2017(v15.7) | 4 |
| C#8.0 | 2019-04-18 | .NET Framework 4.8 | VS2019(v16) | 4 |
| C#9.0 | 2020-11-11 | .NET 5 | VS2019(16.8) |
c#6.0语法
//1.只读自动属性
{
Student student = new Student("Richard", "Richard01");
string fullName = student.ToString();
string fullName1 = student.FullName;
}
//2.using static
{
StaticClass.Next();
StaticClass.NextTo();
Next();
NextTo();
}
//3.Null 条件运算符
{
Student student = null;
// student.FirstName; //如果student为null 未将对象引用至对象的实例
//if (student!=null)
//{
// string fullName = student.FullName;
//}
string fullName = student?.FullName; //得到的结果一定是要支持null
int? id = student?.Id; //? 只能为可空类型服务
student = new Student("AB", "CD");
fullName = student?.FullName;
string testName = student?.TestName ?? "朝夕教育";
student.TestName = "Zhaoxi教育";
testName = student?.TestName ?? "朝夕教育";
}
//4.字符串内插
{
//string.Format();
//stringBuilder
//string+
string firstName = "朝夕";
string lastName = "教育";
var strs = $"{{{firstName}}}-{lastName}-{firstName}-{firstName};{firstName}";
}
//5.异常筛选器
{
之前都是自定义不同的Exception 继承Exception父类
//StaticClass.ExceptionShow();
}
//6.nameof 表达式
{
string clasName= "StaticClass";
string className1 = nameof(StaticClass);
Console.WriteLine(className1);
string className2 = nameof(SharpSixInfo);
Console.WriteLine(className2);
string className3 = nameof(Student);
Console.WriteLine(className3);
//7.什么是事件?
NotifyPropertyChanged notifyPropertyChanged = new NotifyPropertyChanged();
notifyPropertyChanged.PropertyChanged += (object o, PropertyChangedEventArgs b) =>
{
};
notifyPropertyChanged.LastName = "朝夕教育";
}
//8.使用索引器初始化关联集合
{
Dictionary messages = new Dictionary
{
{ 404, "Page not Found"},
{ 302, "Page moved, but left a forwarding address."},
{ 500, "The web server can't come out to play today."}
};
messages.Add(405,"朝夕教育");
messages.Remove(405, out string obj);
Console.WriteLine(obj);
Dictionary webErrors = new Dictionary
{
[404] = "Page not Found",
[302] = "Page moved, but left a forwarding address.",
[500] = "The web server can't come out to play today."
};
webErrors.Add(405, "朝夕教育");
webErrors.Remove(405, out string strResult);
}
c#7.0语法
#region Out变量
{
不需要声明
//string input = Console.ReadLine();
//int.TryParse(input, out int result);
//Console.WriteLine(result);
out 修改是可以是var类型声明
//int.TryParse("456", out var result1);
//Console.WriteLine(result1);
//Console.WriteLine("****************Out*****************");
out 变量
//DInfo dInfo = new DInfo(123);
}
#endregion
#region 元组
{
(string Alpha, string Beta) namedLetters = ("a", "b");
namedLetters.Alpha = "aa";
namedLetters.Beta = "bb";
Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
var alphabetStart = (Alpha: "a", Beta: "b");
alphabetStart.Beta = "B+B";
alphabetStart.Alpha = "A+A";
Console.WriteLine($"{alphabetStart.Alpha}, {alphabetStart.Beta}");
(int max, int min) = Range();
Console.WriteLine(max);
Console.WriteLine(min);
}
{
var p = new Point(12, 13);
Console.WriteLine(p.X);
Console.WriteLine(p.Y);
p.Deconstruct(out double xx, out double yy);
Console.WriteLine(xx);
Console.WriteLine(yy);
}
#endregion
#region 弃元
{
var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
(string, double, int, int, int, int) result = QueryCityDataForYears("New York City", 1960, 2010);
}
#endregion
#region 模式
{
int input = 123;
int sum = 234;
if (input is int count)
sum += count;
}
{
//IEnumerable
c#8.0语法
#region 默认接口方法
CustomInterface interface1 = new CustomClass();
interface1.Show();
interface1.ShowInfo();
#endregion
#region switch 表达式
//老玩法
string week3 = WeekToStringSwitch(WeekInfo.Monday);
string week4 = WeekToStringSwitch(WeekInfo.Tuesday);
string week5 = WeekToStringSwitch(WeekInfo.Wednesday);
//新玩法
string week = WeekToString(WeekInfo.Monday);
string week1 = WeekToString(WeekInfo.Tuesday);
string week2 = WeekToString(WeekInfo.Wednesday);
///
/// C#8新语法
///
///
///
public static string WeekToString(WeekInfo week) => week switch
{
WeekInfo.Monday => "周一",
WeekInfo.Tuesday => "周二",
WeekInfo.Wednesday => "周三",
WeekInfo.Thursday => "周四",
WeekInfo.Friday => "周五",
WeekInfo.Saturday => "周六",
WeekInfo.Sunday => "周七",
_ => throw new NotImplementedException("枚举不存在"),
};
///
///switch 经典switch
///
///
///
public static string WeekToStringSwitch(WeekInfo week)
{
switch (week)
{
case WeekInfo.Monday:
return "周一";
case WeekInfo.Tuesday:
return "周二";
case WeekInfo.Wednesday:
return "周三";
case WeekInfo.Thursday:
return "周四";
case WeekInfo.Friday:
return "周五";
case WeekInfo.Saturday:
return "周六";
case WeekInfo.Sunday:
return "周七";
default:
throw new NotImplementedException("枚举不存在");
}
}
#endregion
#region 属性模式
PropertyPattern product = new PropertyPattern()
{
ProductName = "朝夕架构班",
Price = 5499
};
PropertyPattern product1 = new PropertyPattern()
{
ProductName = "朝夕高级班",
Price = 4299
};
double price = PropertyPatternShow(product);
///
///switch 属性模式
///
///
///
public static double PropertyPatternShow(PropertyPattern pattern) => pattern
switch
{
{ ProductName: "朝夕架构班" } => pattern.Price * 0.5,
{ Price: 234 } => pattern.Price * 0.5,
_ => throw new NotImplementedException(),
};
#endregion
#region 元组模式
string strResult = RockPaperScissors("朝夕", "教育1");
string strResult1 = RockPaperScissors("朝夕", "教育");
string strResult3 = RockPaperScissors("朝夕", "教育3");
///
/// switch 元组模式
///
///
///
///
public static string RockPaperScissors(string first, string second)
=> (first, second) switch
{
("朝夕", "教育1") => $"{first}-{second}",
("朝夕", "教育2") => $"{first}-{second}",
("朝夕", "教育3") => $"{first}-{second}",
(_, _) => "不匹配"
};
#endregion
#region 位置模式
PointInfo point = new PointInfo(123, 234);
int i = 11;
int j = 12;
point.Deconstruct(out i, out j);
public class PointInfo
{
public int X { get; }
public int Y { get; }
public PointInfo(int x, int y) => (X, Y) = (x, y);
public void Deconstruct(out int x, out int y) =>
(x, y) = (X, Y);
}
#endregion
#region 静态本地函数
M();
static int M()
{
int y = 5;
int x = 7;
return Add(x, y);
static int Add(int left, int right) => left + right;
}
#endregion
#region 可空引用类型
//string? str = null;
//string str1 = null;
#endregion
#region 异步流
AsyncIEnumerableShow();
///
/// 异步流
///
public async static void AsyncIEnumerableShow()
{
Console.WriteLine("********************异步流*********************");
IAsyncEnumerable asynclist = GenerateSequence();
await foreach (var item in asynclist)
{
Console.WriteLine(item);
}
Console.WriteLine("********************异步流*********************");
}
public static async IAsyncEnumerable GenerateSequence()
{
for (int i = 0; i < 20; i++)
{
await Task.Delay(1000).ContinueWith(s =>
{
Console.WriteLine($"线程ID:{Thread.CurrentThread.ManagedThreadId.ToString("000")}");
});
yield return i;
}
}
#endregion
c# 9 语法
//Init是针对于类内部的字段而言;
FooInfo003 fooInfo003 = new FooInfo003()
{
PropA = "FooInfo003",
PropB = "FooInfo003_1"
};
//fooInfo003.PropA = "";//报错
//fooInfo003.PropB = "";//报错
string propa3 = fooInfo003.PropA;
string propb3 = fooInfo003.PropB;
public class FooInfo003
{
public string PropA { get; init; }
public string PropB { get; init; }
}
//Record关键字
Person person = new Person("朝夕", "教育");
string firstName = person.FirstName;
string lastName = person.LastName;
//person.FirstName = "";
//person.LastName = "培训";//报错
var otherPerson = person with { FirstName = "武汉朝夕" };
Console.WriteLine(object.ReferenceEquals(person, otherPerson));
Teacher teacher = new Teacher("朝夕", "教育", "科技有限公司");
string firstname1 = teacher.FirstName;
//teacher.FirstName = "";//报错了
var teachernew = teacher with { }; //with 表达式指示编译器创建记录的副本
Console.WriteLine(object.ReferenceEquals(teacher, teachernew));
public record Person
{
public Person(string first, string last) => (FirstName, LastName) = (first, last);
public string LastName { get; init; }
public string FirstName { get; init; }
}
public record Teacher : Person
{
public string Subject { get; }
public Teacher(string first, string last, string sub)
: base(first, last) => Subject = sub;
}
//顶级语句
//见控制台输出语句
不需要在program中定义Main方法,可直接在在Program中写程序
#endregion
//简化new()用法
#region 对象声明
{
FooInfo foo = new FooInfo();
FooInfo fooInfo1 = new(); //语法糖
FooInfo fooInfo2 = new() { PropA = "123", PropB = "234" };
}
#endregion
#region 方法调用
{
var result = ForecastFor(1, "Richard", new());
private static SharpNineInfo ForecastFor(int id, string name, FooInfo fooInfo)
{
return new();
}
}
#endregion
#region 静态修饰
{
Func func1 = () => 1;
Func func = static () => 1;
}
#endregion
#region 模式匹配
public class SharpNineInfo
{
public static void Show()
{
char a = 'A';
bool bResult = a.IsLetterNew();
}
}
public static class SharpNineInfoExtension
{
public static bool IsLetterNew(this char c)
=> c is (>= 'a' and <= 'z') or >= 'A' and <= 'Z';
}
#endregion
#region 为空判断
string name = "Ricahrd";
if (string.IsNullOrWhiteSpace(name))
{
}
if (name is not null)
{
}
#endregion
#region 协变式返回值(Covariant returns)
{
public abstract class Animal
{
public abstract Food GetFood();
}
public class Food
{
}
public class Meat : Food
{
}
///
/// 在C#9中覆写父类的方法,可以返回类型的子类
///
public class Tiger : Animal
{
public override Meat GetFood()
{
return new Meat();
}
}
}
#endregion
#region nint
{
// 原生大小的数字类型
//这次引入一组新类型(nint,nuint,nfloat等)'n'表示native(原生),该特性允许声明一个32位或64位的数据类型,这取决于操作系统的平台类型。
//nint nativeInt = 55; //在32位主机中编译时,需要4个字节。
//nint nativeInt1 = 55; //使用x64编译设置在64位主机中编译时需要8个字节。
//uint;
}
#endregion
#region Lambda 参数弃元
{
// C# 9 之前
Func zero = (a, b) => 0;
Func func = delegate (int a, int b) { return 0; };
}
// C# 9
{
Func zero = (_, _) => 0;
Func func = delegate (int _, int _) { return 0; };
}



