Comparer<T>.Default 屬性

定義

傳回泛型引數指定之型別的預設排序次序比較子 (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 命名空間和 全球化和當地語系化

適用於

另請參閱