Exemplo n.º 1
0
        static void Main(string[] args)
        {
            MultiDictionary <int, string> multyDictionary = new MultiDictionary <int, string>();

            //add values
            multyDictionary.Add(1, "one");
            multyDictionary.Add(1, "ich");
            multyDictionary.Add(2, "two");
            multyDictionary.Add(2, "nee");
            multyDictionary.Add(3, "three");
            multyDictionary.Add(3, "sun");

            // ienumrable
            foreach (var item in multyDictionary)
            {
                Console.WriteLine(item.Value);
            }

            IEnumerable d = multyDictionary;

            foreach (var item in d)
            {
                Console.WriteLine(item.ToString());
            }

            // contains
            string ans;

            if (multyDictionary.ContainsKey(3))
            {
                ans = "contains 3";
            }
            else
            {
                ans = "not contains 3";
            }
            if (multyDictionary.ContainsKey(4))
            {
                ans += " and contains 4";
            }
            else
            {
                ans += " and not contains 4";
            }
            if (multyDictionary.Contains(3, "three"))
            {
                ans += " and contains 3,three";
            }
            else
            {
                ans += " and not contains 3,three";
            }
            if (multyDictionary.Contains(3, "aa"))
            {
                ans += " and contains 3,aa";
            }
            else
            {
                ans += " and not contains 3,aa";
            }

            Console.WriteLine(ans);

            // count and remove
            Console.WriteLine("there are " + multyDictionary.Count + " items in the collection");
            multyDictionary.Remove(1, "one");
            Console.WriteLine("there are " + multyDictionary.Count + " items in the collection");

            multyDictionary.Remove(2);
            Console.WriteLine("there are " + multyDictionary.Count + " items in the collection");

            //clear
            multyDictionary.Clear();
            Console.WriteLine("there are " + multyDictionary.Count + " items in the collection");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            MultiDictionary <int, string> dictionary = new MultiDictionary <int, string>();

            dictionary.Add(1, "one");
            dictionary.Add(2, "two");
            dictionary.Add(3, "three");
            dictionary.Add(1, "ich");
            dictionary.Add(2, "nee");
            dictionary.Add(3, "sun");

            ICollection <string> values1 = dictionary.Values;
            StringBuilder        sb      = new StringBuilder();

            if (dictionary.Count > 0)
            {
                Console.WriteLine("complete list:");
                Console.WriteLine();
                int i = 1;
                iter = dictionary.GetEnumerator();
                while (iter.MoveNext())
                {
                    sb.Append("Key: ");
                    sb.Append(iter.Current.Key.ToString());
                    sb.Append(" Value: ");
                    sb.Append(iter.Current.Value.ToString());
                    Console.WriteLine(sb.ToString());
                    i++;
                    sb.Clear();
                }
                Console.WriteLine();
            }


            System.Collections.Generic.ICollection <int> keys = dictionary.Keys;
            ICollection <string> values2 = dictionary.Values;
            bool test  = dictionary.Contains(3, "three");
            bool test2 = dictionary.Remove(3, "three");
            bool test3 = dictionary.Contains(3, "three");

            Console.WriteLine();

            Console.WriteLine("list after removing {3,three}");

            iter = dictionary.GetEnumerator();
            if (dictionary.Count > 0)
            {
                int i = 1;
                while (iter.MoveNext())
                {
                    sb.Append("Key: ");
                    sb.Append(iter.Current.Key.ToString());
                    sb.Append(" Value: ");
                    sb.Append(iter.Current.Value.ToString());
                    Console.WriteLine(sb.ToString());
                    i++;
                    sb.Clear();
                }
            }

            Console.WriteLine();
            bool test4 = dictionary.Remove(3);
            bool test5 = dictionary.Contains(3, "sun");

            Console.WriteLine("list after removing key number 3:");
            Console.WriteLine();
            iter = dictionary.GetEnumerator();
            if (dictionary.Count > 0)
            {
                int i = 1;
                while (iter.MoveNext())
                {
                    sb.Append("Key: ");
                    sb.Append(iter.Current.Key.ToString());
                    sb.Append(" Value: ");
                    sb.Append(iter.Current.Value.ToString());
                    Console.WriteLine(sb.ToString());
                    i++;
                    sb.Clear();
                }
            }
        }
        static void Main(string[] args)
        {
            MultiDictionary <int, string> md = new  MultiDictionary <int, string>();

            md.Add(1, "one");
            md.Add(2, "two");
            md.Add(3, "three");
            md.Add(1, "ich");
            md.Add(2, "nee");
            md.Add(3, "sun");
            Console.WriteLine("*******************************");
            Console.WriteLine("checking IEnumerable: ");
            foreach (var val in (IEnumerable)md)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("*******************************");
            Console.WriteLine("your multiDictionary size: " + md.Count);
            Console.WriteLine("*******************************");
            Console.WriteLine("remove key 1");
            md.Remove(1);
            Console.WriteLine("*******************************");

            Console.WriteLine("your multiDictionary size after: " + md.Count);
            Console.WriteLine("*******************************");
            Console.WriteLine("checking IEnumerable: ");
            foreach (var val in (IEnumerable)md)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("*******************************");
            Console.WriteLine("remove (2,two)");
            md.Remove(2, "two");
            Console.WriteLine("your multiDictionary size after: " + md.Count);
            Console.WriteLine("*******************************");
            Console.WriteLine("checking IEnumerable: ");
            foreach (var val in (IEnumerable)md)
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("*******************************");

            Console.WriteLine("try to remove value that not exits return: " + md.Remove(3, "kkkk"));
            Console.WriteLine("*******************************");
            Console.WriteLine("checking contains (4,four) return " + md.Contains(4, "four"));
            Console.WriteLine("*******************************");
            Console.WriteLine("checking contains (3,three) return " + md.Contains(3, "three"));
            Console.WriteLine("*******************************");
            Console.WriteLine("checking contains (4) return " + md.ContainsKey(4));
            Console.WriteLine("*******************************");
            Console.WriteLine("checking contains (3) return " + md.ContainsKey(3));
            Console.WriteLine("*******************************");
            Console.WriteLine("your values: ");
            var values = md.Values;

            foreach (var val in (values))
            {
                Console.WriteLine(val);
            }
            Console.WriteLine("*******************************");
            Console.WriteLine("your keys: ");
            var keys = md.Keys;

            foreach (var key in keys)
            {
                Console.WriteLine(key);
            }
            Console.WriteLine("*******************************");
            Console.WriteLine("clear : ");
            md.Clear();
            Console.WriteLine("your multiDictionary size after: " + md.Count);
            Console.WriteLine("*******************************");
        }
Exemplo n.º 4
0
        public static void Main()
        {
            MultiDictionary <int, string> multiDictionary = new MultiDictionary <int, string>();

            multiDictionary.Add(1, "one");
            multiDictionary.Add(2, "two");
            multiDictionary.Add(3, "three");
            multiDictionary.Add(1, "ich");
            multiDictionary.Add(2, "nee");
            multiDictionary.Add(3, "sun");

            foreach (KeyValuePair <int, string> item in multiDictionary)
            {
                Console.WriteLine("the key:{0}, the value: {1}", item.Key, item.Value);
            }

            Console.WriteLine("All the values in dictionary");
            foreach (string valueItem in multiDictionary.Values)
            {
                Console.WriteLine("{0},", valueItem);
            }
            Console.WriteLine("All the keys in the dictionary");

            foreach (int keyItem in multiDictionary.Keys)
            {
                Console.WriteLine("{0},", keyItem);
            }
            Console.WriteLine("the number of items in dictionary {0}", multiDictionary.Count);
            bool isRemoved = multiDictionary.Remove(1, "one");

            if (isRemoved)
            {
                Console.WriteLine("Dictionary after removal");
                foreach (KeyValuePair <int, string> item in multiDictionary)
                {
                    Console.WriteLine("the key:{0}, the value: {1}", item.Key, item.Value);
                }
            }
            else
            {
                Console.WriteLine("cant Remove this item");
            }

            bool isKeyRemoved = multiDictionary.Remove(1);

            if (isKeyRemoved)
            {
                Console.WriteLine("The dictionary after removal");
                foreach (KeyValuePair <int, string> item in multiDictionary)
                {
                    Console.WriteLine("the key:{0}, the value: {1}", item.Key, item.Value);
                }
            }
            else
            {
                Console.WriteLine("cant Remove this item ,its not exist");
            }

            bool isContainKey = multiDictionary.ContainKey(3);

            if (isContainKey)
            {
                Console.WriteLine("the dictionaty contains key");
            }
            else
            {
                Console.WriteLine("The key doesn't exist");
            }

            bool isContains = multiDictionary.Contains(3, null);

            if (isContains)
            {
                Console.WriteLine("The dictionary contains the value");
            }
            else
            {
                Console.WriteLine("No such value");
            }

            multiDictionary.Clear();
        }