I want to do something with each object in a C# Dictionary. keyVal.Value seems a little awkward:
foreach (KeyValuePair<int, Customer> keyVal in customers) {
DoSomething(keyVal.Value);
}
Is there a nicer way to do this that’s also fast?
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
Dictionaryclass has aValuesproperty that you can directly iterate over:An alternative, if you can use LINQ as Arie van Someren shows in his answer:
Or: