Reputation: 2356
I have noticed that a lot of times I need to do something like this
foreach (SomeObject item in listOfItems)
{
string itemName = item.Name;
...
}
But I would much prefer if there was a way to do something like this:
foreach (string item.Name in listOfItems)
{
...
}
Is this possible in C#?
Upvotes: 1
Views: 100
Reputation: 460058
You can use LINQ:
foreach (string name in listOfItems.Select(item => item.Name))
{
// ...
}
Upvotes: 15