Is ToArray faster than ToList?

In the majority of scenarios ToArray will allocate more memory than ToList . Both use arrays for storage, but ToList has a more flexible constraint. It needs the array to be at least as large as the number of elements in the collection. If the array is larger, that is not a problem.

When To call ToList?

Call ToList if: you create new objects (eg. in a select) you have side effects in your query.

What is the use of ToList in c#?

This extension method converts collections (IEnumerables) to List instances. It is fast and easy-to-remember. It returns a List instance with the appropriate elements.

Does ToList return a copy?

As ToList has no way to clone MyObject, it must do a shallow copy, so the created list contains the same references as the original one, so the code returns 5. ToList will create a brand new list.

Can ToList return null?

If you have a Query that returns a empty set then ToList returns null. You would probably expect it to return an empty list (Count = 0), which is the case when using data providers for SQL Server.

Does ToList create a deep copy?

As ToList has no way to clone MyObject, it must do a shallow copy, so the created list contains the same references as the original one, so the code returns 5.

Can IEnumerable be null?

The returned IEnumerable<> might be empty, but it will never be null .

Is null in LINQ query?

LINQ queries should never return null and you should not get an exception if the result is empty.

What is the difference between a shallow copy and a deep copy?

Shallow Copy reflects changes made to the new/copied object in the original object. Deep copy doesn’t reflect changes made to the new/copied object in the original object. Deep copy stores the copy of the original object and recursively copies the objects as well.

When should I use IEnumerable in C#?

IEnumerable is best to query data from in-memory collections like List, Array etc. IEnumerable doesn’t support add or remove items from the list. Using IEnumerable we can find out the no of elements in the collection after iterating the collection. IEnumerable supports deferred execution.