如何比较两种泛型类型? [重复]

时间:2021-03-18 16:14:42

This question already has an answer here:

这个问题在这里已有答案:

I make my class generic.The T - can be string or int.

我使我的类通用.T - 可以是字符串或int。

I have this feature class:

我有这个功能类:

public class Feature<T>// : IComparable
{
    public T CurrentFeatureCode { get; set; }

    public T StreetCode1 { get; set; }
    public T BuildingNumber1 { get; set; }
    public string BuildingLetter1 { get; set; }

    public T StreetCode2 { get; set; }
    public T BuildingNumber2 { get; set; }
    public string BuildingLetter2 { get; set; }

    public double CoordinateX { get; set; }
    public double CoordinateY { get; set; }

    public string Filter { get; set; }

    public string ToString(T streetCode)
    {
        return StreetCode2 == streetCode ? String.Format("{0}{1}", BuildingNumber2, BuildingLetter2) : String.Format("{0}{1}", BuildingNumber1, BuildingLetter1);
    }
}

As you can see I have ToString method inside Feature class that compares two values:

如您所见,我在Feature类中有ToString方法,它比较两个值:

StreetCode2 == streetCode ? String.Format("{0}{1}", BuildingNumber2, BuildingLetter2) : String.Format("{0}{1}", BuildingNumber1, BuildingLetter1);

I get error on this row:

我在这一行得到错误:

Error   11  Operator '==' cannot be applied to operands of type 'T' and 'T' .

My question is how can I compare two values of T type?

我的问题是如何比较T型的两个值?

3 个解决方案

#1


3  

For your type T, implement the IEquatable<T> interface. Override the Equals() method with logic that suits your case. Note that you must override GetHashCode() as well.

对于类型T,实现IEquatable 接口。使用适合您情况的逻辑覆盖Equals()方法。请注意,您还必须覆盖GetHashCode()。

#2


1  

I believe the recommended approach would be to use the IEquatable interface with generics and the Equals() method, but I am not sure if that suits your tastes.

我相信推荐的方法是使用带有泛型和Equals()方法的IEquatable接口,但我不确定这是否适合你的口味。

#3


0  

If you are happy with the default comparer, then you can do this:

如果您对默认比较器感到满意,那么您可以这样做:

public class Feature<T>// : IComparable
{
    public T StreetCode2 { get; set; }
    public string ToString(T streetCode)
    {
        if (EqualityComparer<T>.Default.Equals(StreetCode2, streetCode))
        {
            return "Equal";
        }

        return "Not Equal";
    }
}

And here is a test:

这是一个测试:

var feature1 = new Feature<string>();
feature1.StreetCode2 = "two";
string equals = feature1.ToString("two");

#1


3  

For your type T, implement the IEquatable<T> interface. Override the Equals() method with logic that suits your case. Note that you must override GetHashCode() as well.

对于类型T,实现IEquatable 接口。使用适合您情况的逻辑覆盖Equals()方法。请注意,您还必须覆盖GetHashCode()。

#2


1  

I believe the recommended approach would be to use the IEquatable interface with generics and the Equals() method, but I am not sure if that suits your tastes.

我相信推荐的方法是使用带有泛型和Equals()方法的IEquatable接口,但我不确定这是否适合你的口味。

#3


0  

If you are happy with the default comparer, then you can do this:

如果您对默认比较器感到满意,那么您可以这样做:

public class Feature<T>// : IComparable
{
    public T StreetCode2 { get; set; }
    public string ToString(T streetCode)
    {
        if (EqualityComparer<T>.Default.Equals(StreetCode2, streetCode))
        {
            return "Equal";
        }

        return "Not Equal";
    }
}

And here is a test:

这是一个测试:

var feature1 = new Feature<string>();
feature1.StreetCode2 = "two";
string equals = feature1.ToString("two");