Re: C# Generics: breaking encapsulation?

From: K.K. (kkaitan)
Date: 01/13/05


Date: Thu, 13 Jan 2005 11:40:25 -0500

D'oh -- accidentally posted this in the wrong group. Apologies!

"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:%23dhE4eQ%23EHA.2452@TK2MSFTNGP14.phx.gbl...
> Consider the following code:
>
>>>>>>>>>>>>
> // Define an empty class
> public class ZorgleCollection : Dictionary<string, Zorgle>
> {
> }
>
> // Somewhere outside ZorgleCollection:
>
> // Print all the elements in this collection.
> public void PrintAllMembers(ZorgleCollection collection)
> {
> // Method 1 -- foreach.
> // Can't fill in the blank without specifying the types of the
> // template parameters. But this breaks encapsulation!
>
> foreach( ____(type name)____ entry in collection)
> {
> Console.WriteLine("Found entry: {0}", entry.Value);
> }
>
> // Method 2 -- use an enumerator.
> // Still can't fill in the blank without specifying the types of
> // the template parameters -- which also breaks encapsulation!
>
> ____(type name)____ enumerator = collection.GetEnumerator();
>
> while (enumerator.MoveNext())
> {
> Console.WriteLine("Found entry: {0}", entry.Value);
> }
> }
>
> // ...
> <<<<<<<<<<<
>
> We can't use an enumerator without first knowing the template's type
> parameters. By extension, since foreach is really just syntactic sugar
> around enumerators, we can't use foreach either. How can I walk through
> the collection without specifying the type, which breaks encapsulation and
> creates dependencies? Or is this a doomed effort?
>



Relevant Pages

  • Re: another IList question
    ... If you use your class that implements the IList interface later within a foreach loop - the non-generic Enumerator is called. ... foreach (string str in (IEnumerable)myList) ... public void Add ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach vs. for
    ... >> to be either reading to or writing from the array. ... >> comparing foreach with for, then due to the limitations of foreach, you ... because the Enumerator definition is only as Object. ... > optimizations made by the C# compiler. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: foreach vs. for
    ... >> to be either reading to or writing from the array. ... >> comparing foreach with for, then due to the limitations of foreach, you ... because the Enumerator definition is only as Object. ... > optimizations made by the C# compiler. ...
    (microsoft.public.dotnet.general)
  • Re: foreach or List.ForEach
    ... Not sure what you mean by that, ForEach doesn't allocate enumerators, while ... foreach on the list allocates an enumerator and calls Dispose once per ... special profiler using the HW CPU and memory controller perfcounters). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about Iteration and forEach
    ... It's not at all clear to me that simply "peeking" at the next element in the iteration would be sufficient for the original poster. ... --- The original poster said "I would like to test the next item but if it’s not the one that I want how do I put it back so that when my foreach continues it is in the next iteration". ... to me that it would be simpler to simply emulate a "peekable" enumerator through something like this: ... spend plenty of time offering solutions to others, and in fact very little of my time is spent "knocking others for actually trying". ...
    (microsoft.public.dotnet.languages.csharp)

Loading