Guid.CompareTo Metoda
Definice
Přetížení
CompareTo(Guid) |
Porovná tuto instanci se zadaným Guid objektem a vrátí označení jejich relativních hodnot.Compares this instance to a specified Guid object and returns an indication of their relative values. |
CompareTo(Object) |
Porovná tuto instanci se zadaným objektem a vrací údaj o jejich relativních hodnotách.Compares this instance to a specified object and returns an indication of their relative values. |
CompareTo(Guid)
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
Parametry
- value
- Guid
Objekt k porovnání s touto instancí.An object to compare to this instance.
Návraty
Číslo se znaménkem označující relativní hodnoty této instance a value
.A signed number indicating the relative values of this instance and value
.
Vrácená hodnotaReturn value | DescriptionDescription |
---|---|
Záporné celé čísloA negative integer | Tato instance je menší než value .This instance is less than value .
|
ŽádnouZero | Tato instance je rovna value .This instance is equal to value .
|
Kladné celé čísloA positive integer | Tato instance je větší než value .This instance is greater than value .
|
Implementuje
Příklady
Následující příklad volá CompareTo(Guid) metodu pro porovnání hodnoty GUID se dvěma podobnými hodnotami identifikátoru 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
Poznámky
CompareToMetoda porovnává identifikátory GUID, jako kdyby byly hodnotami poskytnutými Guid(Int32, Int16, Int16, Byte[]) konstruktoru, následovně:The CompareTo method compares the GUIDs as if they were values provided to the Guid(Int32, Int16, Int16, Byte[]) constructor, as follows:
Porovná UInt32 hodnoty a vrátí výsledek, pokud jsou nerovné.It compares the UInt32 values, and returns a result if they are unequal. Pokud jsou stejné, provede další porovnání.If they are equal, it performs the next comparison.
Porovná první UInt16 hodnoty a vrátí výsledek, pokud jsou neshodné.It compares the first UInt16 values, and returns a result if they are unequal. Pokud jsou stejné, provede další porovnání.If they are equal, it performs the next comparison.
Porovná druhé UInt16 hodnoty a vrátí výsledek, pokud jsou nerovné.It compares the second UInt16 values, and returns a result if they are unequal. Pokud jsou stejné, provede další porovnání.If they are equal, it performs the next comparison.
Pokud provádí porovnání po bajtech z dalších osmi Byte hodnot.If performs a byte-by-byte comparison of the next eight Byte values. Když dojde k prvnímu nestejnému páru, vrátí výsledek.When it encounters the first unequal pair, it returns the result. V opačném případě vrátí 0 pro indikaci, že Guid jsou dvě hodnoty stejné.Otherwise, it returns 0 to indicate that the two Guid values are equal.
Všimněte si, že poslední osm bajtů se zobrazí v řetězcové reprezentaci Guid v obráceném pořadí, od nízkého bajtu po horní bajt.Note that the final eight bytes appear in the string representation of a Guid in reverse order, from low byte to high byte. Například v řetězcové reprezentaci Guid hodnoty "01e75c83-c6f5-4192-b57e-7427cec5560d" jsou konečné osm bajtů "b57e-7427cec5560d".For example, in the string representation of the Guid value "01e75c83-c6f5-4192-b57e-7427cec5560d", the final eight bytes are "b57e-7427cec5560d." Jinými slovy, konečné osm bajtů je porovnáno na základě bajtů zleva doprava od 0xB5.In other words, the final eight bytes are compared on a byte-by-byte basis from left to right starting with 0xb5.
Pokud mají dva identifikátory GUID stejné hodnoty pro komponentu, metoda porovná další komponentu.If two GUIDs have equal values for a component, the method compares the next component. Když najde komponentu, jejíž hodnoty nejsou stejné, vrátí výsledek.When it finds a component whose values are unequal, it returns the result.
Tato metoda implementuje System.IComparable<T> rozhraní a provádí mírně lepší než metodu, Guid.CompareTo protože není nutné převést value
parametr na Guid hodnotu.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.
Platí pro
CompareTo(Object)
Porovná tuto instanci se zadaným objektem a vrací údaj o jejich relativních hodnotách.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
Parametry
- value
- Object
Objekt pro porovnání, nebo null
.An object to compare, or null
.
Návraty
Číslo se znaménkem označující relativní hodnoty této instance a value
.A signed number indicating the relative values of this instance and value
.
Vrácená hodnotaReturn value | DescriptionDescription |
---|---|
Záporné celé čísloA negative integer | Tato instance je menší než value .This instance is less than value .
|
ŽádnouZero | Tato instance je rovna value .This instance is equal to value .
|
Kladné celé čísloA positive integer | Tato instance je větší než value nebo value null .This instance is greater than value , or value is null .
|
Implementuje
Výjimky
Příklady
Následující příklad používá GuidAttribute atribut k přiřazení identifikátoru GUID ke třídě.The following example uses the GuidAttribute attribute to assign a GUID to a class. Načítá hodnotu tohoto identifikátoru GUID voláním Attribute.GetCustomAttribute metody a předáním Value Vlastnosti vráceného GuidAttribute objektu Parse metodě.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. Pak porovná identifikátor GUID s polem hodnot.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
Poznámky
value
Parametr musí být null
nebo instance Guid ; v opačném případě je vyvolána výjimka.The value
parameter must be null
or an instance of Guid; otherwise, an exception is thrown. Jakékoli instance Guid , bez ohledu na její hodnotu, se považují za větší než null
.Any instance of Guid, regardless of its value, is considered greater than null
.
CompareToMetoda porovnává identifikátory GUID, jako kdyby byly hodnotami poskytnutými Guid konstruktoru, následovně:The CompareTo method compares the GUIDs as if they were values provided to the Guid constructor, as follows:
Porovná Int32 hodnoty a vrátí výsledek, pokud jsou nerovné.It compares the Int32 values, and returns a result if they are unequal. Pokud jsou stejné, provede další porovnání.If they are equal, it performs the next comparison.
Porovná první Int16 hodnoty a vrátí výsledek, pokud jsou neshodné.It compares the first Int16 values, and returns a result if they are unequal. Pokud jsou stejné, provede další porovnání.If they are equal, it performs the next comparison.
Porovná druhé Int16 hodnoty a vrátí výsledek, pokud jsou nerovné.It compares the second Int16 values, and returns a result if they are unequal. Pokud jsou stejné, provede další porovnání.If they are equal, it performs the next comparison.
Pokud provádí porovnání po bajtech z dalších osmi Byte hodnot.If performs a byte-by-byte comparison of the next eight Byte values. Když dojde k prvnímu nestejnému páru, vrátí výsledek.When it encounters the first unequal pair, it returns the result. V opačném případě vrátí 0 pro indikaci, že Guid jsou dvě hodnoty stejné.Otherwise, it returns 0 to indicate that the two Guid values are equal.
Pokud mají dva identifikátory GUID stejné hodnoty pro komponentu, metoda porovná další komponentu.If two GUIDs have equal values for a component, the method compares the next component. Když najde komponentu, jejíž hodnoty nejsou stejné, vrátí výsledek.When it finds a component whose values are unequal, it returns the result.
Všimněte si, že poslední osm bajtů se zobrazí v řetězcové reprezentaci Guid v obráceném pořadí, od nízkého bajtu po horní bajt.Note that the final eight bytes appear in the string representation of a Guid in reverse order, from low byte to high byte. Například v řetězcové reprezentaci Guid hodnoty "01e75c83-c6f5-4192-b57e-7427cec5560d" jsou konečné osm bajtů "b57e-7427cec5560d".For example, in the string representation of the Guid value "01e75c83-c6f5-4192-b57e-7427cec5560d", the final eight bytes are "b57e-7427cec5560d."