Given a list, which method is preferred to determine the number of elements inside?
var myList = new List<string>();
myList.Count
myList.Count()
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Count()is an extension method introduced by LINQ while theCountproperty is part of the List itself (derived fromICollection). Internally though, LINQ checks if yourIEnumerableimplementsICollectionand if it does it uses theCountproperty. So at the end of the day, there’s no difference which one you use for aList.To prove my point further, here’s the code from Reflector for
Enumerable.Count()