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 7806001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:23:32+00:00 2026-06-02T02:23:32+00:00

I often write code like this: if ( list.Count > 0 ) { }

  • 0

I often write code like this:

if ( list.Count > 0 ) { }

Is this efficient? Does this operation look like:

  • Iterate through the list and count its elements
  • Result: 986,000 elements
  • Is 986,000 greater than 0?
  • return true

Or like this:

  • Retrieve the stored number of elements in the list (986,000)
  • Is 986,000 greater than 0?
  • return true

That is, to get the number of elements in the list, do you have to count all the way through the list, or is the number of elements recorded somewhere? And is this the case for all ICollection classes?

What about the Capacity of the list?

  • 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-06-02T02:23:33+00:00Added an answer on June 2, 2026 at 2:23 am

    I often write code like this: if ( list.Count > 0 ) { } Is this efficient?

    Yes. That retrieves the count in the list, which is stored in a field inside the list, and compares it to zero.

    Now a question you did not ask:

    What about if ( sequence.Count() > 0 ) { } ? (Notice the parentheses on Count().)

    We interrogate the sequence at runtime to see if it is a list that has a Count property that can be computed efficiently. If it does, we call it. If not, we count the entire sequence one item at a time, and then compare that to zero.

    Isn’t that incredibly inefficient?

    Yes.

    What would be more efficient?

    if (sequence.Any())

    Why is that more efficient?

    Because it attempts to iterate over one element. If it succeeds, then Any is true; if it fails then Any is false. You don’t need to count the number of jellybeans in the jar in order to know if there are more than zero. You only need to look to see if there is at least one.

    In addition to being considerably more efficient, the code now reads like the intended meaning of the code. If you are intending to ask “are there any items in the list?” then ask “are there any items in the list?” and not “is the number of items in the list greater than zero?”

    What about the Capacity property of a list?

    That tells you how much space has been pre-allocated in the list’s internal data structures. It is the amount of items the list can store before it has to allocate more memory.

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

Sidebar

Related Questions

I often find I want to write code something like this in C#, but
Without automatic reference counting you often write code like this, when adding a new
In OpenGL, one often writes code like this: glPushMatrix(); // modify the current matrix
I often want to write something like this: new Form { Text = Caption,
When I use an imperative language I often write code like foo (x) {
I've often seen examples like this in other projects' code: class Modela(models.Model): abbr =
I often write stuff that looks like this: def AModel < ActiveRecord::Base belongs_to :user
I use Aptana to write Python code. Aptana often report error due to the
When I code, I always write little pieces of unit, and compile it often.
I often write classes with a DLL export/import specification, but this seems to confuse

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.