The problem is that you are are redefining a new T type separate from the one declared on the class level. Try with one less generic:
class A<T>
{
private T[] stuff;
public A(int n) { stuff = new T[n]; }
public T get(int i) { return stuff[i]; }
}
class B<T>
{
private A<T> things;
public B() { things = new A<T>(10); }
public T get() { return things.get(1); }
}
"I mention this solution in my question and as I say, I have a need to use a generic method. So in essence class A has other uses outside of B and so I need to keep it separate. Since A's data, T[] stuff, is private B has to call its' public method. Generally speaking can a generic method in a generic class call a method in the inner class that returns type T?"
B.Get
is already a generic method that inherits its generic type from the class definition of
B
when it is instantiated. If you try to add a second generic to it, then the new
B.Get
-specific declaration of
T
masks the
B-class
-specific declaration of
T
and the compiler (rightly) assumes that it's a different type - because you can otherwise legitimately say
B<int> b = new B<int>();
string s = b.Get<string>();
Because you can't add any constraints on what type
A.Get
or
B.Get
can return, the compiler won't allow you to mask
T
and return the unmasked type - because it can't guarantee any conversion between them is possible.
Try it in VS with the code as I showed:
A<int> a = new A<int>(7);
int i = a.get(3);
B<int> b = new B<int>();
string s = b.get();
VS will complain you can't convert the
int
to a
string
, and Intellisense will display
b
as being of type
B<int>
automatically.