Guid.IComparable.CompareTo(Object) Method

Definition

将此实例与指定 Guid 对象进行比较并返回它们的相对值。Compares this instance to a specified Guid object and returns an indication of their relative values.

 virtual int System.IComparable.CompareTo(System::Object ^ value) = IComparable::CompareTo;
int IComparable.CompareTo (object value);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (value As Object) As Integer Implements IComparable.CompareTo

Parameters

value
Object

要与此实例进行比较的对象。An object to compare to this instance.

Returns

Int32

一个带符号数字,指示此实例和 value 的相对值。A signed number indicating the relative values of this instance and value.

返回值Return value描述Description
负整数A negative integer此实例小于 valueThis instance is less than value.
Zero此实例等于 valueThis instance is equal to value.
正整数A positive integer此实例大于 valueThis instance is greater than value.

Implements

Remarks

此成员是显式接口成员的实现。This member is an explicit interface member implementation. 仅当 Guid 实例强制转换为 IComparable 接口时,才能使用此方法。It can only be used when the Guid instance is cast to an IComparable interface.

CompareTo 方法将 Guid 视为向 Guid(Int32, Int16, Int16, Byte[]) 构造函数提供的值进行比较,如下所示:The CompareTo method compares the GUIDs as if they were values provided to the Guid(Int32, Int16, Int16, Byte[]) constructor, as follows:

  • 它比较 UInt32 值,如果结果不相等,则返回结果。It compares the UInt32 values, and returns a result if they are unequal. 如果它们相等,则执行下一次比较。If they are equal, it performs the next comparison.
  • 它比较第一个 UInt16 值,如果结果不相等,则返回结果。It compares the first UInt16 values, and returns a result if they are unequal. 如果它们相等,则执行下一次比较。If they are equal, it performs the next comparison.
  • 它比较第二个 UInt16 值,如果结果不相等,则返回结果。It compares the second UInt16 values, and returns a result if they are unequal. 如果它们相等,则执行下一次比较。If they are equal, it performs the next comparison.
  • 如果对接下来的八个 Byte 值执行逐字节的比较。If performs a byte-by-byte comparison of the next eight Byte values. 如果遇到第一个不相等对,则返回结果。When it encounters the first unequal pair, it returns the result. 否则,它将返回0,表示两个 Guid 值相等。Otherwise, it returns 0 to indicate that the two Guid values are equal.

请注意,最后八个字节以相反顺序出现在 Guid 的字符串表示形式中,从低字节到高位字节。Note that the final eight bytes appear in the string representation of a Guid in reverse order, from low byte to high byte. 例如,在 Guid 值为 "01e75c83-c6f5-4192-b57e-7427cec5560d" 的字符串表示形式中,最后八个字节为 "b57e-7427cec5560d"。For example, in the string representation of the Guid value "01e75c83-c6f5-4192-b57e-7427cec5560d", the final eight bytes are "b57e-7427cec5560d." 换而言之,最后八个字节按从0xb5 开始的从左到右的逐字节进行比较。In other words, the final eight bytes are compared on a byte-by-byte basis from left to right starting with 0xb5.

如果两个 Guid 对于一个组件具有相等的值,则该方法会比较下一个组件。If two GUIDs have equal values for a component, the method compares the next component. 当找到值不相等的组件时,它将返回结果。When it finds a component whose values are unequal, it returns the result.

此方法实现 System.IComparable<T> 接口,并比 Guid.CompareTo 方法稍微好些,因为它不需要将 value 参数转换为 Guid 值。This method implements the System.IComparable<T> interface and performs slightly better than the Guid.CompareTo method because it does not have to convert the value parameter to a Guid value.

Applies to