Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running into a compiler error "cannot implicitly convert type T to T..." when I have code similar to this:

C#
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<T>() { return things.get(1); } 
}

error is on things.get(1) above.

The error goes away if B's get is not generic:
C#
public T get(){ return things.get(1)};

However, I do have a need to use a generic method here.
any suggestions/help is greatly appreciated.

Thanks.
Posted
Updated 26-Sep-15 5:35am
v3
Comments
Wendelius 26-Sep-15 11:31am    
What else would the return type of get be than T defined in class B?
Andreas Gieriet 27-Sep-15 6:13am    
Why do you need a generic function? This need seems odd to me.
B.Get() is already generic through B being generic. I guess you are mixing up something here.
Andi

1 solution

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:
C#
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
C#
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:
C#
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.
 
Share this answer
 
v2
Comments
binsafir 27-Sep-15 4:39am    
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?
OriginalGriff 27-Sep-15 5:03am    
Answer updated
Andreas Gieriet 27-Sep-15 6:13am    
My 5!
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900