Re: C# Generics: breaking encapsulation?
From: K.K. (kkaitan)
Date: 01/13/05
- Next message: Amund Glomsås: "VC+ and USB detection"
- Previous message: ipramod_at_gmail.com: "Re: Webcontrols & TreeNav issue after upgrades"
- In reply to: K.K.: "C# Generics: breaking encapsulation?"
- Messages sorted by: [ date ] [ thread ]
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?
>
- Next message: Amund Glomsås: "VC+ and USB detection"
- Previous message: ipramod_at_gmail.com: "Re: Webcontrols & TreeNav issue after upgrades"
- In reply to: K.K.: "C# Generics: breaking encapsulation?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|