頂級語句 將類和類裡面Main函數省略,只留下核心的邏輯代碼就是頂級語句! 1.頂級語句1 await System.Threading.Tasks.Task.Delay(1000); System.Console.WriteLine("Hi!"); return 0; static class $ ...
頂級語句
將類和類裡面Main函數省略,只留下核心的邏輯代碼就是頂級語句!
1.頂級語句1
await System.Threading.Tasks.Task.Delay(1000);
System.Console.WriteLine("Hi!");
return 0;
static class $Program
{
static async Task<int> $Main(string[] args)
{
await System.Threading.Tasks.Task.Delay(1000);
System.Console.WriteLine("Hi!");
return 0;
}
}
1.頂級語句2
System.Console.WriteLine("Hi!");
return 2;
static class $Program
{
static int $Main(string[] args)
{
System.Console.WriteLine("Hi!");
return 2;
}
}
Init
struct Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
}
init通過允許調用方在構造操作過程中改變成員,訪問器使不可變對象更具靈活性。 這意味著對象的不可變屬性可以參與對象初始值設定項,因此不再需要類型中的所有構造函數樣板。 Point類型現在只是:
struct Point
{
public int X { get; init; }
public int Y { get; init; }
}
然後,使用者可以使用對象初始值設定項來創建對象。
var p = new Point() { X = 42, Y = 13 };
記錄
記錄類型是引用類型,類似於類聲明。 如果不包含,記錄將提供一個錯誤 record_base argument_list record_declaration parameter_list 。 部分記錄最多只能有一個分部類型聲明提供 parameter_list 。
記錄參數不能 ref 使用 out 或 this 修飾符 (但 in params 允許) 。
繼承
除非類為 object ,且類不能從記錄繼承,否則記錄不能從類繼承。 記錄可以繼承自其他記錄。
public record Student(string name,int age) {
}
using System;
using System.Collections.Generic;
public class Student : IEquatable<Student>
{
#region 屬性這部分 可以看到set屬性沒有
#記錄具有不可變性,記錄一旦初始化完成,那麼它的屬性值將不可修改(可以通過反射修改)
public string Name { get; init; }
public int Age { get; init; }
#endregion
#region
protected virtual Type EqualityContract => typeof(Student);
#endregion
public override bool Equals(object? obj) => Equals(obj as R1);
public virtual bool Equals(Student? other)
{
return !(other is null) &&
EqualityContract == other.EqualityContract &&
EqualityComparer<string>.Default.Equals(this.Name, other.Name) &&
EqualityComparer<int>.Default.Equals(this.Age, other.Age);
}
public static bool operator ==(R1? left, R1? right)
=> (object)left == right || (left?.Equals(right) ?? false);
public static bool operator !=(R1? left, R1? right)
=> !(left == right);
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Student");
builder.Append(" { ");
if (this.PrintMembers(builder))
{
builder.Append(" ");
}
builder.Append("}");
return builder.ToString();
}
}
支持解構
public record Student(string Name, int Age) {
public string Name { get; set; }
public int Age { get; set; }
}
Student record = new Student("張三",19) ;
var (name, age) = record;
==》這個可以用在switch匹配上 還記得我們
string value = record switch{
("張三", 19) => "01",
("李四", 25) => "02",
_ => "default"
};
## 之所以可以用這個這麼用 是因為編譯後多了這樣的解構函數
public void Deconstruct(out string Name, out int Age) => (Name, Age) = (this.Name, this.Age);
with 表達式
with表達式是使用以下語法的新表達式。
Student record2 = record with { Name = "王五" };
//在編譯後等價於
var temp = record.<Clone>$();
temp.Name = "王五";
record2 = temp;
//不賦值 完全克隆
Student record3 = record with { };
好處:
1.比較兩個屬性是否相等跟屬性設置的順序無關
2.方便快速克隆,不影響原數據
3.補充了結構體不支持繼承以及性能不高的短處
Lambda 棄元參數
允許丟棄 (用作 _ lambda 和匿名方法的參數) 。 例如:
- lambda: (_, _) => 0 , (int _, int _) => 0
- 匿名方法: delegate(int _, int _)
public delegate void FuncA(string a, string c);
static void Main(string[] args)
{
FuncA func = (_, _) => { };
}