Efficiency of List<T> foreach vs ImmutableList<T> foreach

Madura Dias 20 Reputation points
2024-02-03T11:20:42.7533333+00:00

According to this document time complexity is the same (O(n)) of List<T>.Enumerator and ImmutableList<T>.Enumerator.

But in the description, it says

"Enumerating an ImmutableList<T> using a foreach loop is efficient because ImmutableList<T> uses a binary tree to store its data instead of an array like List<T> uses" .

This isn't very clear, how foreach on ImmutableList<T> is more efficient than the same on List<T> if the time complexity is equal ?

Developer technologies | .NET | Other
{count} votes

Accepted answer
  1. David Lowndes 2,640 Reputation points MVP
    2024-02-03T11:46:18.1566667+00:00

    I'd agree, that sentence is a tad confusing. I think it's trying to saying that foreach is more efficient than a for loop for ImmutableList because accessing it with an index is not efficient - unlike List which uses an array and so has fast index access. I suggest you use the feedback link on that topic to pass your comments on.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.