Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:12:01+00:00 2026-05-27T20:12:01+00:00

Thanks for help with explaining to me about IList and IEnumerable. I have marked

  • 0

Thanks for help with explaining to me about IList and IEnumerable. I have marked that question as answered. Now I have a request for just a bit more information.

I have code like this:

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

It was suggested in a previous post that it’s not efficient as the Count() is a method and it gets executed many times. I tried the following:

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

This seems to work.

Can someone please confirm there’s a difference. Both work so I am not 100 % sure.

Would the following be the most efficient? Does the foreach need an IList or and IEnumerable?

foreach(var item in Items) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:12:02+00:00Added an answer on May 27, 2026 at 8:12 pm

    The Count() method call actually calls the static Enumerable.Count() method. This is optimized for IList implementations to use the property – but this requires a dynamic type check, and still ends up going through to the property – so using the property is certainly more efficient. In .NET 3.5 it only optimizes for ICollection<T>, whereas in .NET 4 it optimizes for the non-generic ICollection as well. See my Edulinq blog post on Count for more details.

    As for the last bit of your question: a foreach loop may or may not be more efficient, but it would be more readable in my view, which is rather more important. I would definitely use foreach unless you really need the index of each entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for your help! I'd like to output all companyName entries that have uploads
I have a show/hide toggle working well in multiple instances (thanks to help here
I'm almost there with something thanks to the help of stackoverflow users. I just
Update Thanks to Marc's help the AlphaPagedList class is now available on CodePlex if
I need some help in explaining this behavior in SQL Server 2008. I have
i know nothing about ruby, can anyone help explaining what this script is doing
C# XNA Xbox, in this case optional parameters are not optional Thanks to help
Thanks to pbos help and its program (published here , for xoring a big
Thanks to the help here and on the RestKit mailing list I've been able
Thanks for any help in advance, I can't wrap my SQL skills around this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.