What is the correct syntax for this:
IList<string> names = 'Tom,Scott,Bob'.Split(',').ToList<string>().Reverse();
What am I messing up? What does TSource mean?
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.
The problem is that you’re calling
List<T>.Reverse()which returnsvoid.You could either do:
or:
The latter is more expensive, as reversing an arbitrary
IEnumerable<T>involves buffering all of the data and then yielding it all – whereasList<T>can do all the reversing ‘in-place’. (The difference here is that it’s calling theEnumerable.Reverse<T>()extension method, instead of theList<T>.Reverse()instance method.)More efficient yet, you could use:
This avoids creating any buffers of an inappropriate size – at the cost of taking four statements where one will do… As ever, weigh up readability against performance in the real use case.