Can I make a generic optional, defaulting to a certain class?

前端 未结 2 1421
-上瘾入骨i
-上瘾入骨i 2021-01-03 18:05

My question is related to Is there a reasonable approach to "default" type parameters in C# Generics?, but using an inner generic class that approach doesn\'t work

2条回答
  •  执笔经年
    2021-01-03 18:42

    I don't think there's much you can do about it, to be honest. You could make Foo doubly generic:

    public class Foo where TArgs : FooEventArgs
    {
        public delegate void EventHandler(object sender, TArgs e);
        public event EventHandler Changed;
    }
    

    Then you could write:

    public class Foo : Foo
    

    ... but it's really making things very complicated for very little benefit.

    I would also say that even though it's a bit more verbose to include the type argument, it does make it very clear - whereas inheritance can muddy the waters in various ways. I'd steer clear of class inheritance when you're not really trying to model behaviour specialization.

    The reason your implicit conversion doesn't work has nothing to do with generics, by the way - as the error message states, you can't declare a conversion (implicit or explicit) which goes up or down the inheritance hierarchy. From the C# spec section 6.4.1:

    C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine an already existing implicit or explicit conversion.

    (See that section for more details.)


    As a side note, I find it more common to use inheritance the other way round for generics, typically with interfaces:

    public interface IFoo
    {
        // Members which don't depend on the type parameter
    }
    
    public interface IFoo : IFoo
    {
        // Members which all use T
    }
    

    That way code can receive just an IFoo without worrying about the generics side of things if they don't need to know T.

    Unfortunately, that doesn't help you in your specific case.

提交回复
热议问题