文章目錄
- 項目地址
- 一、基礎50
- 1.1 new keyword
- 1.2 static class vs. static method
- 1. static class
- 2. static method
- 3. static constructor 靜態構造函數
- 4. 靜態成員的生命周期
- 1.3 LinQ
- 1.what is LinQ
- 2. List<T>、IEnumerable<T>、IQueryable<T>
- 3. 在數據庫里用 IEnumerable
- 1.4 ==和equals
- 1. ==
- 2. Equals
- 1.5 綜合
- 1. Property vs. Field
- 2. const和readonly
項目地址
- 教程作者:
- 教程地址:
- 代碼倉庫地址:
- 所用到的框架和插件:
dbt
airflow
一、基礎50
1.1 new keyword
- new operator
- new modifier
- new constraint
1.2 static class vs. static method
1. static class
- 只能包含靜態成員(字段、方法、屬性、事件)
- 不能實例化(new 不出來)
- 不能繼承或被繼承(sealed + abstract 的結合體)
- 默認是 sealed(防止繼承)
- 在程序第一次使用時,CLR 會初始化一次(靜態構造函數)
2. static method
- 屬于類的本身,而不是對象成員
- 不能訪問實例成員
3. static constructor 靜態構造函數
- 沒有訪問修飾符,沒有參數
- CLR 在 第一次訪問類 時調用一次
- 用于初始化靜態字段
- 不能手動調用
public class Logger
{public static string FilePath;static Logger(){FilePath = "log.txt";Console.WriteLine("Static constructor called");}
}
4. 靜態成員的生命周期
靜態字段在 應用程序域(AppDomain)級別唯一
1.3 LinQ
1.what is LinQ
- LINQ is a set of technologies that allow simple and efficient querying over different kinds of data
- There are two ways of querying data: ①query syntax ;② method syntax
2. List、IEnumerable、IQueryable
- List → 具體集合類,數據已經在內存。
- IEnumerable → 內存中逐個枚舉,延遲執行,適合集合操作。
- IQueryable → 針對數據庫等遠程源,查詢會被翻譯成 SQL 執行,更高效。
3. 在數據庫里用 IEnumerable
- 如果你在數據庫中使用 IEnumerable,查詢會在內存中執行,導致先把整張表的數據拉到本地再過濾,非常低效;
- 而 IQueryable 會把查詢翻譯成 SQL 在數據庫端執行,性能更好。
1.4 ==和equals
1. ==
- == 默認比較對象引用,值類型比較值
- 用途:檢查 兩個對象的引用是否相同(默認情況下)。
- 可被重載:像 string、int 等類型重載了 ==,會比較值而不是引用。
- 適合:值類型或經過重載的引用類型。
2. Equals
- Equals 默認也比較引用,但可以重寫來比較對象的邏輯內容
class Person
{public string Name { get; set; }public override bool Equals(object obj){return obj is Person p && this.Name == p.Name;}
}
1.5 綜合
1. Property vs. Field
- A property is a way to encapsulate a field and provide controlled access to it through get and set methods, allowing you to enforce validation, read-only or write-only behavior, and maintain encapsulation.
2. const和readonly
- const is a compile-time constant that’s implicitly static, while readonly is a runtime constant that can be assigned at declaration or in a constructor but not changed afterward.