<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                [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/>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看