Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

StringComparer Class

Represents a string comparison operation that uses specific case and culture-based or ordinal comparison rules.

System.Object
  System.StringComparer

Namespace:  System
Assemblies:   System.Runtime.Extensions (in System.Runtime.Extensions.dll)
  mscorlib (in mscorlib.dll)

[<AbstractClass>]
type StringComparer =  
    class 
        interface IComparer<string>
        interface IEqualityComparer<string>
        interface IComparer 
        interface IEqualityComparer 
    end

The StringComparer type exposes the following members.

  NameDescription
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryStringComparerInitializes a new instance of the StringComparer class.
Top

  NameDescription
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCurrentCultureGets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the current culture.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCurrentCultureIgnoreCaseGets a StringComparer object that performs case-insensitive string comparisons using the word comparison rules of the current culture.
Public propertyStatic memberSupported by the XNA FrameworkInvariantCultureGets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture.
Public propertyStatic memberSupported by the XNA FrameworkInvariantCultureIgnoreCaseGets a StringComparer object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryOrdinalGets a StringComparer object that performs a case-sensitive ordinal string comparison.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryOrdinalIgnoreCaseGets a StringComparer object that performs a case-insensitive ordinal string comparison.
Top

  NameDescription
Public methodSupported by the XNA FrameworkCompare(Object, Object)When overridden in a derived class, compares two objects and returns an indication of their relative sort order.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCompare(String, String)When overridden in a derived class, compares two strings and returns an indication of their relative sort order.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCreateCreates a StringComparer object that compares strings according to the rules of a specified culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkEquals(Object, Object)When overridden in a derived class, indicates whether two objects are equal.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(String, String)When overridden in a derived class, indicates whether two strings are equal.
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCode()Serves as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCode(Object)When overridden in a derived class, gets the hash code for the specified object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCode(String)When overridden in a derived class, gets the hash code for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibraryIComparer.CompareCompares two objects and returns a value that indicates whether one is less than, equal to, or greater than the other.
Explicit interface implemetationPrivate methodSupported by Portable Class LibraryIEqualityComparer.EqualsDetermines whether the specified objects are equal.
Explicit interface implemetationPrivate methodSupported by Portable Class LibraryIEqualityComparer.GetHashCodeReturns a hash code for the specified object.
Top

An object derived from the StringComparer class embodies string-based comparison, equality, and hash code operations that take into account both case and culture-specific comparison rules. You can use the StringComparer class to create a type-specific comparison to sort the elements in a generic collection. Classes such as Hashtable, Dictionary(TKey, TValue), SortedList, and SortedList(TKey, TValue) use the StringComparer class for sorting purposes.

A comparison operation that is represented by the StringComparer class is defined to be either case-sensitive or case-insensitive, and use either word (culture-sensitive) or ordinal (culture-insensitive) comparison rules. For more information about word and ordinal comparison rules, see System.Globalization.CompareOptions.

Implemented Properties

You might be confused about how to use the StringComparer class properties because of a seeming contradiction. The StringComparer class is declared abstract (MustInherit in Visual Basic), which means its members can be invoked only on an object of a class derived from the StringComparer class. The contradiction is that each property of the StringComparer class is declared static (Shared in Visual Basic), which means the property can be invoked without first creating a derived class.

You can call a StringComparer property directly because each property actually returns an instance of an anonymous class that is derived from the StringComparer class. Consequently, the type of each property value is StringComparer, which is the base class of the anonymous class, not the type of the anonymous class itself. Each StringComparer class property returns a StringComparer object that supports predefined case and comparison rules.

The following example demonstrates the properties and the Create method of the StringComparer class. The example illustrates how different StringComparer objects sort three versions of the Latin letter I.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft