C# hash table of array list is not printing any results

I am creating an hash table in C# then adding key for it and i am adding array list as values

Then i am tring to print array list which is values but it is not working

using System;

namespace text.cs
{
    class Exec
    {
        static void Main(string[] args) 
        {
            Hashtable variations_array = new Hashtable();
            ArrayList item_array = new ArrayList();
            item_array.Add ("one");
            item_array.Add ("two");
            variations_array.Add ("hi", item_array);
            if (variations_array.Contains("one"))
            {
                Console.WriteLine("This student name is already in the list");
            }

            foreach (DictionaryEntry entry in variations_array)
            {
                //Console.Write (entry.Key);
                string sv = ( entry.Key as string[] )[ 0 ];
                Console.WriteLine("{0}",sv);


            }

            foreach (KeyValuePair<string, List<string>> pair in variations_array)
            {
                Console.WriteLine(pair.Key);
                foreach (string item in pair.Value)
                    Console.WriteLine("\t" + item);
            }
        }
    }
}

I have three print statements(Console.Write) but none of them are working?

How to make it work?

Jon Skeet
people
quotationmark

This condition checks for a key with a value "one":

variations_array.Contains("one")

That doesn't exist - you've added a single entry with a key of "hi". That's why your first Console.WriteLine doesn't execute.

Next:

string sv = ( entry.Key as string[] )[ 0 ];
Console.WriteLine("{0}",sv);

The key isn't a string array - it's a single string. Therefore the as operator returns null, and you should get a NullReferenceException.

Next:

foreach (KeyValuePair<string, List<string>> pair in variations_array)

This is performing an implicit cast of the DictionaryEntry to KeyValuePair<string, List<string>>. That cast will fail. So if your code hadn't thrown an exception due to the previous problem, it would fail here.

It's hard to know how to "fix" this as we're not really clear on what you're trying to achieve, but I'd strongly advise you to start using the generic collections (e.g. List<T> and Dictionary<TKey, TValue>) which will make life much simpler:

var dictionary = new Dictionary<string, List<string>>();
dictionary["hi"] = new List<string> { "one", "two" };
foreach (var entry in dictionary)
{
    Console.WriteLine("Key: {0}", entry.Key);
    Console.WriteLine("Values: {0}", string.Join(", ", entry.Value));
}

people

See more on this question at Stackoverflow