or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
LINQ基礎 - IEquatable vs IEqualityComparer And IComparable vs IComparer
這四個介面通常是作用在參考型別.
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →IEquatable
IEquatable 是在 C# 2.0 加入的. 它的主要用途是提供實作此介面的類別物件具有判斷自己與另一個與自己相同類別的物件是否相等的能力. 所以實現 IEquatable 介面的類別都必須實作 Equals 方法.
bool Equals(T other);
微軟的文件建議若是我們實作 IEquatable 介面 , 則順便 override Equals() & GetHashCode(). 以避免未預期的結果發生.
IEqualityComparer
IEqualityComparer是在 C# 2.0 加入的. 實作該介面的類別物件具有判斷某相同類別的兩個物件是否相等的能力. 實作IEquatable 的類別物件自身就能夠判斷自己是否與另外一個物件相等. 而實作 IEqualityComparer 的類別物件則是專門負責比較的比較器 , 通常是以不希望更改原類別為前提 , 但又希望可以比較該類別是否相等的狀況下使用.
IEqualityComparer 必須實作下列兩個方法
IComparable
IComparable 必須實現 CompareTo 方法
輸出結果
Id = 1 Name = 綠
Id = 2 Name = 王
Id = 3 Name = 王
IComparer
IComparer 使用在參考型態的自定義排序. (在 C# 3.5 前 , 可能在 Sort 方法的時候會使用) 但在 LinQ 的 ThenBy , OrderBy …出來後 , 使用機率會少很多. LinQ 仍然有接受 IComparer 作為參數的方法.
輸出結果
與 LinQ 的關聯
一些 LinQ 的方法可能會需要知道兩個項目是否相等或者需要比較兩個項目的大小.
參考
補充 : 製造泛型的 IEqualityComparer
補充 : EqualityComparer<T>.Default 屬性
依照泛型來決定回傳哪一個已被微軟定義好的比較器.
Thank you!
You can find me on
若有謬誤 , 煩請告知 , 新手發帖請多包涵
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →