I am developing Interface for a sample project i wanted it to be as generic as possible so i created a interface like 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 above difference is just types they return, so i created something like below only to find i get error Cannot Resolve Symbol T
What am i doing wrong
public interface IFactory
{
IEnumerable<T> GetAll();
T GetOne(int Id);
}
IEnumerable<T> contains a single method that you must implement when implementing this interface; GetEnumerator, which returns an IEnumerator<T> object. The returned IEnumerator<T> provides the ability to iterate through the collection by exposing a Current property.
IEnumerable interface Returns an enumerator that iterates through the collection.
To be less colloquial, you can return IEnumerable when you want to make it clear to consumers of your method that they cannot modify the original source of information. It's important to understand that if you're going to advertise this, you should probably exercise care in how the thing you're returning will behave.
We can get first item values from IEnumerable list by using First() property or loop through the list to get respective element. IEnumerable list is a base for all collections and its having ability to loop through the collection by using current property, MoveNext and Reset methods in c#, vb.net.
You need to use a generic interface/class, not just generic methods:
public interface IFactory<T>
{
IEnumerable<T> GetAll();
T GetOne(int Id);
}
Defining a generic type on the interface/class ensures the type is known throughout the class (wherever the type specifier is used).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With