Skip to main content
added 149 characters in body
Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88

When you return a List, you are saying that "all processing has been done, and the List contains the results".

However, when you return an IEnumerable, you are saying that "processing might still need to be done".

In your example, when you return an IEnumerable, the .Where and .Select have NOT bee processed yet. This is known as "deferred execution".
If the user uses the result 3 times, then the .Where and .Select will execute 3 times. There are a good number of tricky issues that can come from this.

I recommend using a List as often as possible when returning values from a method. In addition to the additional functionality you'll get from a List, because.NET has many optimizations that require a List, debugging support is better, and there's a reduced chance of unintended side effects!

When you return a List, you are saying that "all processing has been done, and the List contains the results".

However, when you return an IEnumerable, you are saying that "processing might still need to be done".

In your example, when you return an IEnumerable, the .Where and .Select have NOT bee processed yet. This is known as "deferred execution".
If the user uses the result 3 times, then the .Where and .Select will execute 3 times. There are a good number of tricky issues that can come from this.

I recommend using a List as often as possible when returning values from a method, because there's a reduced chance of unintended side effects!

When you return a List, you are saying that "all processing has been done, and the List contains the results".

However, when you return an IEnumerable, you are saying that "processing might still need to be done".

In your example, when you return an IEnumerable, the .Where and .Select have NOT bee processed yet. This is known as "deferred execution".
If the user uses the result 3 times, then the .Where and .Select will execute 3 times. There are a good number of tricky issues that can come from this.

I recommend using a List as often as possible when returning values from a method. In addition to the additional functionality you'll get from a List, .NET has many optimizations that require a List, debugging support is better, and there's a reduced chance of unintended side effects!

Source Link
Scott Rippey
  • 15.8k
  • 5
  • 73
  • 88

When you return a List, you are saying that "all processing has been done, and the List contains the results".

However, when you return an IEnumerable, you are saying that "processing might still need to be done".

In your example, when you return an IEnumerable, the .Where and .Select have NOT bee processed yet. This is known as "deferred execution".
If the user uses the result 3 times, then the .Where and .Select will execute 3 times. There are a good number of tricky issues that can come from this.

I recommend using a List as often as possible when returning values from a method, because there's a reduced chance of unintended side effects!