C # generics: using generic class in where generic method clause

Can anyone explain why this is not possible (at least in .Net 2.0):

public class A<T>
{
    public void Method<U>() where U : T
    {
        ...
    }
}

...

A<K> obj = new A<K>();
obj.Method<J>();

where K is a superclass of J

EDIT

I tried to simplify the problem to make the question more understandable, but I clearly overdid it. Excuse me!

My problem is a little more specific, I think. This is my code (based on this ):

public class Container<T>
{
    private static class PerType<U> where U : T
    {
        public static U item;
    }

    public U Get<U>() where U : T
    {
        return PerType<U>.item;
    }

    public void Set<U>(U newItem) where U : T
    {
        PerType<U>.item = newItem;
    }
}

and I get this error:

Container.cs (13,24): error CS0305: using generic type Container<T>.PerType<U>' requires2 'type argument

+5
source share
2 answers

This is actually possible. This code compiles and works very well:

public class A<T>
{
    public void Act<U>() where U : T
    {
        Console.Write("a");
    }
}

static void Main(string[] args)
{
  A<IEnumerable> a = new A<IEnumerable>();
  a.Act<List<int>>();
}

/ generics, :

IEnumerable<Derived> d = new List<Derived>();
IEnumerable<Base> b = d;
+7

(VS 2008).

? ( , )

?


UPDATE

Container<T>,

class A { }
class B : A { }

class Test
{
    public void MethodName( )
    {
        var obj = new Container<A>();
        obj.Set(new B());
    }
}

. , B A? , , , List<B> List<A> (. YavgenyP).


, , Container<T> , PerType<U, X??? >.

+2

All Articles