.net - .net 中是否有表示 “range” 的标准类?

标签 .net design-patterns range

我们有很多代码具有“最小值”和“最大值”值,例如价格、利润、成本等。目前这些作为两个参数传递给方法,并且通常具有不同的属性/方法来检索它们。

在过去的几十年里,我已经看到 101 个自定义类在不同的代码库中存储值的范围,在我创建另一个这样的类之前,我想确认现在的 .NET 框架没有构建这样一个类 某处。

(如果需要,我知道如何创建我自己的类,但是我们在这个世界上已经有太多的轮子了,我无法随心所欲地发明另一个轮子)

最佳答案

AFAIK .NET 中没有这样的东西。不过,想出一个通用的实现会很有趣。

构建通用的 BCL 质量范围类型需要大量工作,但它可能看起来像这样:

public enum RangeBoundaryType
{
    Inclusive = 0,
    Exclusive
}

public struct Range<T> : IComparable<Range<T>>, IEquatable<Range<T>>
    where T : struct, IComparable<T>
{
    public Range(T min, T max) : 
        this(min, RangeBoundaryType.Inclusive, 
            max, RangeBoundaryType.Inclusive)
    {
    }

    public Range(T min, RangeBoundaryType minBoundary,
        T max, RangeBoundaryType maxBoundary)
    {
        this.Min = min;
        this.Max = max;
        this.MinBoundary = minBoundary;
        this.MaxBoundary = maxBoundary;
    }

    public T Min { get; private set; }
    public T Max { get; private set; }
    public RangeBoundaryType MinBoundary { get; private set; }
    public RangeBoundaryType MaxBoundary { get; private set; }

    public bool Contains(Range<T> other)
    {
        // TODO
    }

    public bool OverlapsWith(Range<T> other)
    {
        // TODO
    }

    public override string ToString()
    {
        return string.Format("Min: {0} {1}, Max: {2} {3}",
            this.Min, this.MinBoundary, this.Max, this.MaxBoundary);
    }

    public override int GetHashCode()
    {
        return this.Min.GetHashCode() << 256 ^ this.Max.GetHashCode();
    }

    public bool Equals(Range<T> other)
    {
        return
            this.Min.CompareTo(other.Min) == 0 &&
            this.Max.CompareTo(other.Max) == 0 &&
            this.MinBoundary == other.MinBoundary &&
            this.MaxBoundary == other.MaxBoundary;
    }

    public static bool operator ==(Range<T> left, Range<T> right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(Range<T> left, Range<T> right)
    {
        return !left.Equals(right);
    }

    public int CompareTo(Range<T> other)
    {
        if (this.Min.CompareTo(other.Min) != 0)
        {
            return this.Min.CompareTo(other.Min);
        }

        if (this.Max.CompareTo(other.Max) != 0)
        {
            this.Max.CompareTo(other.Max);
        }

        if (this.MinBoundary != other.MinBoundary)
        {
            return this.MinBoundary.CompareTo(other.Min);
        }

        if (this.MaxBoundary != other.MaxBoundary)
        {
            return this.MaxBoundary.CompareTo(other.MaxBoundary);
        }

        return 0;
    }
}

关于.net - .net 中是否有表示 “range” 的标准类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10172800/

相关文章:

java - 构造函数 vs 方法 vs 工厂

java - 支持多个 xsd 版本

python - 将 n 个 DataFrame 按元素聚合为单个 DataFrame

ruby - 嵌套 Ruby 列表生成

excel - 如何在 Excel 中的一组值中找到*最*平均值/最接近平均值的值?

c# - 以下通过关闭取消订阅事件的模式会导致任何问题吗?

.net - 如何在 C# 中使用枚举来存储字符串常量?

c++ - 为什么叫iota?

c# - 以编程方式读取 w3wp .Net 性能计数器实例

.net - 使用 VS 解决方案的 git 子模块时如何解析 nuget 包