Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type parameter cannot be used with type arguments

Tags:

c#

.net

generics

I wanted to code a helper method in Unit test project, which will initialize the presenter set the views instance to it and set the presenter state.

It threw me the exception:

the type parameter cannot be used with type arguments

Code:

public static **TPresenter<TView>** Initialize<TPresenter,TView>()
    where TPresenter: BasePresenter<TView>, new()
    where TView : new()
{
}

After couple of minutes I found the issue was with my return type TPresenter<Tview>

I read few posts which didn't clearly explain Why I'm not be able to say T1<T2>

I was forced to make the presenter assignment through reference parameter. Any explanations are welcome!

like image 357
ioWint Avatar asked Aug 31 '11 23:08

ioWint


Video Answer


2 Answers

Basically there's no way of saying that a type parameter is itself a generic type with a particular number of type parameters - which you need to be able to do in order to make TPresenter<TView> make sense.

It's not clear what you mean by making it work via a reference parameter - whatever type you used for that ref parameter should be fine as a return type too. My guess is that it was just of type TPresenter, not TPresenter<TView>.

like image 196
Jon Skeet Avatar answered Sep 17 '22 21:09

Jon Skeet


There is no such thing as a TPresenter<TView> it is meaningless. TPresenter is just a placeholder, until it is constrained by the where it could be anything, e.g. there is no int<tview> so you can't have that. Once you add the constraint it means it has to be a BasePresenter<TView> or some derived type so it will always be a Something<TView> so again TPresenter<TView> is meaningless.

like image 30
Ben Robinson Avatar answered Sep 19 '22 21:09

Ben Robinson