Comparer<T>.Default 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回泛型引數指定之型別的預設排序次序比較子 (Comparer)。
public:
static property System::Collections::Generic::Comparer<T> ^ Default { System::Collections::Generic::Comparer<T> ^ get(); };
public static System.Collections.Generic.Comparer<T> Default { get; }
static member Default : System.Collections.Generic.Comparer<'T>
Public Shared ReadOnly Property Default As Comparer(Of T)
物件,繼承 Comparer<T>,並用做型別 T
的排序次序比較子。
下列範例示範如何使用 Default 屬性來取得執行預設比較的物件。 此範例是針對 類別提供的較大範例的 Comparer<T> 一部分。
// Get the default comparer that
// sorts first by the height.
Comparer<Box> defComp = Comparer<Box>.Default;
// Calling Boxes.Sort() with no parameter
// is the same as calling Boxs.Sort(defComp)
// because they are both using the default comparer.
Boxes.Sort();
foreach (Box bx in Boxes)
{
Console.WriteLine("{0}\t{1}\t{2}",
bx.Height.ToString(), bx.Length.ToString(),
bx.Width.ToString());
}
' Get the default comparer that
' sorts first by the height.
Dim defComp As Comparer(Of Box) = Comparer(Of Box).Default
' Calling Boxes.Sort() with no parameter
' is the same as calling Boxs.Sort(defComp)
' because they are both using the default comparer.
Boxes.Sort()
For Each bx As Box In Boxes
Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & "{2}", _
bx.Height.ToString(), _
bx.Length.ToString(), _
bx.Width.ToString())
Next bx
此屬性傳回的 會 Comparer<T> 使用 System.IComparable<T> C# 中的泛型介面 (IComparable<T>
, IComparable(Of T)
在 Visual Basic 中) 比較兩個物件。 如果類型 T
未實作 System.IComparable<T> 泛型介面,這個屬性會 Comparer<T> 傳回使用 介面的 System.IComparable 。
針對字串比較, StringComparer 建議在 Visual Basic) 中,將類別比 Comparer<String>
(Comparer(Of String)
。 類別 StringComparer 的屬性會傳回預先定義的實例,這些實例會使用不同文化特性和區分大小寫的組合來執行字串比較。 區分大小寫與文化特性敏感度在相同 StringComparer 實例的成員之間是一致的。
如需特定文化特性比較的詳細資訊,請參閱 System.Globalization 命名空間和 全球化和當地語系化。
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |