Comparer(CultureInfo) Costruttore
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe Comparer usando il CultureInfo specificato.
public:
Comparer(System::Globalization::CultureInfo ^ culture);
public Comparer (System.Globalization.CultureInfo culture);
new System.Collections.Comparer : System.Globalization.CultureInfo -> System.Collections.Comparer
Public Sub New (culture As CultureInfo)
Parametri
- culture
- CultureInfo
CultureInfo da utilizzare per la nuova Comparer.
Eccezioni
culture
è null
.
Esempio
Nell'esempio di codice seguente viene illustrato come Compare restituisce valori diversi a seconda delle impostazioni cultura associate a Comparer.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
// Creates the strings to compare.
String^ str1 = "llegar";
String^ str2 = "lugar";
Console::WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );
// Uses the DefaultInvariant Comparer.
Console::WriteLine( " Invariant Comparer: {0}", Comparer::DefaultInvariant->Compare( str1, str2 ) );
// Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
Comparer^ myCompIntl = gcnew Comparer( gcnew CultureInfo( "es-ES",false ) );
Console::WriteLine( " International Sort: {0}", myCompIntl->Compare( str1, str2 ) );
// Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
Comparer^ myCompTrad = gcnew Comparer( gcnew CultureInfo( 0x040A,false ) );
Console::WriteLine( " Traditional Sort : {0}", myCompTrad->Compare( str1, str2 ) );
}
/*
This code produces the following output.
Comparing "llegar" and "lugar" ...
Invariant Comparer: -1
International Sort: -1
Traditional Sort : 1
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesComparer {
public static void Main() {
// Creates the strings to compare.
String str1 = "llegar";
String str2 = "lugar";
Console.WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 );
// Uses the DefaultInvariant Comparer.
Console.WriteLine( " Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare( str1, str2 ) );
// Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
Comparer myCompIntl = new Comparer( new CultureInfo( "es-ES", false ) );
Console.WriteLine( " International Sort: {0}", myCompIntl.Compare( str1, str2 ) );
// Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
Comparer myCompTrad = new Comparer( new CultureInfo( 0x040A, false ) );
Console.WriteLine( " Traditional Sort : {0}", myCompTrad.Compare( str1, str2 ) );
}
}
/*
This code produces the following output.
Comparing "llegar" and "lugar" ...
Invariant Comparer: -1
International Sort: -1
Traditional Sort : 1
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesComparer
Public Shared Sub Main()
' Creates the strings to compare.
Dim str1 As [String] = "llegar"
Dim str2 As [String] = "lugar"
Console.WriteLine("Comparing ""{0}"" and ""{1}"" ...", str1, str2)
' Uses the DefaultInvariant Comparer.
Console.WriteLine(" Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare(str1, str2))
' Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort).
Dim myCompIntl As New Comparer(New CultureInfo("es-ES", False))
Console.WriteLine(" International Sort: {0}", myCompIntl.Compare(str1, str2))
' Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort).
Dim myCompTrad As New Comparer(New CultureInfo(&H40A, False))
Console.WriteLine(" Traditional Sort : {0}", myCompTrad.Compare(str1, str2))
End Sub
End Class
'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
' Invariant Comparer: -1
' International Sort: -1
' Traditional Sort : 1
Commenti
Le procedure di confronto usano l'oggetto specificato System.Globalization.CultureInfo per determinare l'ordine di ordinamento e le regole di maiuscole e minuscole. I confronti di stringhe potrebbero avere risultati diversi a seconda delle impostazioni cultura. Per altre informazioni sui confronti specifici delle impostazioni cultura, vedere lo spazio dei nomi e la globalizzazione e laSystem.Globalization localizzazione.