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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:07:03+00:00 2026-05-26T12:07:03+00:00

Working with a collection I have the two ways of getting the count of

  • 0

Working with a collection I have the two ways of getting the count of objects; Count (the property) and Count() (the method). Does anyone know what the key differences are?

I might be wrong, but I always use the Count property in any conditional statements because I’m assuming the Count() method performs some sort of query against the collection, where as Count must have already been assigned prior to me ‘getting.’ But that’s a guess – I don’t know if performance will be affected if I’m wrong.

EDIT: Out of curiosity then, will Count() throw an exception if the collection is null? Because I’m pretty sure the Count property simply returns 0.

  • 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-26T12:07:03+00:00Added an answer on May 26, 2026 at 12:07 pm

    Decompiling the source for the Count() extension method reveals that it tests whether the object is an ICollection (generic or otherwise) and if so simply returns the underlying Count property:

    So, if your code accesses Count instead of calling Count(), you can bypass the type checking – a theoretical performance benefit but I doubt it would be a noticeable one!

    // System.Linq.Enumerable
    public static int Count<TSource>(this IEnumerable<TSource> source)
    {
        checked
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            ICollection<TSource> collection = source as ICollection<TSource>;
            if (collection != null)
            {
                return collection.Count;
            }
            ICollection collection2 = source as ICollection;
            if (collection2 != null)
            {
                return collection2.Count;
            }
            int num = 0;
            using (IEnumerator<TSource> enumerator = source.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    num++;
                }
            }
            return num;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Rails and am working with the collection_select method. I have two
I have two tables, Proteins and Species . Protein has Species.Id as foreign key
I have a ObservableCollection<Object> that holds two different types of objects: Directory and File
How to remove items in Entity Collection? ex: I have two entities that is
In Mongo my understanding is that you can have databases and collections. I'm working
I'd like to get a tcp/ip connection working over the internet. I already have
When working with a collection of forms that the user must step through, is
I spent the last three days working on the collection _ select form helper
Im currently working with an API which requires we send our collection details in
I'm having a hard time working with arrays. Coming from AS2/AS3 and garbage collection

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.