Discussion on:

5
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
This seems old
Mark Miller Updated - 11th Dec 2011
I'm not up on what version of .Net is more commonly in use now, so I don't mean this as a slam against you, but I remember reading about yield, and this iterator technique you're talking about, back when .Net 2.0 was released. In .Net 3 or 4 I believe you can accomplish what you're talking about using the List container:

List<string> list = new List<string> {"Tony", "John", "Timothy", "Anthony", "Julius"};
List<string> filteredList = list.FindAll(name => name.StartsWith("T"));
0 Votes
+ -
On second thought...
Mark Miller Updated - 11th Dec 2011
I think I got hung up on your example code, and missed your premise. I can see that for long lists what you're talking about would be valid. If you're iterating through the whole list, filtering, and carrying out an action, a simpler way of doing what you describe in the newer versions of .Net, using my code above, would be something like:

list.ForEach(name =>
if (name.StartsWith("T")) {Console.WriteLine(name);})

though there would be no way to break out of the loop, unless you wanted to add an extension method to List that allowed you more control, which I guess would be just as much work as what you describe. So six of one, half dozen of the other.
0 Votes
+ -
Caveat
nate.irvin@... 11th Dec 2011
"yield" is a useful piece of syntactic sugar, but be careful if you have a solution with mixed frameworks - if you have .NET 2.0 and .NET 4.0 projects in the same solution, and one of these projects uses yield, I've had the whole solution fail to compile, with the least helpful errors message ever. If you're using a single framework though, got to town!
0 Votes
+ -
I've never had a problem with that.
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.