是否有重新presenting整数范围内的C#类型? [英] Is there a C# type for representing an integer Range?

查看:155
本文介绍了是否有重新presenting整数范围内的C#类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必要存储的整数范围。是否有在现有类型C#4.0?

I have a need to store an integer range. Is there an existing type for that in C# 4.0?

当然,我可以写我自己的 int类从 int值属性和建立正确的逻辑确保来自以下=要。但是,如果一个类型已经存在,我当然宁愿使用。

Of course, I could write my own class with int From and int To properties and build in proper logic to ensure that From <= To. But if a type already exists, I'd of course rather use that.

推荐答案

我发现最好推出自己的。有些人使用元组 s或 S,但你希望你的范围<结束/ code>来是广泛的,提供,涉及到一个范围一些方便的方法。这也是最好的,如果通用的(如果你需要一系列 S,或一定范围内的一些自定义类的?)例如:

I found it best to roll my own. Some people use Tuples or Points, but in the end you want your Range to be extensive and provide some handy methods that relate to a Range. It's also best if generic (what if you need a range of Doubles, or a range of some custom class?) For example:

/// <summary>The Range class.</summary>
/// <typeparam name="T">Generic parameter.</typeparam>
public class Range<T> where T : IComparable<T>
{
    /// <summary>Minimum value of the range.</summary>
    public T Minimum { get; set; }

    /// <summary>Maximum value of the range.</summary>
    public T Maximum { get; set; }

    /// <summary>Presents the Range in readable format.</summary>
    /// <returns>String representation of the Range</returns>
    public override string ToString()
    {
        return string.Format("[{0} - {1}]", this.Minimum, this.Maximum);
    }

    /// <summary>Determines if the range is valid.</summary>
    /// <returns>True if range is valid, else false</returns>
    public bool IsValid()
    {
        return this.Minimum.CompareTo(this.Maximum) <= 0;
    }

    /// <summary>Determines if the provided value is inside the range.</summary>
    /// <param name="value">The value to test</param>
    /// <returns>True if the value is inside Range, else false</returns>
    public bool ContainsValue(T value)
    {
        return (this.Minimum.CompareTo(value) <= 0) && (value.CompareTo(this.Maximum) <= 0);
    }

    /// <summary>Determines if this Range is inside the bounds of another range.</summary>
    /// <param name="Range">The parent range to test on</param>
    /// <returns>True if range is inclusive, else false</returns>
    public bool IsInsideRange(Range<T> range)
    {
        return this.IsValid() && range.IsValid() && range.ContainsValue(this.Minimum) && range.ContainsValue(this.Maximum);
    }

    /// <summary>Determines if another range is inside the bounds of this range.</summary>
    /// <param name="Range">The child range to test</param>
    /// <returns>True if range is inside, else false</returns>
    public bool ContainsRange(Range<T> range)
    {
        return this.IsValid() && range.IsValid() && this.ContainsValue(range.Minimum) && this.ContainsValue(range.Maximum);
    }
}

这篇关于是否有重新presenting整数范围内的C#类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆