R
raylopez99
No need. FTM already posted an example that generates an IEnumerator
that enumerates every combination of first and last names based on two
string arrays (one the holds just first names, and the other holds
just last names). FTM just happen to use the yield keyword method.
It could have been done by manually declaring and implementing the
IEnumerator but that would be more work.
It also raises the point of the OP--what's the point of
'foreach' (other than eye candy and one somewhat important point)? So
"yield" keyword avoids the awkward IEnumerator inheritance, which I
kind of like, analogous to how delegates are now easier to register
(using 'new') in C#2.0 syntax and/or you don't need to invoke
delegates using .Invoke method--big deal. The "one somewhat important
point" is the one you made: using 'foreach' you can (I think my lingo
is right) do "delayed execution" and/or "lazy execution / evaluation"
and/or you don't need to set up a temporary collection class but can
serially iterate through a collection and get results now, rather than
iterate, get results, store, then output. Fine, that's great and I
agree 'foreach' is great for that. But it's not a big deal IMO* and a
lot of work to get the syntax right.
As for the Dictionary I was focused more on the public interface and
not necessary on how Microsoft made that happen. I just wanted to
present an example design pattern for case #2. Fortunately, Microsoft
has made the source code for v3.5 of the framework available. I'm not
sure about the details for downloading it as I have yet to do that,
but it is available nevertheless.
Well that's pretty worthless "it's out there"--but not to worry, I
have no intention of going through the code anyway, which is probably
understandable only to the author and an expert with years of C# under
their belt. I have about 2 months C# experience (more with C++) as a
hobbyist, and my goal is to get to the 80% competency level, which I
think I'm there, rather than get into minutiae. Like learning a
language, I want to be conversant in C#, not fluent or to teach the
rules of grammar at university.
Of course, you could always use
Reflector or even ILDASM to examine the framework assemblies. The
Dictionary class is either in mscorlib.dll or System.dll.
Yeah I notice these .dlls are always popular and have a lot of stuff
in them.
Thanks for your help, it was helpful.
RL
* In other posts I've argued that delegates are a lot of work for the
little they give you, and that inheritance is rarely used by practical
coders in OOP --both of these observations being the exceptions--that
prove the rule--for library code, meaning that for library code indeed
you use delegates and inheritance a lot. I made these observations
before I really knew much about delegates and inheritance, now that I
know a lot more, I see my initial hunch was largely right--though,
that said, I like to use inheritance and delegates in my code because
it's kind of cool (delegates are a sophisticated 'GOTO' statement;
inheritance is another form of nesting/hiding) and it keeps your
skills sharp to use these constructs once in a while so you don't
forget the syntax. I'm sure the same is also true for LINQ, which I
see as a poor man's SQL.