Count method or property for IList

Thanks for the help in explaining to me about IList and IEnumerable. I noted that the question answered. Now I have a request for some additional information.

I have a code like this:

for (var index = 0; index < Model.Items.Count(); index++) 

In a previous post, it was suggested that it is inefficient because the Count () method is a method and is executed multiple times. I tried the following:

for (var index = 0; index < Model.Items.Count; index++) 

It seems to work.

Can someone please confirm the difference. Both work, so I'm not 100% sure.

Will it be the most effective? Is foreach IList or IEnumerable required?

foreach(var item in Items) 
+5
source share
2 answers

Count() Enumerable.Count(). IList , - , , . .NET 3.5 ICollection<T>, .NET 4 -generic ICollection. . Edulinq Count.

: foreach , , , . foreach, .

+4

Count() , IEnumerable, IEnumerable , IEnumerable, IEnumerator, , .

, Count(), , ICollection ( , , FW4.0, IList IList<T> ), ICollection ( Count), Count() . . , , ICollection IList, Count Count()?

, , Count() :

var count = Model.Items.Count();
for(var index = 0; index < count; index++) { }
+6

All Articles