Why is it impossible to declare extension methods in a generic static class?

后端 未结 6 1499
忘了有多久
忘了有多久 2020-12-03 10:43

I\'d like to create a lot of extension methods for some generic class, e.g. for

public class SimpleLinkedList where T:IComparable

6条回答
  •  天命终不由人
    2020-12-03 11:15

    Static classes should be able to be defined as abstract. If they were able to do this, then you could specify that they could not be used directly, but must be inherited just like a regular class and then generic static classes would be doable for extension methods because you could define them in the abstract class, inherit from said class, and everything would work properly.

    As it is right now, there are a ton of significant limitations with static classes, this just being one that really messes with the mojo in a lot of cases. Combined with the inability to have the compiler infer the return type and thus requiring you to enter the whole generic implementation to call generic functions that don't have all of the generics in the method signature makes this stuff aggravatingly weak and makes for a lot of typing that shouldn't need to be there.

    (There is also the case where the second generic is defined in the definition of the first and it won't use it, even though it's obvious to infer as well at compile time. This one is super annoying because it is completely obvious.) MS has a TON of work on Generics that they could be doing to improve this stuff.

提交回复
热议问题