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

Posted by Hun1Ahpu on Stack Overflow See other posts from Stack Overflow or by Hun1Ahpu
Published on 2010-04-11T18:55:45Z Indexed on 2010/04/12 6:53 UTC
Read the original article Hit count: 357

Filed under:
|
|

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

public class SimpleLinkedList<T> where T:IComparable

And I've started creating methods like this:

public static class LinkedListExtensions
{
    public static T[] ToArray<T>(this SimpleLinkedList<T> simpleLinkedList) where T:IComparable
    {
       //// code
    }
}

But when I tried to make LinkedListExtensions class generic like this:

public static class LinkedListExtensions<T> where T:IComparable
{
    public static T[] ToArray(this SimpleLinkedList<T> simpleLinkedList)
    {
         ////code
    }
}

I get "Extension methods can only be declared in non-generic, non-nested static class".

And I'm trying to guess where this restriction came from and have no ideas.

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics