Partager via


Comparer<T>.Default Propriété

Définition

Retourne un comparateur d’ordre de tri par défaut pour le type spécifié par l’argument générique.

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)

Valeur de propriété

Objet qui hérite Comparer<T> et sert de comparateur d’ordre de tri pour le type T.

Exemples

L’exemple suivant montre comment utiliser la Default propriété pour obtenir un objet qui effectue la comparaison par défaut. Cet exemple fait partie d’un exemple plus large fourni pour la Comparer<T> classe.

// 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.
let defComp = Comparer<Box>.Default

// Calling Boxes.Sort() with no parameter
// is the same as calling boxes.Sort defComp
// because they are both using the default comparer.
boxes.Sort()

for bx in boxes do
    printfn $"{bx.Height}\t{bx.Length}\t{bx.Width}"
' 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

Remarques

La Comparer<T> propriété retournée utilise l’interface System.IComparable<T> générique (IComparable<T> en C#, IComparable(Of T) en Visual Basic) pour comparer deux objets. Si le type T n’implémente pas l’interface System.IComparable<T> générique, cette propriété retourne une Comparer<T> valeur qui utilise l’interface System.IComparable .

Notes pour les appelants

Pour les comparaisons de chaînes, la StringComparer classe est recommandée sur Comparer<String> (Comparer(Of String) en Visual Basic). Les propriétés de la StringComparer classe retournent des instances prédéfinies qui effectuent des comparaisons de chaînes avec différentes combinaisons de respect de la culture et de sensibilité à la casse. La sensibilité à la casse et la sensibilité à la culture sont cohérentes entre les membres de la même StringComparer instance.

Pour plus d’informations sur les comparaisons propres à la culture, consultez l’espace de noms et la System.Globalizationglobalisation et la localisation.

S’applique à

Voir aussi