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<int, string> messages = new Dictionary<int, string>
{
{ 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<int, string> webErrors = new Dictionary<int, string>
{
[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<object> enumerablelist = new List<object>()
//{
// 0,
// new List<int>(){ 0,1,2,3,4,5,6 },
// 100,
// null
//};
//int iResult = SumPositiveNumbers(enumerablelist);
/// <summary>
/// 模式
/// </summary>
/// <param name="sequence"></param>
/// <returns></returns>
public static int SumPositiveNumbers(IEnumerable<object> sequence)
{
int sum = 0;
foreach (var i in sequence)
{
switch (i)
{
case 0:
break;
case IEnumerable<int> childSequence:
{
foreach (var item in childSequence)
sum += (item > 0) ? item : 0;
break;
}
case int n when n > 0:
sum += n;
break;
case null:
throw new NullReferenceException("Null found in sequence");
default:
throw new InvalidOperationException("Unrecognized type");
}
}
return sum;
}
}
#region 本地方法
LocalFunction("朝夕教育");
public static string LocalFunction(string name)
{
return ZhxiToString(name);
string ZhxiToString(string name)
{
return name;
}
}
#endregion
#region 默认文本表达式
//Func<string, bool> whereClause = default(Func<string, bool>);
//Func<string, bool> whereClause1 = default;
//string str = default;
//int i = default;
#endregion
#region 数字文本语法改进
//二进制数形式写入
//误读的数值常量可能使第一次阅读代码时更难理解。 位掩码或其他符号值容易产生误解。 C# 7.0 包括两项新功能,可用于以最可读的方式写入数字来用于预期用途:二进制文本和数字分隔符 。
//在创建位掩码时,或每当数字的二进制表示形式使代码最具可读性时,以二进制形式写入该数字
int Sixteen = 0b0001_0000;
Console.WriteLine(Sixteen);
int ThirtyTwo = 0b0010_0000;
Console.WriteLine(ThirtyTwo);
int SixtyFour = 0b0100_0000;
Console.WriteLine(SixtyFour);
int OneHundredTwentyEight = 0b1000_0000;
Console.WriteLine(OneHundredTwentyEight);
//long类型
//常量开头的 0b 表示该数字以二进制数形式写入。 二进制数可能会很长,因此通过引入 _ 作为数字分隔符通常更易于查看位模式,如前面示例中的二进制常量所示。 数字分隔符可以出现在常量的任何位置。 对于十进制数字,通常将其用作千位分隔符。 十六进制和二进制文本可采用 _ 开头:
long BillionsAndBillions = 100_000_000_000;
Console.WriteLine(BillionsAndBillions);
//decimal、float 和 double
//数字分隔符也可以与 decimal、float 和 double 类型一起使用:
double AvogadroConstant = 6.022_140_857_747_474e23;
Console.WriteLine(AvogadroConstant);
decimal GoldenRatio = 1.618_033_988_749_894_848_204_586_834_365_638_117_720_309_179M;
Console.WriteLine(GoldenRatio);
#endregion
#region 命名实参
// 该方法可以通过使用位置参数以正常方式调用。
PrintOrderDetails("Gift Shop", 31, "Red Mug");
// 可以按任何顺序为参数提供命名参数。
PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop");
PrintOrderDetails(productName: "Red Mug", sellerName: "Gift Shop", orderNum: 31);
// 与位置参数混合的命名参数有效
// 只要它们被使用在正确的位置。
PrintOrderDetails("Gift Shop", 31, productName: "Red Mug");
PrintOrderDetails(sellerName: "Gift Shop", 31, productName: "Red Mug"); // C# 7.2 onwards
PrintOrderDetails("Gift Shop", orderNum: 31, "Red Mug"); // C# 7.2 onwards
但是,如果使用顺序错误,则混合参数无效。
以下语句将导致编译器错误。
//PrintOrderDetails("Gift Shop", 31, productName: "Red Mug");
//PrintOrderDetails(31, sellerName: "Gift Shop", "Red Mug");
//PrintOrderDetails(31, "Red Mug", sellerName: "Gift Shop");
#endregion
#region private protected 访问修饰符---新复合访问修饰符
// 访问修饰符是关键字,用于指定成员或类型已声明的可访问性。 本部分介绍四个访问修饰符:
//可使用访问修饰符指定以下六个可访问性级别:
//public:访问不受限制。
//protected:访问限于包含类或派生自包含类的类型。
//internal:访问限于当前程序集。
//protected internal:访问限于当前程序集或派生自包含类的类型。
//private:访问限于包含类。
//C#7特有
//private protected:访问限于包含类或当前程序集中派生自包含类的类型。
#endregion
#region 增强的泛型约束
///之前讲的泛型约束有几种?
//枚举做基类约束
GerenicInfo<EnumInfo> info = new GerenicInfo<EnumInfo>();
info.Show(EnumInfo.ONE);
//约束不能为空
GerenicInfoUnmanaged<int> gerenicInfoUnmanaged = new GerenicInfoUnmanaged<int>();
//GerenicInfoUnmanaged<string> gerenicInfoUnmanaged = new GerenicInfoUnmanaged<string>();
public class GerenicInfo<T> where T : Enum
{
public void Show(T t)
{
}
}
public enum EnumInfo
{
ONE,
TOWN,
THREE
}
/// <summary>
/// 约束不能为空
/// </summary>
/// <typeparam name="T"></typeparam>
public class GerenicInfoUnmanaged<T> where T : unmanaged
{
public void Show(T t)
{
}
}
#endregion
#region 通用的异步返回类型
//ValueTask<int> task = Func();
//await foreach (var item in task)
//{
//}
public static async ValueTask<int> Func()
{
await Task.Delay(100);
return 5;
}
#endregion
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);
/// <summary>
/// C#8新语法
/// </summary>
/// <param name="week"></param>
/// <returns></returns>
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("枚举不存在"),
};
/// <summary>
///switch 经典switch
/// </summary>
/// <param name="week"></param>
/// <returns></returns>
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);
/// <summary>
///switch 属性模式
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
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");
/// <summary>
/// switch 元组模式
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
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();
/// <summary>
/// 异步流
/// </summary>
public async static void AsyncIEnumerableShow()
{
Console.WriteLine("********************异步流*********************");
IAsyncEnumerable<int> asynclist = GenerateSequence();
await foreach (var item in asynclist)
{
Console.WriteLine(item);
}
Console.WriteLine("********************异步流*********************");
}
public static async IAsyncEnumerable<int> 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<int> func1 = () => 1;
Func<int> 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
{
}
/// <summary>
/// 在C#9中覆写父类的方法,可以返回类型的子类
/// </summary>
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<int, int, int> zero = (a, b) => 0;
Func<int, int, int> func = delegate (int a, int b) { return 0; };
}
// C# 9
{
Func<int, int, int> zero = (_, _) => 0;
Func<int, int, int> func = delegate (int _, int _) { return 0; };
}