In some case I've to return an empty list of items in a method.
Most of the case, I'm returning an IEnumerable<T>
, so the Enumerable.Empty<T>()
does exactly the job.
But I've one case where I've to return absolutely an IList<T>
instance(we could use an IEnumerable, but this would results in a code much less efficient).
I didn't found an equivalent for the IList
. I can perfectly imagine doing my own Singleton provider for empty list, but I would like to know if there is something I did miss?
The elements of each array in the collection are added to the resulting IEnumerable<T> only if that array contains four or more elements. Empty is used to generate the seed value for Aggregate because if no array in the collection has four or more elements, only the empty sequence is returned.
Now to check whether a list is empty or not, use the Count property. if (subjects. Count == 0) Console.
The returned IEnumerable<> might be empty, but it will never be null .
To handle empty collections, use the DefaultIfEmpty() method in C#. If an array is empty, then using this method will show the default method instead of displaying an error.
As Array is implementing IList use
Array.Empty<T>()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With