Provides information about the version of Unicode used to compare and order strings.
Assembly: mscorlib (in mscorlib.dll)
Returns a value that indicates whether this Object.Equals(Object).)instance is equal to a specified object.(Overrides
Returns a value that indicates whether thisinstance is equal to a specified object.
Returns a hash code for this instance.(Overrides Object.GetHashCode().)
Returns a string that represents the current object.(Inherited from Object.)
From the .NET Framework 2.0 Service Pack 1 through the .NET Framework 4, each version of the.NET Framework has included tables that contain sort weights and data on string normalization and that are based on a particular version of Unicode. In the .NET Framework 4.5, the presence of these tables depends on the operating system:
On Windows 7 and previous versions of the Windows operating system, the tables continue to be used for comparing and ordering strings.
On Windows 8, the .NET Framework delegates string comparison and ordering operations to the operating system.
Consequently, the result of a string comparison can depend not only on the .NET Framework version, but also on the operating system version, as the following table shows. Note that this list of supported Unicode versions applies to character comparison and sorting only; it does not apply to classification of Unicode characters by category.
.NET Framework version
.NET Framework 4
All operating systems
.NET Framework 4.5 and later versions of the .NET Framework
.NET Framework 4.5 and later versions of the .NET Framework
Windows 8 and later Windows operating system versions
On Windows 8, because the version of Unicode used in string comparison and ordering depends on the version of the operating system, the results of string comparison may differ even for applications that run on a specific version of the .NET Framework.
Theclass provides information about the Unicode version used by the .NET Framework for string comparison and ordering. It enables developers to write applications that can detect and successfully handle changes in the version of Unicode that is used to compare and sort an application's strings.
You can instantiate aobject in two ways:
By calling the SortVersion constructor, which instantiates a new object based on a version number and sort ID. This constructor is most useful when recreating a object from saved data.
By retrieving the value of the CompareInfo.Version property. This property provides information about the Unicode version used by the .NET Framework on which the application is running.
The FullVersion and SortId, that indicate the Unicode version and the specific culture used for string comparison. The FullVersion property is an arbitrary numeric value that reflects the Unicode version used for string comparison, and the SortId property is an arbitrary Guid that reflects the culture whose conventions are used for string comparison. The values of these two properties are important only when you compare two objects by using the Equals method, the Equality operator, or the Inequality operator.class has two properties,
You typically use aobject when saving or retrieving some form of culture-sensitive, ordered string data, such as indexes or the literal strings themselves. This requires the following steps:
When the ordered string data is retrieved, you can recreate the SortVersion constructor.object used for ordering the strings by calling the
This newly instantiatedobject is compared with a object that reflects the culture whose conventions are used to order the string data.
If the twoobjects are not equal, the string data must be reordered.
The example provides an illustration.
The following example contains a portion of the source code from an application that uses the RegionInfo objects are ordered appropriately for the current system and current culture. It uses the BinaryReader and BinaryWriter objects to store and retrieve ordered data from a data file named Regions.dat rather than retrieving and ordering data each time the application is run. The example first checks to determine whether the data file exists. If it does not, it creates the data and sets the reindex flag, which indicates that the data must be resorted and saved again. Otherwise, it retrieves the data and compares the saved object with the object for the current culture on the current system. If they are not equal, or if the reindex flag had been set previously, it resorts the RegionInfo data.class to ensure that the native names of
Imports System.Collections Imports System.Collections.Generic Imports System.Globalization Imports System.IO Imports System.Text Public Class Example : Implements IComparer Private Const FILENAME As String = ".\Regions.dat" Private Structure Region Friend Sub New(id As String, name As String) Me.Id = id Me.NativeName = name End Sub Dim Id As String Dim NativeName As String Public Overrides Function ToString() As String Return Me.NativeName End Function End Structure Public Shared Sub Main() Dim reindex As Boolean = False Dim regions() As Region Dim ver As SortVersion = Nothing ' If the data has not been saved, create it. If Not File.Exists(FILENAME) Then regions = GenerateData() ver = CultureInfo.CurrentCulture.CompareInfo.Version reindex = True ' Retrieve the existing data. Else regions = RestoreData(ver) End If ' Determine whether the current ordering is valid; if not, reorder. If reindex OrElse ver <> CultureInfo.CurrentCulture.CompareInfo.Version Then Array.Sort(regions, New Example()) ' Save newly reordered data. SaveData(regions) End If ' Continue with application... End Sub Private Shared Function GenerateData() As Region() Dim regions As New List(Of Region)() For Each culture In CultureInfo.GetCultures(CultureTypes.AllCultures) If culture.IsNeutralCulture Or culture.Equals(CultureInfo.InvariantCulture) Then Continue For Dim region As New RegionInfo(culture.Name) regions.Add(New Region(region.Name, region.NativeName)) Next Return regions.ToArray() End Function Private Shared Function RestoreData(ByRef ver As SortVersion) As Region() Dim regions As New List(Of Region) Dim rdr As New BinaryReader(File.Open(FILENAME, FileMode.Open)) Dim sortVer As Integer = rdr.ReadInt32 Dim sortId As Guid = Guid.Parse(rdr.ReadString()) ver = New SortVersion(sortVer, sortId) Dim id As String, name As String Do While rdr.PeekChar <> -1 id = rdr.ReadString() name = rdr.ReadString() regions.Add(New Region(id, name)) Loop Return regions.ToArray() End Function Private Shared Sub SaveData(regions As Region()) Dim ver As SortVersion = CultureInfo.CurrentCulture.CompareInfo.Version Dim wrtr As New BinaryWriter(File.Open(FILENAME, FileMode.Create)) wrtr.Write(ver.FullVersion) wrtr.Write(ver.SortId.ToString()) For Each region In regions wrtr.Write(region.Id) wrtr.Write(region.NativeName) Next wrtr.Close() End Sub Private Function SortByNativeName(o1 As Object, o2 As Object) As Integer _ Implements IComparer.Compare ' Assume that all conversions succeed. Dim r1 As Region = CType(o1, Region) Dim r2 As Region = CType(o2, Region) Return String.Compare(r1.NativeName, r2.NativeName, StringComparison.CurrentCulture) End Function End Class
Available since 4.5
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.