0

I have the following signature which has a generic type and parameter as below:

List<T> GetMyList<T>(T filter);

The filter type is different from the list type. I am trying to call like this:

var myList = repo.GetMyList<List<items>>(filter);

I am not sure how to specify the type of 'filter' in the method call. Is this possible?

3
  • What do you mean? Whatever type you pass into your method will define it for the method, so you can just call repo.GetMyList(filter), and the T will be whatever type your filter parameter is. Commented Aug 18, 2017 at 13:32
  • You can't - the function takes in a filter of type T and returns a list of type T. There's no way around that. Maybe if you explained the problem in more detail a solution could be determined, but what you are asking is not possible with that signature. Commented Aug 18, 2017 at 13:32
  • Are you in control of List<T> GetMyList<T>(T filter);? Meaning can you change it's signature? Commented Aug 18, 2017 at 13:33

1 Answer 1

2

If the types are different, you need to have 2 generic types, like below:

public List<T1> GetMyList<T1, T2>(T2 filter);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That worked and to call it I used var myList = repo.GetMyList<items, string>(filter);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.