Comparer 클래스

정의

문자열의 대/소문자를 구분하여 두 개체를 비교합니다.

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

예제

다음 코드 예제에서는 연결된 Comparer문화권에 따라 다른 값을 반환하는 방법을 Compare 보여줍니다.

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

설명

이 클래스는 인터페이스의 기본 구현입니다 IComparer . CaseInsensitiveComparer 클래스는 대/소문 IComparer 자를 구분하지 않는 문자열 비교를 수행하는 인터페이스의 구현입니다. System.Collections.Generic.Comparer<T> 는 이 클래스에 해당하는 제네릭입니다.

달리 지정하지 않는 한 비교 프로시저는 현재 스레드를 사용합니다 Thread.CurrentCulture . 문자열 비교는 문화권에 따라 다른 결과를 가질 수 있습니다. 문화권별 비교에 대한 자세한 내용은 네임스페이 System.Globalization 스, 세계화 및 지역화를 참조하세요.

생성자

Comparer(CultureInfo)

지정된 Comparer를 사용하여 CultureInfo 클래스의 새 인스턴스를 초기화합니다.

필드

Default

현재 스레드의 Comparer와 관련된 CurrentCulture의 인스턴스를 나타냅니다. 이 필드는 읽기 전용입니다.

DefaultInvariant

Comparer와 관련된 InvariantCulture의 인스턴스를 나타냅니다. 이 필드는 읽기 전용입니다.

메서드

Compare(Object, Object)

대/소문자를 구분하여 같은 형식의 두 개체를 비교한 다음 한 개체가 다른 개체보다 작은지, 큰지 또는 두 개체가 같은지 여부를 나타내는 값을 반환합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetObjectData(SerializationInfo, StreamingContext)

serialization에 필요한 데이터로 SerializationInfo 개체를 채웁니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보