none
Performant Refactoring RRS feed

  • Question

  • Yo msdn-homies, I got a question!

    Is it necessay to call ToList on an IEnumerable if I just want to do a foreach with that, for performance reasons?

    Here is an example:

    var input = new List<string> {null, "123", "1234", null}
    var enumerable = input.Where(x => x != null);
    
    // Is this better
    foreach(var str in enumerable)
    {
        Console.WriteLine(str);
    }
    
    // Or this
    var list = enumerable.ToList();
    foreach(var str in list)
    {
        Console.WriteLine(str);
    }

    Thanks for any explains,

    KingPinguin


    Friday, March 15, 2019 8:51 AM

Answers

  • Hi RefactoringKingPinPinguin,

    >>But is there a performance reason or is foreach on list and enumerable the same calculation time

    Yes, please check the link below about the performance.

    https://stackoverflow.com/questions/1211491/c-sharp-listt-vs-ienumerablet-performance-question

    >>So if I understand you correctly, you mean that there is the same performance by itererating enumerable and lists and this is why there is no need to create a list for it, correct?

    When we use much data, the performance would be obvious. They are not the same.

    But if you just want to foreach, IEnumerable could be used to foreach directly.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 9:52 AM
    Moderator

All replies

  • Hi RefactoringKingPinPinguin,

    Thank you for posting here.

    For your question, there is no need to call ToList if you just want to do a foreach with that. Only we use so much data that the performance would been affected. But IEnumerable and IEnumerable.ToList do difference things. You could check the link below.

    https://codereview.stackexchange.com/questions/66756/method-returning-ienumerablet-should-tolist-or-not

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 9:35 AM
    Moderator
  • Hi Wendy,

    thank you for your answer. But is there a performance reason or is foreach on list and enumerable the same calculation time? I just heard that it is better to not use enumerable if I want to use random access because it is like a linked list, is this true?

    So if I understand you correctly, you mean that there is the same performance by itererating enumerable and lists and this is why there is no need to create a list for it, correct?

    Thanks a lot,

    Kinguin
    Friday, March 15, 2019 9:38 AM
  • a ToList() instances a List<T>.

    List<string> strlist;  // it is defined but it is not instanced as a object.

    strlist = enumerable.ToList(); // the ToList() instanced the List<string> for strlist

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist?view=netframework-4.7.2

    Friday, March 15, 2019 9:43 AM
  • Hi RefactoringKingPinPinguin,

    >>But is there a performance reason or is foreach on list and enumerable the same calculation time

    Yes, please check the link below about the performance.

    https://stackoverflow.com/questions/1211491/c-sharp-listt-vs-ienumerablet-performance-question

    >>So if I understand you correctly, you mean that there is the same performance by itererating enumerable and lists and this is why there is no need to create a list for it, correct?

    When we use much data, the performance would be obvious. They are not the same.

    But if you just want to foreach, IEnumerable could be used to foreach directly.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 9:52 AM
    Moderator
  • Hi Wendy, hi DA924x,

    thank you so much for the fast response. :)

    Friday, March 15, 2019 9:55 AM