1. 程式人生 > >[C#.NET 拾遺補漏]05:操作符的幾個騷操作

[C#.NET 拾遺補漏]05:操作符的幾個騷操作

閱讀本文大概需要 1.5 分鐘。

大家好,這是極客精神【C#.NET 拾遺補漏】專輯的第 5 篇文章,今天要講的內容是操作符。 操作符的英文是 `Operator`,在數值計算中習慣性的被叫作運算子,所以在中文的概念中,運算子是操作符的一個子集。 操作符是很基礎的知識了,基礎歸基礎,我們來回顧一下 C# 操作符那些比較騷的操作,能想到的不多,請大家補充。 ## 操作符的過載 操作符過載大部分語言都沒有,而 C# 有。C# 允許使用者定義型別對操作符進行過載,方式是使用 `operate` 關鍵字把操作符寫成公開靜態函式。下面來演示一下過載 `+` 這個操作符。 我們建立一個 Complex 結構型別來代表一個複數,我們知道複數有實數和虛陣列成,於是可以這樣定義: ```cs public struct Complex { public double Real { get; set; } public double Imaginary { get; set; } } ``` 現在我們想實現複數的相加操作,即: ```cs Complex a = new Complex() { Real = 1, Imaginary = 2 }; Complex b = new Complex() { Real = 4, Imaginary = 8 }; Complex c = a + b; ``` 預設情況,自定義類是不能進行算術運算的,以上 `a + b` 會編譯報錯,我們需要對 `+` 進行操作符過載: ```cs public static Complex operator +(Complex c1, Complex c2) { return new Complex { Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary }; } ``` C# 中像加減乘除等這類操作符都可以過載,也有些操作符是不能過載的,具體請檢視文末參考連結。 ## 隱式和顯式轉換操作符 我們知道子類可以隱式轉換為父類,在某種情況下(如父類由子類賦值而來)父類可以顯式轉換為子類。 在 C# 中,對於沒有子父類關係的使用者定義型別,也是可以實現顯式和隱式轉換的。C# 允許使用者定義型別通過使用 `implicit` 和 `explicit` 關鍵字來控制物件的賦值和物件的型別轉換。它的定義形式如下: ```cs public static
operator <結果型別>(<源型別> myType) ``` 這裡以結果型別為方法名,源型別物件作為引數,只能是這一個引數,不能定義第二個引數,但可以通過該引數物件訪問其類的私有成員。下面是一個既有顯式又有隱式轉換操作符的例子: ```cs public class BinaryImage { private readonly bool[] _pixels; // 隱式轉換操作符示例 public static implicit operator ColorImage(BinaryImage bm) { return new ColorImage(bm); } // 顯式轉換操作符示例 public static explicit operator bool[](BinaryImage bm) { return bm._pixels; } } public class ColorImage { public ColorImage(BinaryImage bm) { } } ``` 這樣,我們就可以把 BinaryImage 物件隱式轉換為 ColorImage 物件,把 BinaryImage 物件顯式轉換為 bool 陣列物件: ```cs var binaryImage = new BinaryImage(); ColorImage colorImage = binaryImage; // 隱式轉換 bool[] pixels = (bool[])binaryImage; // 顯式轉換 ``` 而且轉換操作符可以定義為雙向顯示和隱式轉換。既可從你的型別而來,亦可到你的型別而去: ```cs public class BinaryImage { public BinaryImage(ColorImage cm) { } public static implicit operator ColorImage(BinaryImage bm) { return new ColorImage(bm); } public static explicit operator BinaryImage(ColorImage cm) { return new BinaryImage(cm); } } ``` 我們知道 as 操作符也是一種顯式轉換操作符,那它適用於上面的這種情況嗎,即: ```cs ColorImage cm = myBinaryImage as ColorImage; ``` 你覺得這樣寫有問題嗎?請在評論區告訴我答案。 ## 空條件和空聯合操作符 空條件(Null Conditional)操作符 `?.` 和空聯合(Null Coalescing)操作符 `??`,都是 C# 6.0 的語法,大多數人都很熟悉了,使用也很簡單。 `?.` 操作符會在物件為 null 時立即返回 null,不為 null 時才會呼叫後面的程式碼。其中的符號 `?` 代表物件本身,符號 `.` 代表呼叫,後面不僅可以是物件的屬性也可以是索引器或方法。以該操作符為分隔的每一截型別相同時可以接龍。示例: ```cs var bar = foo?.Value; // 相當於 foo == null ? null : foo.Value var bar = foo?.StringValue?.ToString(); // 每一截型別相同支援接龍 var bar = foo?.IntValue?.ToString(); // 每一截型別不同,不能接龍,因為結果型別無法確定 ``` 如果是呼叫索引器,則不需要符號 `.`,比如: ```cs var foo = new[] { 1, 2, 3 }; var bar = foo?[1]; // 相當於 foo == null ? null : foo[1] ``` 空聯合操作符 `??`,當左邊為空時則返回右邊的值,否則返回左邊的值。同樣,每一截的型別相同時支援接龍。 ```cs var fizz = foo.GetBar() ?? bar; var buzz = foo ?? bar ?? fizz; ``` ## => Lambda 操作符 Lambda 操作符,即 `=>`,它用來定義 Lambda 表示式,也被廣泛用於 LINQ 查詢。它的一般定義形式如下: ```cs (input parameters) => expression ``` 示例: ```cs string[] words = { "cherry", "apple", "blueberry" }; int minLength = words.Min((string w) => w.Length); ``` 實際應用中我們一般省略引數的型別宣告: ```cs int minLength = words.Min(w => w.Length); ``` Lambda 操作符的後面可以是表示式,可以是語句,也可以是語句塊,比如: ```cs // 表示式 (int x, int y) => x + y // 語句 (string x) => Console.WriteLine(x) // 語句塊 (string x) => { x += " says Hello!"; Console.WriteLine(x); } ``` 這個操作符也可以很方便的用來定義委託方法(其實 Lambda 操作符就是由委託演變而來)。 單獨定義委託方法: ```cs void MyMethod(string s) { Console.WriteLine(s + " World"); } delegate void TestDelegate(string s); TestDelegate myDelegate = MyMethod; myDelegate("Hello"); ``` 使用 Lambda 操作符: ```cs delegate void TestDelegate(string s); TestDelegate myDelegate = s => Console.WriteLine(s + " World"); myDelegate("Hello"); ``` 在一個類中,當實現體只有一句程式碼時,也可以用 Lambda 操作符對方法和 Setter / Getter 進行簡寫: ```cs public class Test { public int MyProp { get => 123; } public void MyMethod() => Console.WriteLine("Hello!"); } ``` 以上是幾種比較有代表性的操作符的“騷”操作,當然還有,但大多都過於基礎,大家都知道,就不總結了。 C# 雖然目前不是最受歡迎的語言,但確實是一門優美的語言,其中少不了這些操作符語法糖帶來的功勞。 參考:https://bit.ly/3h5yKNr