SortVersion 类

定义

提供有关用于比较和排序字符串的 Unicode 版本的信息。

public ref class SortVersion sealed : IEquatable<System::Globalization::SortVersion ^>
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
[System.Serializable]
public sealed class SortVersion : IEquatable<System.Globalization.SortVersion>
type SortVersion = class
    interface IEquatable<SortVersion>
[<System.Serializable>]
type SortVersion = class
    interface IEquatable<SortVersion>
Public NotInheritable Class SortVersion
Implements IEquatable(Of SortVersion)
继承
SortVersion
属性
实现

注解

排序和字符串比较

执行字符串比较时,所有版本的 .NET (Core) 都依赖于基础作系统。 因此,字符串比较的结果或字符串排序顺序取决于作系统在执行比较时使用的 Unicode 版本。 在 Linux、macOS 和 Windows 10 及更高版本上, Unicode 库的国际组件 提供用于比较和排序 API 的实现。

使用 SortVersion 类

SortVersion 类提供有关 .NET 用于字符串比较和排序的 Unicode 版本的信息。 它使开发人员能够编写可以检测和成功处理 Unicode 版本中的更改的应用程序,这些更改用于比较和排序应用程序的字符串。

可以通过两种方式实例化 SortVersion 对象:

  • 通过调用 SortVersion 构造函数来实例化基于版本号和排序 ID 的新 SortVersion 对象。 从保存的数据重新创建 SortVersion 对象时,此构造函数最有用。
  • 通过检索 CompareInfo.Version 属性的值。 此属性提供有关运行应用程序的 .NET 实现使用的 Unicode 版本的信息。

SortVersion 类具有两个属性:FullVersionSortId,分别指示用于字符串比较的 Unicode 版本和特定区域性。 该 FullVersion 属性是一个任意数值,反映用于字符串比较的 Unicode 版本,该 SortId 属性是一个任意 Guid 值,反映用于字符串比较的文化习俗。 仅当使用SortVersion方法、Equals运算符或Equality运算符比较两个对象时,这两Inequality个属性的值才很重要。

在保存或检索某种形式的区域性敏感有序字符串数据(例如索引或文本字符串本身)时,通常使用 SortVersion 对象。 这需要以下步骤:

  1. 保存有序字符串数据时,FullVersionSortId 的属性值也会被保存。

  2. 检索有序字符串数据时,可以通过调用SortVersion构造函数重新创建SortVersion用于对字符串进行排序的对象。

  3. 此新的 SortVersion 实例化对象与一个 SortVersion 对象进行比较,该对象反映出用于对字符串数据进行排序的文化约定。

  4. 如果两 SortVersion 个对象不相等,则必须对字符串数据重新排序。

示例

  The following example contains a portion of the source code from an application that uses the <xref:System.Globalization.SortVersion> class to ensure that the native names of <xref:System.Globalization.RegionInfo> objects are ordered appropriately for the current system and current culture. It uses the <xref:System.IO.BinaryReader> and <xref:System.IO.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 <xref:System.Globalization.SortVersion> object with the <xref:System.Globalization.SortVersion> object for the current culture on the current system. If they aren't equal, or if the `reindex` flag had been set previously, it resorts to the <xref:System.Globalization.RegionInfo> data.

  :::code language="csharp" source="~/snippets/csharp/System.Globalization/SortVersion/Overview/example1.cs" id="Snippet1":::
  :::code language="vb" source="~/snippets/visualbasic/System.Globalization/SortVersion/Overview/example1.vb" id="Snippet1":::

构造函数

名称 说明
SortVersion(Int32, Guid)

创建类的新实例 SortVersion

属性

名称 说明
FullVersion

获取对象的完整版本号 SortVersion

SortId

获取此 SortVersion 对象的全局唯一标识符。

方法

名称 说明
Equals(Object)

返回一个值,该值指示此 SortVersion 实例是否等于指定对象。

Equals(SortVersion)

返回一个值,该值指示此 SortVersion 实例是否等于指定 SortVersion 对象。

GetHashCode()

返回此实例的哈希代码。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

运营商

名称 说明
Equality(SortVersion, SortVersion)

指示两个 SortVersion 实例是否相等。

Inequality(SortVersion, SortVersion)

指示两 SortVersion 个实例是否不相等。

适用于

另请参阅