int[] mylist = { 2, 4, 5 }; IEnumerable<int> list1 = mylist; list1.ToList().Add(1); // why 1 does not get addedto list1??
int[] mylist = { 2, 4, 5 }; IEnumerable<int> list1 = mylist; list1.ToList().Add(1); //
Share
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.
Why would it? ToList() generates a new List and the value ‘1’ gets added to it. Since you don’t store the return, the new list then gets tossed when it’s out of scope.
ToList() doesn’t change the original IEnumerable object list1 or give a new representation (it would be called AsList() if it did).