En iyi Tarafı C# IEnumerable Nerelerde Kullanılıyor

Wiki Article

Botz3000Botz3000 39.4k88 gold badges106106 silver badges128128 bronze badges 1 I believe this is was derece asked here so it can be a comment derece an answer, but anyway makes sense from refactoring standpoint

So when you have to simply iterate through the in-memory collection, use IEnumerable, if you need to do any manipulation with the collection like Dataset and other data sources, use IQueryable

I understand why IQueryable is better choice for "out-of-memory" veri but I am struggling to understand why IEnumerable is better for "in-memory" veri? I still think it's better to get filtered data than to get get data and apply filter.

Consider that in your code example a new list created. I don't know what is m_states, but if this is a value types collection, you create a clone of the original list. In this way the returning list güç be manipulated form the caller Add/Remove/Update elements. without

(birli per Mike P's excellent answer) wrap an enumerator to pretend to be enumerable, there are some things that you kişi't really do - for example, it is hoped

B. IEnumerator can remember the current C# IEnumerable Nerelerde Kullanılıyor index when we pass from one method to another (it start working with current index) but IEnumerable yaşama't remember the index and it reset the index to beginning. More in this C# IEnumerable Temel Özellikleri video

// but this throws an exception, because the pointer or link to the // database namely the DbContext called MyEntities no longer exists.

It özgü a good performance when you are iterating through big objects or collections because it C# IEnumerable Nasıl Kullanılır does derece load the entire object to memory in order to make iteration.

Nobody mentioned one crucial difference, ironically answered on a question closed C# IEnumerable Nasıl Kullanılır as a duplicated of this. IEnumerable is read-only and List is hamiş.

Are there substantive differences between the different approaches to C# IEnumerable Nasıl Kullanılır "size issues" in category theory? more hot questions lang-cs

Then with a IQueryable the generated SQL will contains “where name = “a”, but with a IEnumerable many more roles will be pulled back from the database, then the x.name = “a” check will be done by .Kupkuru.

IEnumerable enable implicit reference conversion for array types which known as Covariance. Consider the following example:

If you create an IEnumerable, then all rows will be pulled into memory birli objects before running the query.

The most important thing to realize is that, using Linq, the query does derece get evaluated immediately. It is only run birli part of iterating through the resulting IEnumerable in a foreach - that's what all the weird delegates are doing.

Report this wiki page