Tuple<T1>.Equals(Object) 方法
定义
public:
override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
public override bool Equals (object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean
参数
- obj
- Object
与该实例进行比较的对象。The object to compare with this instance.
返回
如果当前实例等于指定对象,则为 true;否则为 false。true if the current instance is equal to the specified object; otherwise, false.
示例
下面的示例调用 Tuple<T1>.Equals(Object) 方法来比较对象, Tuple<T1> 其组件为 Double 具有三个对象( Tuple<T1> 其组件具有以下特征)的值:The following example calls the Tuple<T1>.Equals(Object) method to compare a Tuple<T1> object whose component is a Double value with three Tuple<T1> objects whose components have the following characteristics:
) (相同 Double 的类型,但值不同。Same type (Double), but different value.
不同类型 (Single) ,但值相同。Different type (Single), but same value.
using System;
public class Example
{
public static void Main()
{
var doubleTuple1 = Tuple.Create(12.3455);
var doubleTuple2 = Tuple.Create(16.8912);
var doubleTuple3 = Tuple.Create(12.3455);
var singleTuple1 = Tuple.Create(12.3455f);
var tuple2 = Tuple.Create("James", 97.3);
// Compare first tuple with a Tuple(Of Double) with a different value.
TestEquality(doubleTuple1, doubleTuple2);
// Compare first tuple with a Tuple(Of Double) with the same value.
TestEquality(doubleTuple1, doubleTuple3);
// Compare first tuple with a Tuple(Of Single) with the same value.
TestEquality(doubleTuple1, singleTuple1);
// Compare a 1-tuple with a 2-tuple.
TestEquality(doubleTuple1, tuple2);
}
private static void TestEquality(Tuple<double> tuple, object obj)
{
Console.WriteLine("{0} = {1}: {2}", tuple.ToString(),
obj.ToString(),
tuple.Equals(obj));
}
}
// The example displays the following output:
// (12.3455) = (16.8912): False
// (12.3455) = (12.3455): True
// (12.3455) = (12.3455): False
// (12.3455) = (James, 97.3): False
Module Example
Sub Main()
Dim doubleTuple1 = Tuple.Create(12.3455)
Dim doubleTuple2 = Tuple.Create(16.8912)
Dim doubleTuple3 = Tuple.Create(12.3455)
Dim singleTuple1 = Tuple.Create(CSng(12.3455))
Dim tuple2 = Tuple.Create("James", 97.3)
' Compare first tuple with a Tuple(Of Double) with a different value.
TestEquality(doubleTuple1, doubleTuple2)
' Compare first tuple with a Tuple(Of Double) with the same value.
TestEquality(doubleTuple1, doubleTuple3)
' Compare first tuple with a Tuple(Of Single) with the same value.
TestEquality(doubleTuple1, singleTuple1)
' Compare a 1-tuple with a 2-tuple.
TestEquality(doubleTuple1, tuple2)
End Sub
Private Sub TestEquality(tuple As Tuple(Of Double), obj As Object)
Try
Console.WriteLine("{0} = {1}: {2}", tuple.ToString(),
obj.ToString,
tuple.Equals(obj))
Catch e As ArgumentException
If obj.GetType.IsGenericType Then
If obj.GetType().Name = "Tuple`1" Then
Console.WriteLine("Cannot compare a Tuple(Of {0}) with a Tuple(Of {1}).",
tuple.Item1.GetType().Name, obj.Item1.GetType().Name)
Else
Console.WriteLine("Cannot compare a {0} with a {1}.", tuple.GetType().Name,
obj.GetType().Name)
End If
Else
Console.WriteLine("Cannot compare a {0} with a {1}.", tuple.GetType().Name,
obj.GetType().Name)
End If
End Try
End Sub
End Module
' The example displays the following output:
' (12.3455) = (16.8912): False
' (12.3455) = (12.3455): True
' (12.3455) = (12.3455): False
' (12.3455) = (James, 97.3): False
注解
obj在下列条件下,参数被视为等于当前实例:The obj parameter is considered to be equal to the current instance under the following conditions:
其单个组件的类型与当前实例相同。Its single component is of the same type as the current instance.
它的单个组件等于当前实例的组件。Its single component is equal to that of the current instance. 相等性取决于每个组件的默认对象相等比较器。Equality is determined by the default object equality comparer for each component.