IDictionary

  • Thread starter Thread starter CD
  • Start date Start date
C

CD

Hi,
I'm trying to learn C#. I'm coming from Smalltalk. I need a good example of
implementing a Dictionary (Keys->values) without having to create my own
subclass.
I see how to add, but not sure how to create and instance, how to access the
value for one key

thanks
 
Hi CD,

Hmm...

If by "a good example of implementing a Dictionary" you mean *using* an
existing dictionary class, then the Hashtable class implements the
IDictionary interface, and can be used as a dictionary.

Create a C# console application, and paste this code into/over the Main
method call:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Hashtable h = new Hashtable();
h.Add(@"001", @"One");
h.Add(@"002", @"Two");
h.Add(@"003", @"Three");
h.Add(@"004", @"Four");

// Access values via keys
Console.WriteLine(@"ONE = {0}", h[@"001"]);
Console.WriteLine(@"THREE = {0}", h[@"003"]);
Console.ReadLine();
}

Hope this helps...

regards
Patrick
 
Yes it does.
Thanks

P.Simpe-Asante said:
Hi CD,

Hmm...

If by "a good example of implementing a Dictionary" you mean *using* an
existing dictionary class, then the Hashtable class implements the
IDictionary interface, and can be used as a dictionary.

Create a C# console application, and paste this code into/over the Main
method call:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Hashtable h = new Hashtable();
h.Add(@"001", @"One");
h.Add(@"002", @"Two");
h.Add(@"003", @"Three");
h.Add(@"004", @"Four");

// Access values via keys
Console.WriteLine(@"ONE = {0}", h[@"001"]);
Console.WriteLine(@"THREE = {0}", h[@"003"]);
Console.ReadLine();
}

Hope this helps...

regards
Patrick


CD said:
Hi,
I'm trying to learn C#. I'm coming from Smalltalk. I need a good example
of
implementing a Dictionary (Keys->values) without having to create my own
subclass.
I see how to add, but not sure how to create and instance, how to access
the
value for one key

thanks
 
Back
Top