ICollection as argument: Provide List or Array [duplicate] - c#

This question already has answers here:
Array versus List<T>: When to use which?
(16 answers)
Closed 3 years ago.
If some method requires an ICollection<T> as an argument, but I have only an IEnumerable<T> available: Is it better to convert this IEnumerable<T> to a IList<T> or is it better to convert it to an array T[] or shall I convert it to something else or is there no difference at all?
The IEnumerable<T> is only required for this method call, so no further read/write/extend is required.

If you're not going to be accessing the elements, you should use ToList() as explained why here.
If you'll be iterating through the elements, then it'd be better performance-wise to do it via an index (meaning casting them to an array), so that you can use a for loop, instead of a foreach loop.
for is faster than your typical foreach due to it using an index to access each element.

Related

Why use IEnumerable<T> when we can Iterate through collection using foreach() [duplicate]

This question already has answers here:
Can anyone explain IEnumerable and IEnumerator to me? [closed]
(16 answers)
Closed 7 days ago.
I want to know the exact place where we should use IEnumberable<T>
I know how IEnumerable<T> work and returns IEnumerator<T> and all that but the ultimate goal of IEnumerable<T> is to query the data from the collection isn't it? That is what we can already do using foreach() loop ? So when to use IEnumerable<T>? what is the actual practical scenario where the IEnumerable<T> is the only option to query the collection?
but the ultimate goal of IEnumerable<T> is to query the data from the collection isn't it?
No; the goal of IEnumerable<T> is to provide access to a sequence, which may or may not be a collection. The point being to abstract away what the underlying source is. It could be a raw collection, but it could be:
some LINQ (or similar) projection (collection.Where(...).Select(...) etc)
an open query to ADO.NET, redis, a socket, gRPC, a file or some other data provider that isn't readily countable, repeatable, etc - just: "a sequence"
an in-process data generator
some producer/consumer setup
etc
If you know you're always iterating a collection, then sure: feel free to use the concrete type, or ICollection<T>/IList<T> etc; but: not every sequence is a collection.

Fast way to search through an array of elements [duplicate]

This question already has answers here:
FindAll vs Where extension-method
(5 answers)
Slow LINQ query for .ToArray()
(3 answers)
C# FindAll VS Where Speed
(5 answers)
Closed 3 years ago.
I have an array of structs and I need to locate all the elements that share a certain condition.
Currently I use:
Array.FindAll(someArray, x => x.eg == "*Perfomance Test*")
But after some googling I noticed that there is a much faster way by using the LINQ Where-method.
someArray.Where(x => x.eg == "*Perfomance Test*")
I did some testing and the results are pretty impressive:
FindAll: 00:00:03.06
Where: 00:00:00.20
The problem is that Where returns IEnumerable. And I call this method that returns all these certain elements from the array within the for loop. Which is the type of loop I need to use. I used the .ToArray() method, but that made it much worse so that FindAll() is the faster approach.
Because of that it seems to me that if I need to get an array of elements, Where is much slower option than FindAll. But maybe I'm missing something.
Are there any better, faster options?

Why to use interface IList to create an object of List type? [duplicate]

This question already has answers here:
List<T> or IList<T> [closed]
(18 answers)
Closed 8 years ago.
Why here interface Ilist is used to create an object of List type
IList<BankAccountView> bankAccountViews = new List<BankAccountView>();
when it can be done like this
List<BankAccountView> bankAccountViews = new List<BankAccountView>();
A List is a concrete type, while an IList is a contract for which you can use any implemtation that an IList has.
An IList has a set of methods as defined on MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx
Here's a very good blogpost explaining it in detail: http://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/
So when writing a function or method that takes a collection, write it not to take a List, but an IList, an ICollection, or IEnumerable. The generic interfaces will still work even for heterogenous lists because System.Object can be a T too. Doing this will save you headache if you decide to use a Stack or some other data structure further down the road. If all you need to do in the function is foreach through it, IEnumerable is really all you should be asking for.
On the other hand, when returning an object out of a function, you want to give the user the richest possible set of operations without them having to cast around. So in that case, if it's a List internally, return a copy as a List.
Read link: When to use IList and when to use List

How to get the first element of IEnumerable (non-generic)? [duplicate]

This question already has answers here:
How to get the first element of IEnumerable
(5 answers)
Closed 9 years ago.
If I have a generic IEnumerable<int>. I can simply apply ToList() or ToArray() or FirstOrDefault() to it. How to apply these methods to a non-generic IEnumerable?
You have couple of options:
If you know that all objects in your enumerable are of the same type, you can cast it to the generic IEnumerable<YourType>. In worst case you can always use object:
object first = enumerable.Cast<object>().First();
Or you can use enumerator, make one step and take current element:
IEnumerator enumerator = enumerable.GetEnumerator();
enumerator.MoveNext();
object first = enumerator.Current;
You have two options here:
Follow the question that #Danier Gimenez suggested and make use of the Cast<TResult> method. After the cast you get a generic enumerable on which you can apply the First() method. This is also the most simple implementation.
Use the GetEnumerator() method which gives you an IEnumerator. And from here you can iterate over the collection. Starting with MoveNext(), you can use the Current property to get the first element.
Edit:
Andrei was ahead of me.
it is simple
look this sample code
IEnumerable collection;
--- fill collection here---
collection.OfType().ToList() or collection.OfType().ToArray
collection.OfType().ToList() or collection.OfType().ToArray()
it's filter the (int/MyClass) types object and convert it to a list or array

What should I use an IEnumerable or IList? [duplicate]

This question already has answers here:
IList vs IEnumerable for Collections on Entities
(2 answers)
Closed 9 years ago.
Can anyone tell me when I should use either.
For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct?
Thank you.
Generally speaking, you should try and use the least specific type that suits your purpose. IEnumerable is less specific than IList (IList implements IEnumerable) so unless you want something specific from IList (such as Count as you suggest, or perhaps Add, Delete, etc), I'd use IEnumerable.
One benefit of remaining with IEnumerable is that you can write iterator methods to return this type (look up "yield return" and iterator methods if you are not familiar with them). This allows you to write very memory efficient "pipelines" for your loops.
You use IEnumerable when you want to loop through the items in a collection.
IList is when you want to add, remove, and access the list contents out of order.
IList vs IEnumerable for Collections on Entities
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=722
You should use IList when you need access by index to your collection, add and delete elements, etc., and IEnumerable when you need just enumerate over your collection.
A very simple answer, I can extend it if you will describe you scenario.

Categories