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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:04:15+00:00 2026-06-11T23:04:15+00:00

Possible Duplicate: Which method performs better: .Any() vs .Count() > 0? I just wonder

  • 0

Possible Duplicate:
Which method performs better: .Any() vs .Count() > 0?

I just wonder why should I use Any() instead of Count()?, if we took the msdn example :

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}
class Person
{
    public string LastName { get; set; }
    public Pet[] Pets { get; set; }
}

public static void AnyEx2()
{
    List<Person> people = new List<Person>
        { new Person { LastName = "Haas",
                       Pets = new Pet[] { new Pet { Name="Barley", Age=10 },
                                          new Pet { Name="Boots", Age=14 },
                                          new Pet { Name="Whiskers", Age=6 }}},
          new Person { LastName = "Fakhouri",
                       Pets = new Pet[] { new Pet { Name = "Snowball", Age = 1}}},
          new Person { LastName = "Antebi",
                       Pets = new Pet[] { }},
          new Person { LastName = "Philips",
                       Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
                                          new Pet { Name = "Rover", Age = 13}} }
        };

    // Determine which people have a non-empty Pet array.
    IEnumerable<string> names = from person in people
                            where person.Pets.AsQueryable().Any()
                            select person.LastName;

    foreach (string name in names)
        Console.WriteLine(name);

    /* This code produces the following output:

       Haas
       Fakhouri
       Philips
    */
}

What if I used :

  IEnumerable<string> names = from person in people
                            where person.Pets.Count() > 0
                            select person.LastName;

It will give same result ! , (I don’t think it created for shortness or something) , is there any feature for Any() ??

  • 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-11T23:04:16+00:00Added an answer on June 11, 2026 at 11:04 pm

    Any just checks if the sequence contains at least one element, while Count needs to iterate over all elements. That’s the difference. A classic scenario where Any is preferred over Count is this:

    if (sec.Count() > 0)
    

    vs

    if (sec.Any())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ STL: Which method of iteration over a STL container is better?
Possible Duplicate: ArgumentNullException or NullReferenceException from extension method? Which exception should be thrown here?
Possible Duplicate: C#: Passing null to overloaded method - which method is called? Here
Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR Which method is
Possible Duplicate: A better way to compare Strings which could be null I have
Possible Duplicate: When should I use static methods in a class and what are
Possible Duplicate: C#: Passing null to overloaded method - which method is called? Consider
Possible Duplicate: How to differentiate between http and cli requests? Which method to detect
Possible Duplicate: Find out which class called a method in another class. Hello everyone,
Possible Duplicate: indexOf method in an object array? I have a javascript array which

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.