Many people ask me why, and I don`t have a good answer for them.
Obviously there is a good reason. Does anyone know it?
I searched here and found this question. It explains how it works, but not why.
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.
Suppose you wanted the equivalent of an
IEnumerable<int>but were using C# 1.0. You could implementIEnumerable– but that would require boxing and unboxing on each iteration. Using the sort of duck-typing version offoreachyou could get away without any boxing. In many cases the boxing wouldn’t actually be that harmful (I tend to find that the performance hit is exaggerated) but it’s still inelegant.I strongly, strongly suspect that if generics had been around in C# 1.0,
foreachwould have been restricted toIEnumerable<T>.