Comparer Comparer Comparer Comparer Class

정의

문자열의 대/소문자를 구분하여 두 개체를 비교합니다.Compares two objects for equivalence, where string comparisons are case-sensitive.

public ref class Comparer sealed : System::Collections::IComparer, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Comparer : System.Collections.IComparer, System.Runtime.Serialization.ISerializable
type Comparer = class
    interface IComparer
    interface ISerializable
Public NotInheritable Class Comparer
Implements IComparer, ISerializable
상속
ComparerComparerComparerComparer
특성
구현

예제

다음 코드 예제에서는 어떻게 Compare 연결 된 문화권에 따라 다른 값을 반환 합니다 Comparer합니다.The following code example shows how Compare returns different values depending on the culture associated with the 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 'SamplesComparer


'This code produces the following output.
'
'Comparing "llegar" and "lugar" ...
'   Invariant Comparer: -1
'   International Sort: -1
'   Traditional Sort  : 1

설명

이 클래스는 기본적으로는 IComparer 인터페이스입니다.This class is the default implementation of the IComparer interface. 합니다 CaseInsensitiveComparer 클래스의 구현인는 IComparer 대/소문자 구분 문자열 비교를 수행 하는 인터페이스입니다.The CaseInsensitiveComparer class is the implementation of the IComparer interface that performs case-insensitive string comparisons. System.Collections.Generic.Comparer<T> 이 클래스의 제네릭과 같습니다.System.Collections.Generic.Comparer<T> is the generic equivalent of this class.

비교 절차를 사용 합니다 Thread.CurrentCulture 현재 달리 지정 하지 않으면 스레드입니다.Comparison procedures use the Thread.CurrentCulture of the current thread unless otherwise specified. 문자열 비교는 문화권에 따라 다른 결과가 있을 수 있습니다.String comparisons might have different results depending on the culture. 문화권별 비교에 대 한 자세한 내용은 참조는 System.Globalization 네임 스페이스 및 전역화 및 지역화합니다.For more information on culture-specific comparisons, see the System.Globalization namespace and Globalization and Localization.

생성자

Comparer(CultureInfo) Comparer(CultureInfo) Comparer(CultureInfo) Comparer(CultureInfo)

지정된 Comparer를 사용하여 CultureInfo 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the Comparer class using the specified CultureInfo.

필드

Default Default Default Default

현재 스레드의 Comparer와 관련된 CurrentCulture의 인스턴스를 나타냅니다.Represents an instance of Comparer that is associated with the CurrentCulture of the current thread. 이 필드는 읽기 전용입니다.This field is read-only.

DefaultInvariant DefaultInvariant DefaultInvariant DefaultInvariant

Comparer와 관련된 InvariantCulture의 인스턴스를 나타냅니다.Represents an instance of Comparer that is associated with InvariantCulture. 이 필드는 읽기 전용입니다.This field is read-only.

메서드

Compare(Object, Object) Compare(Object, Object) Compare(Object, Object) Compare(Object, Object)

대/소문자를 구분하여 같은 형식의 두 개체를 비교한 다음 한 개체가 다른 개체보다 작은지, 큰지 또는 두 개체가 같은지 여부를 나타내는 값을 반환합니다.Performs a case-sensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

serialization에 필요한 데이터로 SerializationInfo 개체를 채웁니다.Populates a SerializationInfo object with the data required for serialization.

GetType() GetType() GetType() GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

현재 개체를 나타내는 문자열을 반환합니다.Returns a string that represents the current object.

(Inherited from Object)

적용 대상

추가 정보