C# Programming Performance Tips - Part Five - List.Count() Vs List.Count

You can read all the C# performance tips from the following links,

In this blog, we will do benchmarking for List.Count() which is a method and List.Count which is a property.

  1. List<string> strs = new List<string>() { "Akshay""Patel""Panth""Patel" };  
  2. Stopwatch watch = new Stopwatch();  
List.Count()
  1. watch.Start();  
  2. int count = strs.Count();  
  3. Console.WriteLine("Count()-{0}", watch.Elapsed);  
List.Count

  1. watch.Restart();  
  2. int count1 = strs.Count;  
  3. Console.WriteLine("Count - {0}", watch.Elapsed);  

Result

List.Count() vs List.Count

Thus, as we can see, whenever we want to get the count of a list, we should use the List.Count property.