The type parameter cannot be used with type arguments.

I would like to program a helper method in a Unit test project that initializes the presenter, sets up an instance of the views for it, and sets the state of the presenter.

This excluded me:

type parameter cannot be used with type arguments

the code:

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

After a couple of minutes, I found that the problem was with my return type TPresenter<Tview>

I read a few posts that clearly didn't explain why I can't say T1<T2>

I was forced to make the assignment of the leader through the reference parameter. Any explanation is appreciated!

+6
source share
4 answers

Basically, there is no way to say that a type parameter is itself a typical type with a certain number of type parameters, which you need to do to make TPresenter<TView> understandable.

It's unclear what you mean by making it work with the reference parameter - no matter what type you used for this parameter ref should be exact as the return type. I assume that it was only a TPresenter type, not a TPresenter<TView> .

+9
source

There is no such thing as TPresenter<TView> , it makes no sense. TPresenter is just a placeholder until it is limited to where it can be anything, for example. there is no int<tview> , so you cannot do this. When you add a constraint, it means it must be BasePresenter<TView> or some derived type, so it will always be Something<TView> , so again TPresenter<TView> does not make sense.

+1
source

It is old, but I hit it too. In a class definition, just use one type and then several types in which you use it. For instance:

 public class Template1<T>{} void SomeFunc() { <Template1<SomeClass1,SomeClass2>> someValue = new <Template1<SomeClass1,SomeClass2>>() } //or even: void SomeOtherFunc<U,V>() { <Template1<U,V>> someValue = new <Template1<U,V>>(); } 
0
source

I was getting a similar error in my code. @Jon Skeet correctly points in the right direction. The return type is already generic, as indicated by TPresenter : BasePresenter<TView> . Therefore, we can simply use it as TPresenter instead of TPresenter<TView> .

 public class BasePresenter<T> { } public class Demo { public static TPresenter Initialize<TPresenter, TView>() where TPresenter: BasePresenter<TView>, new() { return null; } } 
0
source

Source: https://habr.com/ru/post/896363/


All Articles