gpt4 book ai didi

c# - 有没有一种方法可以在不复制大量代码的情况下消除 IComparable 运算符重载?

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:16 24 4
gpt4 key购买 nike

我有一堆实现 IComparable<T> 的类型.因为这些类型实现了该接口(interface),所以提供以下重载是有意义的:

/// <summary>Equality comparison operator.</summary>
/// <param name="lhs">The left hand side.</param>
/// <param name="rhs">The right hand side.</param>
/// <returns>True if <paramref name="lhs"/> is equal to <paramref name="rhs"/>; otherwise, false.</returns>
public static bool operator==(T lhs, T rhs)
{
if (object.ReferenceEquals(lhs, null))
{
return object.ReferenceEquals(rhs, null);
}

return lhs.CompareTo(rhs) == 0;
}

/// <summary>Inequality comparison operator.</summary>
/// <param name="lhs">The left hand side.</param>
/// <param name="rhs">The right hand side.</param>
/// <returns>True if <paramref name="lhs"/> is not equal to <paramref name="rhs"/>; otherwise, false.</returns>
public static bool operator !=(T lhs, T rhs)
{
return !(lhs == rhs);
}

/// <summary>Less than comparison operator.</summary>
/// <param name="lhs">The left hand side.</param>
/// <param name="rhs">The right hand side.</param>
/// <returns>True if <paramref name="lhs"/> is less than <paramref name="rhs"/>; otherwise, false.</returns>
public static bool operator <(T lhs, T rhs)
{
if (lhs == null)
{
if (rhs == null)
{
return false;
}
else
{
return true;
}
}
else
{
return lhs.CompareTo(rhs) < 0;
}
}

/// <summary>Greater than comparison operator.</summary>
/// <param name="lhs">The left hand side.</param>
/// <param name="rhs">The right hand side.</param>
/// <returns>True if <paramref name="lhs"/> is greater than <paramref name="rhs"/>; otherwise, false.</returns>
public static bool operator >(T lhs, T rhs)
{
return rhs < lhs;
}

/// <summary>Less than or equal comparison operator.</summary>
/// <param name="lhs">The left hand side.</param>
/// <param name="rhs">The right hand side.</param>
/// <returns>True if <paramref name="lhs"/> is less` than or equal to <paramref name="rhs"/>; otherwise, false.</returns>
public static bool operator <=(T lhs, T rhs)
{
return !(rhs < lhs);
}

/// <summary>Greater than or equal comparison operator.</summary>
/// <param name="lhs">The left hand side.</param>
/// <param name="rhs">The right hand side.</param>
/// <returns>True if <paramref name="lhs"/> is greater than or equal to <paramref name="rhs"/>; otherwise, false.</returns>
public static bool operator >=(T lhs, T rhs)
{
return !(lhs < rhs);
}

有很多代码需要一遍又一遍地重复。是否有一些创建抽象类或其他东西的方法(例如 C++ 的 std::rel_ops )可以更容易地实现这些?

最佳答案

有一个方法,但我不确定我会推荐它。在示例中,为了简洁起见,我只定义了两个运算符。在其中一个中,我转换了 TemplateComparable<T> 的实例。至 T因为在类约束中,T必须确实继承自 TemplateComparable<T>并且类型转换将有效。

abstract class TemplateComparable<T> where T : TemplateComparable<T>, IComparable<T>
{
public static bool operator ==(TemplateComparable<T> lhs, T rhs)
{
if (ReferenceEquals(lhs, null))
{
return ReferenceEquals(rhs, null);
}
return rhs.CompareTo(lhs as T) == 0;
}

public static bool operator !=(TemplateComparable<T> lhs, T rhs)
{
return !(lhs == rhs);
}
}

class Foo : TemplateComparable<Foo>, IComparable<Foo>
{
public int CompareTo(Foo other)
{
//Write your comparison code
return 0;
}
}

示例用法:

var v1 = new Foo();
var v2 = new Foo();
var equals = v1 == v2; //equals will be true

另一种选择 TemplateComparable<T>工具 IComparable<T>Foo覆盖其成员:

abstract class TemplateComparable<T> : IComparable<T> where T : TemplateComparable<T>
{
public abstract int CompareTo(T other);

public static bool operator ==(TemplateComparable<T> lhs, T rhs)
{
if (ReferenceEquals(lhs, null))
{
return ReferenceEquals(rhs, null);
}
return rhs.CompareTo(lhs as T) == 0;
}

public static bool operator !=(TemplateComparable<T> lhs, T rhs)
{
return !(lhs == rhs);
}
}

class Foo : TemplateComparable<Foo>
{
public override int CompareTo(Foo other)
{
//Write your comparation code
return 0;
}
}

关于c# - 有没有一种方法可以在不复制大量代码的情况下消除 IComparable<T> 运算符重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14206618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com