C# - No implicit reference conversion from 'T' to 'System.IComparable'
I have taken the following class from another SO question:
public class Range<T> where T : IComparable<T>
{
public T Minimum { get; set; }
public T Maximum { get; set; }
public override string ToString() { return String.Format("[{0} -
{1}]", Minimum, Maximum); }
public Boolean IsValid() { return Minimum.CompareTo(Maximum) <= 0; }
public Boolean ContainsValue(T value)
{
return (Minimum.CompareTo(value) <= 0) &&
(value.CompareTo(Maximum) <= 0);
}
}
I would like, however, to create another class that contains many
instances of this class, and can execute a foreach loop on them all,
returning true if the number passed is contained in any one of the ranges:
public class Ranges<T> where T : Range<T>
{
private List<Range<T>> rangelist;
public void add(Range<T> range)
{
rangelist.Add(range);
}
public Boolean ContainsValue(T value)
{
foreach (Range<T> range in rangelist)
{
if (range.ContainsValue(value)) return true;
}
return false;
}
}
However, i am getting the error The type 'T' cannot be used as type
parameter 'T' in the generic type or method 'Range<T>'. There is no
implicit reference conversion from 'T' to 'System.IComparable<T>'.
What exactly is going wrong here?
No comments:
Post a Comment