[TOC] <br/><br/><br/> # <b style="color:#4F4F4F;">简介说明</b> 结构脑图:[地址](http://naotu.baidu.com/file/f2bb85c58468f9bcbca228ecb8ff004b?token=1fa1b5a27a1e902e) 原文链接: - [Microsoft 技术文档](https://docs.microsoft.com/zh-cn/) - [.NET 中的程序集](https://docs.microsoft.com/zh-cn/dotnet/standard/assembly/) - [建议用于 C# 文档注释的 XML 标记](https://docs.microsoft.com/zh-cn/dotnet/csharp/codedoc) - [.NET 微服务](https://learn.microsoft.com/zh-cn/dotnet/architecture/microservices/) ``` 版本:C# 作用:简单的、现代的、通用的、面向对象的编程语言 ``` <br/> # <b style="color:#4F4F4F;">基本语法</b> <br/> # <span style="color:#619BE4">public</span> ***** 声明公共的内容 <br/> # <span style="color:#619BE4">private</span> ***** 声明私有的内容 <br/> # <span style="color:#619BE4">partial</span> ***** 声明部分的内容 <br/> # <span style="color:#619BE4">internal</span> ***** 用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问 <br/> # <span style="color:#619BE4">throw</span> ***** 抛出一个异常 <br/> # <span style="color:#619BE4">using static</span> ***** 导入一个静态对象 <br/> # <span style="color:#619BE4">using</span> ***** 导入一个命名空间 <br/> ### 参数说明 <b style="color:#808080;">static:</b> * 类型:字符串 * 默认值:无 * 描述:导入一个静态的方法 * 可选值:[ ] <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` // 自动调用Dispose()方法 using (var i = new MyDispose()) { i.Print(); }; ``` <br/> # <span style="color:#619BE4">switch</span> ***** 分支语句 <br/> ### 示例内容 <span style="color:red;">1. 类判断</span> ``` public static decimal ComputeSalesTax(Address location, decimal salePrice) { return location switch { { State: "WA" } => salePrice * 0.06M, { State: "MN" } => salePrice * 0.075M, { State: "MI" } => salePrice * 0.05M, _ => 0M }; } ``` <span style="color:red;">2. 位置模式</span> ``` static Quadrant GetQuadrant(Point point) => point switch { (0, 0) => Quadrant.Origin, var (x, y) when x > 0 && y > 0 => Quadrant.One, var (x, y) when x < 0 && y > 0 => Quadrant.Two, var (x, y) when x < 0 && y < 0 => Quadrant.Three, var (x, y) when x > 0 && y < 0 => Quadrant.Four, var (_, _) => Quadrant.OnBorder, _ => Quadrant.Unknown }; ``` <br/> # <span style="color:#619BE4">try catch when</span> ***** 当when满足时捕获异常 <br/> # <span style="color:#619BE4">$" "</span> ***** 使{ }里面的内容可以被替换 <br/> # <span style="color:#619BE4">@" "</span> ***** 字符串中需要转义内容可以直接写入 <br/> # <span style="color:#619BE4">type?</span> ***** 定义对象时预先声明对象可为空值 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` int?[] arr=new int?[7]; ``` <br/> # <span style="color:#619BE4">_</span> ***** 弃元,形参命名忽略占位符 <br/> # <span style="color:#619BE4">::</span> ***** 命名冲突解决方案 <br/> # <span style="color:#619BE4">?:</span> ***** 三元运算符 <br/> # <span style="color:#619BE4">??</span> ***** a??b 当a为null时则返回b,a不为null时则返回a本身 <br/> # <span style="color:#619BE4">??=</span> ***** 仅当左操作数计算为 null 时,才能使用运算符 ??= 将其右操作数的值分配给左操作数 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` List<int> numbers = null; int? i = null; numbers ??= new List<int>(); numbers.Add(i ??= 17); numbers.Add(i ??= 20); Console.WriteLine(string.Join(" ", numbers)); // output: 17 17 Console.WriteLine(i); // output: 17 ``` <br/> # <span style="color:#619BE4">?.</span> ***** 如果对象为NULL,则不进行后面的获取成员的运算,直接返回NULL <br/> # <span style="color:#619BE4">?[]</span> ***** 不存在索引不继续调用,直接返回null <br/> # <span style="color:#619BE4">..</span> ***** 范围运算符 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` 可以使用 ^1 索引检索最后一个词 Console.WriteLine($"The last word is {words[^1]}"); 它包括 words[1] 到 words[3]。 元素 words[4] 不在该范围内 var quickBrownFox = words[1..4]; ``` <br/> # <span style="color:#619BE4">-\></span> ***** 结构体指针语法糖 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` public struct Coords { public int X; public int Y; public override string ToString() => $"({X}, {Y})"; } public class PointerMemberAccessExample { public static unsafe void Main() { Coords coords; Coords* p = &coords; p->X = 3; p->Y = 4; Console.WriteLine(p->ToString()); // output: (3, 4) } } ``` <br/> # <span style="color:#619BE4">record</span> ***** 定义整个对象都是不可变的,且行为像一个值 <br/> # <span style="color:#619BE4">with</span> ***** 修改init属性值,生成副本 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` var person = new Person { FirstName = "Mads", LastName = "Nielsen" }; var otherPerson = person with { LastName = "Torgersen" }; ``` <span style="color:red;">2. 定义解构方法</span> ``` using System; namespace ConsoleApp2 { public record Person { public string FirstName { get; init; } public string LastName { get; init; } public Person(string firstName, string lastName) => (FirstName, LastName) = (firstName, lastName); public void Deconstruct(out string firstName, out string lastName) { (firstName, lastName) = (FirstName, LastName); } } class Program { static void Main(string[] args) { var person = new Person("a", "b"); var (a, b) = person; Console.WriteLine(a); Console.WriteLine(b); } } } ``` <br/> # <span style="color:#619BE4">var</span> ***** 让编译器编译时类型推断 <br/> # <span style="color:#619BE4">item</span> ***** 类定义item属性 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` int this[int index] { get; } ``` <br/> # <span style="color:#619BE4">implicit</span> ***** 确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换 <br/> # <span style="color:#619BE4">operator</span> ***** 操作符重载 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` class Trial1 { public static Trial1 operator +(Trial1 t1) { // 运算符重载重载 return new Trial1(); } public static implicit operator Trial2(Trial1 op1) { // 隐式转换重载 return new Trial2(); } } class Trial2 { public static explicit operator Trial1(Trial2 op2) { // 显式转换重载 return new Trial1(); } } ``` <br/> # <span style="color:#619BE4">override</span> ***** 重写父类方法 <br/> # <span style="color:#619BE4">new</span> ***** 隐藏父类方法 <br/> # <span style="color:#619BE4">dynamic</span> ***** 声明动态类型,编译时不进行类型检测,对象方法属性都是可变的 <br/> # <span style="color:#619BE4">struct</span> ***** 定义一个结构变量 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` class Program { struct Light { public Color color; public int size; } private static void Main(string[] args) { if (args is null) { throw new ArgumentNullException(nameof(args)); } Light myLight; myLight.size = 21; myLight.color = Color.RED; string testStr = JsonConvert.SerializeObject(myLight); Console.WriteLine(testStr); Console.ReadKey(); } } ``` <br/> # <span style="color:#619BE4">enum</span> ***** 定义枚举类型 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` using System; namespace ConsoleApp { enum Orientation : int { RED = 1, BLUE = 2, YELLOW = 3 } class Program { private static void Main(string[] args) { if (args is null) { throw new ArgumentNullException(nameof(args)); } Console.WriteLine($"{Orientation.BLUE.ToString()}"); Console.WriteLine($"{(int)Orientation.BLUE}"); Console.ReadKey(); } } } ``` <br/> # <span style="color:#619BE4">lambada</span> ***** 匿名函数 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` static int Add(int x, int y) => x + y; ``` <br/> # <span style="color:#619BE4">event</span> ***** 声明事件委托变量 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` using System; namespace Sample001 { public delegate void InformHandle(object sender); public class JIA { public event InformHandle EatOver; public void Eat() { Console.WriteLine("吃饭中......"); System.Threading.Thread.Sleep(2000); //吃饭事件两秒 OnEating(); //这个相当于是一个信号,当运行这个函数的时候会发出一个信号。 } public virtual void OnEating() { EatOver?.Invoke(this); } } public class YI { public string name = "yi"; public void TakeJiaToWangBa(object sender) { Console.WriteLine(name + "带" + sender.ToString() + "去网吧!"); } } public class BING { public string name = "bing"; public void TakeJiaToWangBa(object sender) { Console.WriteLine(name + "带" + sender.ToString() + "去网吧!"); } } class Program { static void Main(string[] args) { JIA jia1 = new JIA(); YI yi1 = new YI(); BING bing1 = new BING(); jia1.EatOver += new InformHandle(yi1.TakeJiaToWangBa); jia1.EatOver += new InformHandle(bing1.TakeJiaToWangBa); Console.WriteLine("空闲中"); Console.WriteLine("现在甲不知道在干什么"); jia1.Eat(); Console.WriteLine("去了网吧通宵一个晚上到了第二天中午"); jia1.Eat(); Console.ReadKey(); } } } ``` <br/> # <span style="color:#619BE4">delegate</span> ***** 委托函数 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` class Program { delegate int DelegateProcess(int x, int y); static int Add(int x, int y) => x + y; static int Subtraction(int x, int y) => x - y; private static void Main(string[] args) { if (args is null) { throw new ArgumentNullException(nameof(args)); } DelegateProcess process; string directive = Console.ReadLine(); if (directive.ToLower() == "m") { process = new DelegateProcess(Add); } else { process = new DelegateProcess(Subtraction); } Console.WriteLine($"{process(15, 23)}"); Console.ReadKey(); } } ``` <span style="color:red;">2. 接收委托</span> ``` namespace ConsoleApp { public delegate string MyCalculator(); class Trial { public void Add(MyCalculator culator) { Console.WriteLine(culator()); } } class Program { public static string Print() { return "hello"; } static void Main(string[] args) { Trial t1 = new Trial(); t1.Add(delegate () { return "abc"; }); MyCalculator c = new MyCalculator(Print); Trial t2 = new Trial(); t2.Add(c); Console.ReadKey(); } } } ``` <br/> # <span style="color:#619BE4">params</span> ***** 定义参数数组,可以传入任意数量参数 <br/> # <span style="color:#619BE4">ref</span> ***** 定义引用类型变量,无须返回,直接影响作用域变量值 <br/> # <span style="color:#619BE4">out</span> ***** 定义输出类型变量,和ref用法一样,但是能接收未初始化变量,直接影响作用域变量值 <br/> # <span style="color:#619BE4">base</span> ***** 调用继承后父类内容 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` using System; namespace ConsoleApp { class Parent { public Parent() { Console.WriteLine("hello parent"); } public void Print() { Console.WriteLine("print"); } } class Child : Parent { public Child() : base() { Console.WriteLine("hello child"); } public new void Print() // new 关键字代表覆盖掉父类方法 { base.Print(); Console.WriteLine("abc"); } } class Program { private static void Main(string[] args) { if (args is null) { throw new ArgumentNullException(nameof(args)); } new Child().Print(); Console.ReadKey(); } } } ``` <br/> # <span style="color:#619BE4">T</span> ***** 定义泛类型 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` public class MyClass<T> { readonly T innerT; public MyClass(T val) { innerT = val; Console.WriteLine(default(T)); } public void Print() { Console.WriteLine(innerT); } } class Program { private static void Main(string[] args) { new MyClass<int>(233).Print(); Console.ReadKey(); } } ``` <span style="color:red;">2. 泛型约束</span> ``` public class MyClass5<T,K,V,W,X,Y,Z> where T:struct //约束T必须是值类型 where K:class //约束K必须是引用类型 where V:IComparable //约束V必须实现IComparable,int就符合,原因是int类型实现了IComparable接口 where W:K //要求W必须是K类型或者K类型的子类 where X:class,new() //多个约束,其中new()表示无参数的构造函数,表示约束是引用类型并且有无参数的构造函数 { } ``` <br/> # <span style="color:#619BE4">contravariance</span> ***** 协变,关键字out,隐式改变T的类型 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` namespace ConsoleApp { public class RectangleBase { public int ID { get; set; } } public class Rectangle : RectangleBase { // 继承RectangleBase public string Name { get; set; } } public interface IIndex<out T> { T this[int index] { get; } int Count { get; } } public class RectangleCollection : IIndex<Rectangle> { List<Rectangle> list = new List<Rectangle>(); public Rectangle this[int index] { get { if (index < 0 || index > Count) throw new ArgumentOutOfRangeException("index"); return list[index]; } } public int Count { get { return list.Count; } } public void Add(Rectangle value) { list.Add(value); } } class Program { static void Main(string[] args) { RectangleCollection list = new RectangleCollection(); list.Add(new Rectangle { ID = 1, Name = "111" }); list.Add(new Rectangle { ID = 2, Name = "222" }); list.Add(new Rectangle { ID = 3, Name = "33" }); // RectangleCollection 同时也是 IIndex<Rectangle> // 如果接口去掉out,无法隐式改变成IIndex<RectangleBase> // 加上out关键字,代表可以隐式发生协变 IIndex<RectangleBase> Bas = list; for (int i = 0; i < Bas.Count; i++) { Console.WriteLine(Bas[i].ID); } Console.ReadKey(); } } } ``` <br/> # <span style="color:#619BE4">convariance</span> ***** 抗变,关键字in,抗变让 T 只能用作方法参数,不能用作返回内容 <br/> # <span style="color:#619BE4">new{}</span> ***** 快捷生成字典 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` var c = new { Name = 123 }; ``` <br/> # <span style="color:#619BE4">new[]</span> ***** 快捷生成列表 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` var c = new[] { 1,2,3,4,5,6 }; ``` <br/> # <span style="color:#619BE4">sealed</span> ***** 定义类无法被继承,无法继承的类 <br/> # <span style="color:#619BE4">kwargs</span> ***** 命名参数 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` class Trial { public void Add([Optional] int age, string name = "zyp") { Console.WriteLine(name); Console.WriteLine(age); } } class Program { static void Main(string[] args) { new Trial().Add(name: "kkp"); Console.ReadKey(); } } ``` <br/> # <span style="color:#619BE4">Extensions</span> ***** 静态方法拓展 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` public static class MyConfigurationBuilderExtensions { public static IConfigurationBuilder AddMyConfiguration(this IConfigurationBuilder builder) { builder.Add(new MyConfigurationSource()); return builder; } } ``` <br/> # <span style="color:#619BE4">is</span> ***** 检查类型是否兼容 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` System.Boolean b1 = (o is System.Object);//b1 为true System.Boolean b2 = (o is Employee);//b2为false ``` <br/> # <span style="color:#619BE4">field</span> ***** 使用它时会自动为属性创建字段定义 <br/> # <span style="color:#619BE4">global</span> ***** 定义一系列的类型别名在整个项目内使用 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ``` global using System.Linq; global using static System.Math; global using i32 = System.Int32; global using i64 = System.Int64; ``` <br/> # <span style="color:#619BE4">extern</span> ***** 声明在外部实现的方法 <br/> # <span style="color:#619BE4">nameof()</span> ***** 可生成变量、类型或成员的名称作为字符串常量 <br/> # <span style="color:#619BE4">typeof()</span> ***** 获取对象类型 <br/> ### 返回类型 ``` System.Type ``` <br/> # <span style="color:#619BE4">sizeof()</span> ***** 获取变量大小 <br/> # <span style="color:#619BE4">default()</span> ***** 获取该类型的默认值,同时把关键字赋值给变量同样可以得到默认值 <br/> # <span style="color:#619BE4">checked()</span> ***** 检查是否强制转换会产生数据溢出,溢出报异常 <br/> # <span style="color:#619BE4">unchecked()</span> ***** 不检查强制转换会产生数据溢出,直接自动舍去多余数据 <br/> # <b style="color:#4F4F4F;">特性内容</b> <br/> # <span style="color:#619BE4">编译过程</span> ***** 编译过程及原理图 <br/> ### 示例内容 <span style="color:red;">1. 举例说明</span> ![CSharp编译过程](https://img.kancloud.cn/da/10/da10ab213a4e16eaf8fcfd59be8cc104_637x429.png) <br/>