1

I am coding a C# forms application, and would like to know if the following two functions achieve the same result:

public List<object> Method1(int parentId)
{
    List<object> allChildren = new List<object>();
    foreach (var item in list.Where(c => c.parentHtmlNodeForeignKey == parentId))
    {
        allChildren.Add(item);
        allChildren.AddRange(Method1(item.id));
    }
    return allChildren;
}
public IEnumerable<object> Method2(int parentId)
{
    foreach (var item in list.Where(c => c.parentHtmlNodeForeignKey == parentId))
    {
        yield return item;
        foreach (var itemy in Method2(item.id))
        {
            yield return itemy;
        }
    }
}

Am I correct in saying that the Method1 function is more efficient than the Method2?

Also, can either of the above functions be coded to be more efficient?

EDIT

I am using the function to return some objects that are then displayed in a ListView. I am then looping through these same objects to check if a string occurs.

Thanks.

Simon
  • 7,991
  • 21
  • 83
  • 163
  • Hard to read with all those long names. What's wrong with `Method1` , `Method2` ? – Royi Namir Jul 19 '15 at 08:32
  • 2
    How are they being used? Are you iterating over the resluts multiple times? Are you iterating through *all* of the results? – Jon Skeet Jul 19 '15 at 08:32
  • It depends on how you use the function. The Yield function is executed on demand, hence a single call to the function requires less runtime than the List function (but you won't get the result). The Yield function requires the overhead of constructing an iterator object. So if you call `ToList()` on the result of the Yield method, this is probably slower than the pure `List` function. – Nico Schertler Jul 19 '15 at 08:34
  • Exposing `List` vs `IEnumerable` isn't really ever a question of efficiency, more intent. If you intend someone to take your items and add or remove from them, `List` is the way to go. If all you intend is for someone to iterate them, `IEnumerable` is the contract for that. – Adam Houldsworth Jul 19 '15 at 08:35
  • possible duplicate of [When NOT to use yield (return)](http://stackoverflow.com/questions/3969963/when-not-to-use-yield-return) – Felix K. Jul 19 '15 at 08:36

2 Answers2

3

This highly depends on what you want to do. For example if you use FirstOrDefault(p => ....) the yield method can be faster because it's not required to store all the stuff into a list and if the first element is the right one the list method has some overhead ( Of course the yield method has also overhead but as i said it depends ).

If you want to iterate over and over again over the data then you should go with the list.

Felix K.
  • 6,201
  • 2
  • 38
  • 71
1

It depends on lot's of things.

Here are some reasons to use IEnumerable<T> over List<T>:

  1. When you are iterating a part of a collection (e.g. using FirstOrDefault, Any, Take etc.).
  2. When you have an large collection and you can ToList() it (e.g. Fibonacci Series).

When you shouldn't use IEnumerable<T> over List<T>:

  1. When you are enumerating a DB query multiple times with different conditions (You may want the results in memory).
  2. When you want to iterate the whole collection more than once - There is no need to create iterators each time.
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99