CompareInfo.GetCompareInfo Method

Definition

Initializes a new CompareInfo object.

Overloads

GetCompareInfo(Int32)

Initializes a new CompareInfo object that is associated with the culture with the specified identifier.

GetCompareInfo(String)

Initializes a new CompareInfo object that is associated with the culture with the specified name.

GetCompareInfo(Int32, Assembly)

Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.

GetCompareInfo(String, Assembly)

Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.

GetCompareInfo(Int32)

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Initializes a new CompareInfo object that is associated with the culture with the specified identifier.

C#
public static System.Globalization.CompareInfo GetCompareInfo(int culture);

Parameters

culture
Int32

An integer representing the culture identifier.

Returns

A new CompareInfo object associated with the culture with the specified identifier and using string comparison methods in the current Assembly.

Examples

The following example compares portions of two strings using the different CompareInfo objects:

C#
// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.

using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, myStr2 ) );
   }
}


/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetCompareInfo(String)

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Initializes a new CompareInfo object that is associated with the culture with the specified name.

C#
public static System.Globalization.CompareInfo GetCompareInfo(string name);

Parameters

name
String

A string representing the culture name.

Returns

A new CompareInfo object associated with the culture with the specified identifier and using string comparison methods in the current Assembly.

Exceptions

name is null.

name is an invalid culture name.

Examples

The following example compares portions of two strings using the different CompareInfo objects:

C#
// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.

using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, myStr2 ) );
   }
}


/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCompareInfo(Int32, Assembly)

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.

C#
public static System.Globalization.CompareInfo GetCompareInfo(int culture, System.Reflection.Assembly assembly);

Parameters

culture
Int32

An integer representing the culture identifier.

assembly
Assembly

An Assembly that contains the string comparison methods to use.

Returns

A new CompareInfo object associated with the culture with the specified identifier and using string comparison methods in the current Assembly.

Exceptions

assembly is null.

assembly is of an invalid type.

Remarks

Note

The behavior of this method is unpredictable. It is recommended for your application to use a version of this method that does not take an assembly input.

The assembly parameter must be of the same type as Module.Assembly.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetCompareInfo(String, Assembly)

Source:
CompareInfo.cs
Source:
CompareInfo.cs
Source:
CompareInfo.cs

Initializes a new CompareInfo object that is associated with the specified culture and that uses string comparison methods in the specified Assembly.

C#
public static System.Globalization.CompareInfo GetCompareInfo(string name, System.Reflection.Assembly assembly);

Parameters

name
String

A string representing the culture name.

assembly
Assembly

An Assembly that contains the string comparison methods to use.

Returns

A new CompareInfo object associated with the culture with the specified identifier and using string comparison methods in the current Assembly.

Exceptions

name is null.

-or-

assembly is null.

name is an invalid culture name.

-or-

assembly is of an invalid type.

Remarks

Note

The behavior of this method is unpredictable. We recommend that you use a version of this method that does not take an assembly input.

The assembly parameter must be of the same type as Module.Assembly.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1