In the following code, is the Select() method smart enough to keep the size of the list somewhere internally for the ToArray() method to be cheap?
List<Thing> bigList = someBigList;
var bigArray = bigList.Select(t => t.SomeField).ToArray();
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.
That’s easy to check, without looking at the implementation. Just create a class that implements
IList<T>, and put a trace in theCountproperty:If the
Countproperty is accessed, this code should print “Count accessed”:But it doesn’t print anything, so no, it doesn’t keep track of the count. Of course there could be an optimization specific to
List<T>, but it seems unlikely…