How can I return an IEnumerable <T> from a method

I am developing an interface for a sample project, I wanted it to be as general as possible, so I created an interface as shown below

public interface IUserFactory { IEnumerable<Users> GetAll(); Users GetOne(int Id); } 

but then it happened, I had to duplicate the interface to do below

 public interface IProjectFactory { IEnumerable<Projects> GetAll(User user); Project GetOne(int Id); } 

If you looked at the difference above, it's just the types they return, so I created something like below just to find me get error Cannot Resolve Symbol T What am I doing wrong

 public interface IFactory { IEnumerable<T> GetAll(); T GetOne(int Id); } 
+6
source share
3 answers

You need to use a common interface / class , not just common methods :

 public interface IFactory<T> { IEnumerable<T> GetAll(); T GetOne(int Id); } 

Defining a generic interface / class type ensures that the type is known throughout the class (wherever the type specifier is used).

+11
source

Declare an interface type:

 public interface IFactory<T> 
+10
source

The compiler cannot determine what T. is intended for. You also need to declare it at the class level.

Try:

  public interface IFactory<T> { IEnumerable<T> GetAll(); T GetOne(int Id); } 
+2
source

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


All Articles