Why does this generic constraint compile when it seems to have a circular reference

后端 未结 5 546
逝去的感伤
逝去的感伤 2020-12-01 19:46

I have written an extension method in csharp for an MVCContrib Html helper and was surprised at the form of the generic constraint, which on the face of it seems to circular

5条回答
  •  借酒劲吻你
    2020-12-01 20:08

    The way you are using it makes no sense at all. But using a generic parameter in a constraint on that same parameter is quite normal, here's a more obvious example:

    class MySortedList where T : IComparable
    

    The constraint expresses the fact that there must be an ordering between objects of type T in order to put them into sorted order.

    EDIT: I'm going to deconstruct your second example, where the constraint is actually wrong but helps it compile.

    The code in question is:

    /*analogous method for comparison*/
    public static List AddNullItem(this List list, bool value) 
        where T : List
    {
        list.Add(null);
        return list;
    }
    

    The reason it won't compile without a constraint is that value types can't be null. List is a reference type, so by forcing where T : List you force T to be a reference type which can be null. But you also make AddNullItem nearly useless, since you can no longer call it on List, etc. The correct constraint is:

    /* corrected constraint so the compiler won't complain about null */
    public static List AddNullItem(this List list) 
        where T : class
    {
        list.Add(null);
        return list;
    }
    

    NB: I also removed the second parameter which was unused.

    But you can even remove that constraint if you use default(T), which is provided for exactly this purpose, it means null when T is a reference type and all-zero for any value type.

    /* most generic form */
    public static List AddNullItem(this List list) 
    {
        list.Add(default(T));
        return list;
    }
    

    I suspect that your first method also needs a constraint like T : class, but since I don't have all the classes you're using I can't say for certain.

提交回复
热议问题