简体   繁体   English

为什么不Nullable <T> 实现IComparable?

[英]Why doesn't Nullable<T> implement IComparable?

The answer to this is probably glaringly obvious, but I'll stoop to asking anyway. 对此的答案可能非常明显,但无论如何我都会屈服于询问。

I was in the midst of writing a Range<T> class, and in the process realized I needed a comparer for it. 我正在写一个Range<T>类,在这个过程中意识到我需要一个比较器。 So, I wrote a generic comparer, derived, naturally, from Comparer<T> : 所以,我写了一个通用的比较器,自然地从Comparer<T>派生出来:

public class Range<T> where T : IComparable
{

    // Simplified here for brevity -- actually uses the comparer
    public T Min { get; set; }

    // Simplified here for brevity -- actually uses the comparer
    public T Max { get; set; }

    private class DefaultComparer : Comparer<T> 
    {
        public override int Compare(T x, T y)
        {
            return x == null && y == null 
                ? 0 
                : x == null 
                    ? -1 
                    : y == null 
                        ? 1 
                        : x.CompareTo(y);
        }
    }
}

Now, this all works fine and well, until you pass a Nullable<T> as the type. 现在,这一切都运行良好,直到您传递Nullable<T>作为类型。 For example: 例如:

public Range<DateTime?> DateSentFilter { get; set; }

This breaks horribly, because, of course, Nullable<T> doesn't implement IComparable . 这可怕地打破了,因为Nullable<T>当然没有实现IComparable And this got me to thinking: Why not? 这让我想到:为什么不呢?

Given how a comparer is generally written, why couldn't it? 鉴于比较器一般是如何编写的,为什么不能呢?

Does anyone with deep knowledge of this stuff have any idea? 知道这些东西的人有什么想法吗? Is there a scenario that would prohibit this from being done? 是否存在禁止这样做的情况?

Well, the generic type provided to Nullable<T> doesn't need to implement IComparable itself. 那么,提供给Nullable<T>的泛型类型不需要实现IComparable本身。

You have provided this condition yourself: T : IComparable , but that is not necessarily true for the T in Nullable<T> . 你自己提供了这个条件: T : IComparable ,但对于Nullable<T>T不一定如此。 I will agree that Nullable<T> is often used on primitive types that do implement IComparable but you could use it on structs, and they don't have IComparable implemented. 我同意Nullable<T>经常用于实现IComparable原始类型,但你可以在结构上使用它,并且它们没有实现IComparable

This is a comment from the source code : 这是来自源代码的评论:

// Warning, don't put System.Runtime.Serialization.On*Serializ*Attribute
// on this class without first fixing ObjectClone::InvokeVtsCallbacks
// Also, because we have special type system support that says a a boxed Nullable<T>
// can be used where a boxed<T> is use, Nullable<T> can not implement any intefaces
// at all (since T may not).   Do NOT add any interfaces to Nullable!
// 

As it says, Nullable<T> can't implement any interfaces for given reasons. 正如它所说, Nullable<T>由于给定的原因无法实现任何接口。

Now, the second question is: if it could, would they implement IComparable ? 现在,第二个问题是: 如果可以,他们会实施IComparable吗? No. Patrick has told why. 帕特里克已经说明了原因。 Not every T implements IComparable , it just wouldn't make sense. 不是每个T实现IComparable ,它只是没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM