Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SortKey.ToString Method ()


Returns a string that represents the current SortKey object.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public override string ToString()

Return Value

Type: System.String

A string that represents the current SortKey object.

The return value is the concatenation of the string "SortKey - ", the culture identifier and compare options of the current SortKey object, and the value of the OriginalString property.

This method overrides Object.ToString.

The following code example displays an original string, the strings yielded by the ToString method for case-sensitive and case-insensitive sort keys, and the key data for case-sensitive and case-insensitive sort keys.

// This code example demonstrates the 
// GetSortKey() and ToString() methods, and the 
// OriginalString and KeyData properties of the 
// System.Globalization.SortKey class.

using System;
using System.Globalization;

class Sample 
    public static void Main() 
    CompareInfo cmpi = null;
    SortKey sk1 = null;
    SortKey sk2 = null;
    string s = "ABC";
    string ignoreCase = "Ignore case";
    string useCase    = "Use case   ";

// Get a CompareInfo object for the English-Great Britain culture.
    cmpi = CompareInfo.GetCompareInfo("en-GB");

// Get a sort key that ignores case for the specified string.
    sk1 = cmpi.GetSortKey(s, CompareOptions.IgnoreCase);
// Get a sort key with no compare option for the specified string.
    sk2 = cmpi.GetSortKey(s);

// Display the original string.
    Console.WriteLine("Original string: \"{0}\"", sk1.OriginalString);

// Display the the string equivalent of the two sort keys.
    Console.WriteLine("CompareInfo (culture) name: {0}", cmpi.Name);
    Console.WriteLine("ToString - {0}: \"{1}\"", ignoreCase, sk1.ToString());
    Console.WriteLine("ToString - {0}: \"{1}\"", useCase, sk2.ToString());

// Display the key data of the two sort keys.
    DisplayKeyData(sk1, ignoreCase);
    DisplayKeyData(sk2, useCase);

    public static void DisplayKeyData(SortKey sk, string title)
    Console.Write("Key Data - {0}: ", title);
    foreach (byte keyDatum in sk.KeyData)
        Console.Write("0x{0} ", (uint)keyDatum);
This code example produces the following results:

Original string: "ABC"

CompareInfo (culture) name: en-GB
ToString - Ignore case: "SortKey - 2057, IgnoreCase, ABC"
ToString - Use case   : "SortKey - 2057, None, ABC"

Key Data - Ignore case: 0x14 0x2 0x14 0x9 0x14 0x10 0x1 0x1 0x1 0x1 0x0
Key Data - Use case   : 0x14 0x2 0x14 0x9 0x14 0x10 0x1 0x1 0x18 0x18 0x18 0x1 0x1 0x0


.NET Framework
Available since 1.1
Return to top
© 2017 Microsoft