0

This is one of the sample Implementation and not the original code.. This question may be old one but would like to understand the basics.. Here the return type is specified as IEnumerable but actually we are returning a List here..

So if we are returning the List, then we can make the method return type also as List.. rit?

  public IEnumerable<product> AddProducts(IEnumerable<Product> pList)
       {
           List<product>  pNewProducts = new List <product>();
           Product prod = new Product();
           foreach (Product p in pList)
           {
               prod = Get(p.Id); // get item being updated
               if (p.Equals(Add(p)))
                   pNewProducts.Add(p);
           }
           return pNewProducts; // return collection of added products
       }

This is one of the sample method in WebAPI

TechQuery
  • 253
  • 3
  • 17
  • Possible duplicate of [IEnumerable vs List - What to Use? How do they work?](http://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work) – CodeCaster Nov 03 '15 at 17:59
  • _"if we are returning the List, then we can make the method return type also as List"_ - yes, but read the [duplicate](http://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work) for why that may not be a good idea. – CodeCaster Nov 03 '15 at 18:00
  • Thanks.. Actually i have gone through the link already.. but that post describes more in terms of "What are the advantages and flexibility of returning as IEnumerable".. Here here actually i m converting it to a list and i m returning only list, but still i have a webAPI method return type is shown as IEnumerable.. Ideally speaking, i feel like i negated the flexibility of IEnumerable by converting it to list in my method itself.. Isnt it? – TechQuery Nov 03 '15 at 18:03
  • 1
    In general this issue is raised when talking about creating statically-typed APIs, WebAPI is a different beast altogether. And no, you're not limiting anything, as a `List` _is_ an `IEnumerable`. It's not going to matter for WebAPI anyway. – CodeCaster Nov 03 '15 at 18:05
  • Hmm.. Yes.. Actually i got confused using it interchangeably in WebAPI. Is there any link or article where i can get more info on this.. Thanks a lot @CodeCaster – TechQuery Nov 03 '15 at 18:10
  • @CodeCaster Also one more query.. Lets take normal scenario other than WebAPI. Assume, we are returning IEnumerable. Later in another method, i m enumerating it and connect to the DB Entity. My Question is, in this case, will the EF DB object continues to be alive till it is used enumerated in that method???? – TechQuery Nov 03 '15 at 19:28

0 Answers0