Guid.CompareTo Guid.CompareTo Guid.CompareTo Guid.CompareTo Method

定义

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

重载

CompareTo(Guid) CompareTo(Guid) CompareTo(Guid) CompareTo(Guid)

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

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

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

CompareTo(Guid) CompareTo(Guid) CompareTo(Guid) CompareTo(Guid)

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

public:
 virtual int CompareTo(Guid value);
public int CompareTo (Guid value);
abstract member CompareTo : Guid -> int
override this.CompareTo : Guid -> int
Public Function CompareTo (value As Guid) As Integer

参数

value
Guid Guid Guid Guid

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

返回

一个带符号数字,指示此实例和 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.

实现

示例

下面的示例调用CompareTo(Guid)方法来比较 guid 值和两个相似的 guid 值。The following example calls the CompareTo(Guid) method to compare a GUID value with two similar GUID values.

using System;

public class Example
{
   public static void Main()
   {
      Guid mainGuid = Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d");
      unchecked {
         Guid guid2 = new Guid(0x01e75c83, (short) 0xc6f5, 
                               0x4192,
                               new Byte[] { 0xb5, 0x7e, 0x74, 0x27, 0xce, 0xc5, 0x56, 0x0c} );
         Guid guid3 = Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d");
         
         Console.WriteLine("{0} {1:F} {2}", mainGuid, 
                           (Comparison) mainGuid.CompareTo(guid2), guid2);
         Console.WriteLine("{0} {1:F} {2}", mainGuid, 
                           (Comparison) mainGuid.CompareTo(guid3), guid3);
      }
   }

   private enum Comparison
   { LessThan = -1, Equals = 0, GreaterThan = 1 }
}
// The example displays the following output:
//    01e75c83-c6f5-4192-b57e-7427cec5560d GreaterThan 01e75c83-c6f5-4192-b57e-7427cec5560c
//    01e75c83-c6f5-4192-b57e-7427cec5560d LessThan 01e75c84-c6f5-4192-b57e-7427cec5560d
Module Example
   Public Sub Main()
      Dim mainGuid As Guid = Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d")
      Dim guid2 As New Guid(&h01e75c83, 
                            BitConverter.ToInt16(new Byte() { &hf5, &hc6 }, 0), 
                            &h4192, 
                            new Byte() { &hb5, &h7e, &h74, &h27, &hce, &hc5, &h56, &h0c} )
      Dim guid3 As Guid = Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d")
      
      Console.WriteLine("{0} {1:F} {2}", mainGuid, 
                        CType(mainGuid.CompareTo(guid2), Comparison), guid2)
      Console.WriteLine("{0} {1:F} {2}", mainGuid, 
                        CType(mainGuid.CompareTo(guid3), Comparison), guid3)
   End Sub
   
   Private Enum Comparison As Integer
      LessThan = -1
      Equals = 0
      GreaterThan = 1
   End Enum
End Module
' The example displays the following output:
'    01e75c83-c6f5-4192-b57e-7427cec5560d GreaterThan 01e75c83-c6f5-4192-b57e-7427cec5560c
'    01e75c83-c6f5-4192-b57e-7427cec5560d LessThan 01e75c84-c6f5-4192-b57e-7427cec5560d

注解

方法将 guid 与提供Guid(Int32, Int16, Int16, Byte[])给构造函数的值进行比较,如下所示: CompareToThe 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.

  • 如果对接下来的 8 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方式略优于方法,因为它不必Guidvalue参数转换为值。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.

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

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

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

参数

value
Object Object Object Object

要比较的对象,或为 nullAn object to compare, or null.

返回

一个带符号数字,指示此实例和 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 此实例大于 value,或 valuenullThis instance is greater than value, or value is null.

实现

异常

示例

下面的示例使用GuidAttribute属性将 GUID 分配给某个类。The following example uses the GuidAttribute attribute to assign a GUID to a class. 它通过调用Attribute.GetCustomAttribute方法并将GuidAttribute返回Parse的对象的Value属性传递给方法来检索此 GUID 的值。It retrieves the value of this GUID by calling the Attribute.GetCustomAttribute method and passing the Value property of the returned GuidAttribute object to the Parse method. 然后,它会将该 GUID 与值数组进行比较。Then it compares that GUID with an array of values.

using System;
using System.Runtime.InteropServices;

[Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
public class Example
{
   public static void Main()
   {
      GuidAttribute guidAttr = (GuidAttribute) Attribute.GetCustomAttribute(typeof(Example), 
                                                      typeof(GuidAttribute));
      Guid guidValue = Guid.Parse(guidAttr.Value);
      Object[] values = { null , 16, 
                          Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d"),
                          guidValue };
      foreach (var value in values) {
         try {
            Console.WriteLine("{0} and {1}: {2}", guidValue, 
                              value == null ? "null" : value,
                              guidValue.CompareTo(value));
         }
         catch (ArgumentException) {
            Console.WriteLine("Cannot compare {0} and {1}", guidValue,
                              value == null ? "null" : value);
         }                     
      }                         
   }
}
// The example displays the following output:
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and null: 1
//    Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0
Imports System.Runtime.InteropServices

<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")>
Module Example
   Public Sub Main()
      Dim guidAttr As GuidAttribute = CType(Attribute.GetCustomAttribute(GetType(Example), 
                                                      GetType(GuidAttribute)), GuidAttribute)
      Dim guidValue As Guid = Guid.Parse(guidAttr.Value)
      Dim values() As Object = { Nothing, 16, 
                               Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d"),
                               guidValue }
      For Each value In values
         Try
            Console.WriteLine("{0} and {1}: {2}", guidValue, 
                              If(value Is Nothing, "null", value),
                              guidValue.CompareTo(value))
         Catch e As ArgumentException
            Console.WriteLine("Cannot compare {0} and {1}", guidValue,
                              If(value Is Nothing, "null", value))
         End Try                     
      Next                         
   End Sub
End Module
' The example displays the following output:
'    936da01f-9abd-4d9d-80c7-02af85c822a8 and null: 1
'    Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
'    936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
'    936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0

注解

参数必须为null或的实例Guid; 否则,将引发异常。 valueThe value parameter must be null or an instance of Guid; otherwise, an exception is thrown. 的任何实例Guid,无论其值如何,都视为null大于。Any instance of Guid, regardless of its value, is considered greater than null.

方法将 guid 与提供Guid给构造函数的值进行比较,如下所示: CompareToThe CompareTo method compares the GUIDs as if they were values provided to the Guid constructor, as follows:

  • 它比较Int32值,如果结果不相等,则返回结果。It compares the Int32 values, and returns a result if they are unequal. 如果它们相等,则执行下一次比较。If they are equal, it performs the next comparison.

  • 它比较第一个Int16值,如果结果不相等,则返回结果。It compares the first Int16 values, and returns a result if they are unequal. 如果它们相等,则执行下一次比较。If they are equal, it performs the next comparison.

  • 它比较第二Int16个值,如果结果不相等,则返回结果。It compares the second Int16 values, and returns a result if they are unequal. 如果它们相等,则执行下一次比较。If they are equal, it performs the next comparison.

  • 如果对接下来的 8 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 对于一个组件具有相等的值,则该方法会比较下一个组件。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.

请注意,最终的八个字节以相反顺序出现在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."

适用于