Vijay
Vijay

Reputation: 815

Method is returning List when the method return type IEnumerable.But GetType shows as List

Though, the below question seems to be silly, but still it would help others also. I have method that return a list.And the method return type IEnumnerable. Method:

public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public static IEnumerable<Person> GetPersons()
        {
            List<Person> personList = new List<Person>()
                                          {
                                              new Person() { Age = 10, Name = "Vijay"}
                                          };

           return personList;

        }
    }

Now whenever I am calling this method and checking the type.The output is

System.Collections.Generic.List

Since it is a List. I should be able add Items to the list. But system is throwing a compilation error. Below is the execution part:

public class Program
    {
        public static void Main(string[] ar)
        {
            var personList = Person.GetPersons();
            Console.WriteLine(personList.GetType()); //System.Collections.Generic.List
            personList.Add(new Person(){Age = 11, Name = "Ajay"}); // Compilation error
            Console.ReadKey();

        }
    }

So could anyone help me with the understanding. It would be great help. Thanks in advance.

Upvotes: 0

Views: 901

Answers (3)

Prasad Telkikar
Prasad Telkikar

Reputation: 16049

IEnumerable does not supports adding new element to it.

IEnumerable interface from MSDN :

Exposes an enumerator, which supports a simple iteration over a non-generic collection.

Instead of using IEnumerable<Person> as a return type of GetPerson() function use List<Person>.

Like

    public static List<Person> GetPersons()
    {
        List<Person> personList = new List<Person>()
                                      {
                                          new Person() { Age = 10, Name = "Vijay"}
                                      };

       return personList;
    }

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81493

IEnumerable Interface

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

That's it, that's all it does. Now, there are many extension methods that work with IEnumerable, though it just so happens Add is not one of them

List<T>, implements IEnumerable which makes sense, and it has the ability to Add.

So from here you have 2 options

  1. Return a List<T>

    • public static List<Person> GetPersons()
  2. Enumerate your IEnumerable to a List<T>

    • var personList = Person.GetPersons().ToList();

Which one should you use? Well the fact that you are trying to add something and you are just not enumerating it, or using it in a Linq statement, says you probably should go with option 1


But whenever the caller calls the method and gets the type of the method. It stil shows as System.Collections.Generic.List . Could you explain why GetType is showing as List

Because that was the type created, when you return an IEnumerable, you are just casting the List<T> as a more fundamental type (in this case an interface). When you call GetType, it is telling you what type it is, because it IS that type. The compiler still knows what type your created! in-case you want to cast it back.

I think you need to hit the documentation a little more

Upvotes: 1

JonasMH
JonasMH

Reputation: 1183

The IEnumerable interface is in charge of iterating over the collection, not the storage and the adding/removing that goes on. So to enable adding to the list you can change your return type to List<Person> like so:

public static List<Person> GetPersons()
[...]

And everything should work.

While your return method does indeed return a List, by saying the return type is an IEnumerable you are saying to the user of your function should only iterate, not alter. Which is also why you get the compilation error.

Upvotes: 0

Related Questions