0
public interface IPersonRepository
{
    IEnumerable<Person> GetPeople();
}

Was in the process of learning C# when I came across this method skeleton. Forgive me if this is a silly question, but what is the return type of this method? In other words, what is IEnumerable<Person>?

List, interface, list of interfaces, or something else entirely?

3 Answers3

0

IEnumerable is the interface, which Lists etc implement Its similar to a IList, but its not the same. U know its an interface because of the "I" in the name

Master Azazel
  • 612
  • 1
  • 12
  • 32
0

IPersonRepository is an interface.
Its content is the function GetPeople() that returns an object of the type IEnumerable<Person>.
IEnumerable<> is a generic interface, here used in a concrete implementation with the class Person.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
0

IEnumerable is a Generic Collection, which has a type Person. So your method GetPeople() returns a list of Person objects. IPersonRepository is an interface, so any implementing class will have a method GetPeople().

Example using a List, which implement IEnumerable:

class PersonRepository : IPersonRepository{
    public IEnumerable<Person> GetPeople() {
        var people = new List<Person>();
        people = // Wherever you get them from.
        return people;
    }
Eric Yeoman
  • 1,036
  • 1
  • 14
  • 31
  • Thank you, that was very helpful. However, I am still confused about one thing. You said "your method GetPeople() returns a list of Person objects." However, can you conclude that an IEnumerable is always a List of People objects? Could it also be another data structure, perhaps a Dictionary, Stack, Queue (I'm not too familiar with data structures yet)? I wasn't sure if you meant list in the programming sense. – reincarnationofstackexchange May 28 '17 at 19:20
  • And yes, it could be a Stack, Queue, or a Linked List. A Dictionary wouldn't match as it implements IEnumerable>. This is one of the reasons to return IEnumerable from repositories, it allows you to do things with it elsewhere and doesn't tie you down to an implementation. I just used a List as an example. – Eric Yeoman May 28 '17 at 21:01
  • Strictly speaking, GetPeople() returns anything which implements the IEnumerable interface for type Person. I see you're learning, so I suggest you have a look at how to implement IEnumerable in your own classes, then have a look at how this works with a Generic IEnumerable. There's quite a few tutorials online, and it's usually covered in books on C#. – Eric Yeoman May 28 '17 at 21:05