当前位置: 首页>>代码示例>>C#>>正文


C# HashTable.ContainsKey方法代码示例

本文整理汇总了C#中HashTable.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.ContainsKey方法的具体用法?C# HashTable.ContainsKey怎么用?C# HashTable.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HashTable的用法示例。


在下文中一共展示了HashTable.ContainsKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

    static void Main()
    {
        var grades = new HashTable<string, int>();
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        grades.Add("Peter", 3);
        grades.Add("Maria", 6);
        grades["George"] = 5;
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        grades.AddOrReplace("Peter", 33);
        grades.AddOrReplace("Tanya", 4);
        grades["George"] = 55;
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        Console.WriteLine("Keys: " + string.Join(", ", grades.Keys));
        Console.WriteLine("Values: " + string.Join(", ", grades.Values));
        Console.WriteLine("Count = " + string.Join(", ", grades.Count));
        Console.WriteLine("--------------------");

        grades.Remove("Peter");
        grades.Remove("George");
        grades.Remove("George");
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        Console.WriteLine("ContainsKey[\"Tanya\"] = " + grades.ContainsKey("Tanya"));
        Console.WriteLine("ContainsKey[\"George\"] = " + grades.ContainsKey("George"));
        Console.WriteLine("Grades[\"Tanya\"] = " + grades["Tanya"]);
        Console.WriteLine("--------------------");
    }
开发者ID:bobosam,项目名称:DataStructures,代码行数:34,代码来源:HashTableExample.cs

示例2: ShouldAlwaysReturnFalseWhenTableIsEmpty

        public void ShouldAlwaysReturnFalseWhenTableIsEmpty()
        {
            var hashtable = new HashTable<int, string>();

            Assert.IsFalse(hashtable.ContainsKey(-1));
            Assert.IsFalse(hashtable.ContainsKey(0));
            Assert.IsFalse(hashtable.ContainsKey(1));
        }
开发者ID:VladimirDimov,项目名称:DSA,代码行数:8,代码来源:ContainsKeyTests.cs

示例3: Main

        internal static void Main()
        {
            string decorationLine = new string('-', Console.WindowWidth);
            Console.Write(decorationLine);
            Console.WriteLine("***Presenting the functionality of the data structure 'Hash table'***");
            Console.Write(decorationLine);

            HashTable<string, int> clubsPoints = new HashTable<string, int>();

            Console.WriteLine("---Add operation---");
            clubsPoints.Add("Rabbits-O!", 15);
            clubsPoints.Add("Frogssy", 19);
            clubsPoints.Add("Puma Runners", 17);
            clubsPoints.Add("Lion Kings", 33);
            Console.WriteLine("Count = " + clubsPoints.Count);
            Console.WriteLine();

            Console.WriteLine("---Iterator functionality---");
            PrintClubsPoints(clubsPoints);
            Console.WriteLine();

            Console.WriteLine("---Remove operation---");
            bool isRemoved = clubsPoints.Remove("Frogssy");
            Console.WriteLine("Are 'Frogssy' removed: " + isRemoved);
            Console.WriteLine("Count = " + clubsPoints.Count);
            Console.WriteLine("Hash table after removal:");
            PrintClubsPoints(clubsPoints);
            Console.WriteLine();

            Console.WriteLine("---Indexer---");
            Console.WriteLine("'Lion Kings' points so far: " + clubsPoints["Lion Kings"]);
            clubsPoints["Rabbits-O!"] += 3;
            Console.WriteLine("'Rabbits-O!' points after a win: " + clubsPoints["Rabbits-O!"]);
            Console.WriteLine();

            Console.WriteLine("---Find operation---");
            Console.WriteLine("Info about 'Puma Runners'" + clubsPoints.Find("Puma Runners"));
            Console.WriteLine();

            Console.WriteLine("---ContainsKey operation---");
            Console.WriteLine("Is there a club 'Birdy'? - " + clubsPoints.ContainsKey("Birdy"));
            Console.WriteLine("Is there a club 'Rabbits-O!'? - " + clubsPoints.ContainsKey("Rabbits-O!"));
            Console.WriteLine();

            Console.WriteLine("---Keys property---");
            Console.Write("All clubs names are: ");
            Console.WriteLine(string.Join(", ", clubsPoints.Keys));
            Console.WriteLine();

            Console.WriteLine("---Values property---");
            Console.Write("All club's points are: ");
            Console.WriteLine(string.Join(", ", clubsPoints.Values));
            Console.WriteLine();

            Console.WriteLine("---Clear operation---");
            clubsPoints.Clear();
            Console.WriteLine("Elements count after clearing: " + clubsPoints.Count);
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:58,代码来源:HashTableDemo.cs

示例4: ShouldReturnAppropriateResults

        public void ShouldReturnAppropriateResults()
        {
            var hashtable = new HashTable<int, string>();

            int numberOfAddedElements = 5;
            for (int i = 0; i < numberOfAddedElements; i++)
            {
                hashtable.Add(i, "test" + i);
            }

            var expectedTrue = hashtable.ContainsKey(3);
            var expectedFalse = hashtable.ContainsKey(-1);

            Assert.IsTrue(expectedTrue);
            Assert.IsFalse(expectedFalse);
        }
开发者ID:VladimirDimov,项目名称:DSA,代码行数:16,代码来源:ContainsKeyTests.cs

示例5: Main

        static void Main()
        {
            HashTable<string, string> hashTable = new HashTable<string, string>();

            string input = Console.ReadLine();

            while (input != "search")
            {
                string[] parameters = input.Split('-');
                string contact = parameters[0];
                string number = parameters[1];

                hashTable[contact] = number;

                input = Console.ReadLine();
            }

            input = Console.ReadLine();

            while (input != "end")
            {
                if (hashTable.ContainsKey(input))
                {
                    Console.WriteLine("{0} -> {1}", input, hashTable[input]);
                }
                else
                {
                    Console.WriteLine("Contact {0} does not exist", input);
                }

                input = Console.ReadLine();
            }
        }
开发者ID:dev-velkov,项目名称:Software-University-Courses,代码行数:33,代码来源:Program.cs

示例6: Main

        static void Main()
        {
            string input = Console.ReadLine();
            var phonebook = new HashTable<string, string>();

            while (input != "search")
            {
                string[] record = input.Split( new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
                phonebook.AddOrReplace(record[0], record[1]);
                input = Console.ReadLine();
            }
            
            input = Console.ReadLine();
            
            while (input != "")
            {
                if (phonebook.ContainsKey(input))
                {
                    Console.WriteLine("{0} -> {1}", input, phonebook[input]);
                }
                else
                {
                    Console.WriteLine("Contact {0} does not exist.", input);
                }

                input = Console.ReadLine();
            }
        }
开发者ID:pkanev,项目名称:Data.Structures,代码行数:28,代码来源:PhonebookMain.cs

示例7: ClearShouldEmptyTheTableAndKeepTheCurrentCapacity

        public void ClearShouldEmptyTheTableAndKeepTheCurrentCapacity()
        {
            var table = new HashTable<string, List<int>>();
            for (int i = 0; i < 50; i++)
            {
                table[i.ToString()] = new List<int>();
                for (int j = 0; j < 5; j++)
                {
                    table[i.ToString()].Add(j);
                }

                Assert.IsTrue(table[i.ToString()].Count > 0);
            }

            int finalCapacity = table.Capacity;
            table.Clear();

            Assert.IsTrue(finalCapacity >= 50);
            Assert.AreEqual(finalCapacity, table.Capacity);
            Assert.AreEqual(0, table.Count);

            for (int i = 0; i < 50; i++)
            {
                Assert.IsFalse(table.ContainsKey(i.ToString()));
            }
        }
开发者ID:kidroca,项目名称:Data-Structures-And-Algorithms-2015,代码行数:26,代码来源:HashTableFunctionallity.cs

示例8: AddingNewKeyPairShouldWorkCorrect

        public void AddingNewKeyPairShouldWorkCorrect()
        {
            var hashTable = new HashTable<string, int>();
            hashTable.Add("Ivan", 1);

            Assert.IsTrue(hashTable.ContainsKey("Ivan"));
            Assert.AreEqual(hashTable["Ivan"], 1);
        }
开发者ID:dchakov,项目名称:Data-Structures-and-Algorithms_HW,代码行数:8,代码来源:HashTableTests.cs

示例9: Main

        static void Main()
        {
            var phoneBook = new HashTable<string, string>();
            string inputContactInfo = Console.ReadLine();

            while (!inputContactInfo.Equals("search"))
            {
                string[] nameAndPhone = inputContactInfo.Split('-');
                string name = nameAndPhone[0];
                string phoneNumber = nameAndPhone[1];

                if (!phoneBook.ContainsKey(name))
                {
                    phoneBook[name] = phoneNumber;
                }
                else
                {
                    Console.WriteLine("* The name \"{0}\" already exists in contact list.", name);
                }
                inputContactInfo = Console.ReadLine();
            }

            Console.WriteLine();
            Console.WriteLine("-- Please, input \"end\" to see the search results --\n");
            string inputName = Console.ReadLine();
            var names = new List<string>();

            while (!inputName.Equals("end"))
            {
                names.Add(inputName);
                inputName = Console.ReadLine();
            }

            Console.WriteLine();
            foreach (var name in names)
            {
                if (phoneBook.ContainsKey(name))
                {
                    Console.WriteLine("{0} -> {1}", name, phoneBook[name]);
                }
                else
                {
                    Console.WriteLine("Contact {0} does not exist.", name);
                }
            }
        }
开发者ID:bobosam,项目名称:DataStructures,代码行数:46,代码来源:Phonebook.cs

示例10: Main

    public static void Main()
    {
        var topCities = new HashTable<string, double>();

        // Add(key, value)
        topCities.Add("Tokyo", 36.9);
        topCities.Add("Jakarta", 12.8);
        topCities.Add("Seoul", 25.7);
        topCities.Add("Beijing", 23.7);
        topCities.Add("Shanghai", 21.7);

        // foreach
        foreach (var pair in topCities.OrderBy(p => -p.Value))
        {
            Console.WriteLine("{0, -8} -> {1}", pair.Key, pair.Value);
        }

        // Add already existing key
        try
        {
            topCities.Add("Jakarta", 0);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        
        // Keys
        Console.WriteLine("Keys: {0}", string.Join(", ", topCities.Keys));

        // Count
        Console.WriteLine("Number of key-value pairs: {0}", topCities.Count);

        // Find(key)
        topCities.PrintValue("Tokyo");
        Console.WriteLine("Jakarta -> {0}", topCities.Find("Jakarta"));

        // this[key]
        Console.WriteLine("Beijing -> {0}", topCities["Beijing"]);

        // ContainsKey(key)
        Console.WriteLine("Hash table contains 'Delhi': {0}", topCities.ContainsKey("Delhi"));

        // Remove(key)
        topCities.Remove("Seoul");
        Console.WriteLine("Remove 'Seoul'");
        Console.WriteLine("Keys: {0}", string.Join(", ", topCities.Keys));
        Console.WriteLine("Number of key-value pairs: {0}", topCities.Count);

        // Remove non-existing
        Console.WriteLine("Removing 'Sofia' -> {0}", topCities.Remove("Sofia"));

        // Clear
        topCities.Clear();
        Console.WriteLine("Clear hash table");
        Console.WriteLine("Number of key-value pairs: {0}", topCities.Count);
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:57,代码来源:Program.cs

示例11: Main

    static void Main()
    {
        // Arrange
        var hashTable = new HashTable<DateTime, string>();
        var date = new DateTime(1995, 7, 18);
        hashTable.Add(date, "Some value");

        // Act
        var containsKey = hashTable.ContainsKey(date);

        ///////////////////////////////////////////////////
        var grades = new HashTable<string, int>();
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        grades.Add("Peter", 3);
        grades.Add("Maria", 6);
        grades["George"] = 5;
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        grades.AddOrReplace("Peter", 33);
        grades.AddOrReplace("Tanya", 4);
        grades["George"] = 55;
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        Console.WriteLine("Keys: " + string.Join(", ", grades.Keys));
        Console.WriteLine("Values: " + string.Join(", ", grades.Values));
        Console.WriteLine("Count = " + string.Join(", ", grades.Count));
        Console.WriteLine("--------------------");

        grades.Remove("Peter");
        grades.Remove("George");
        grades.Remove("George");
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        Console.WriteLine("ContainsKey[\"Tanya\"] = " + grades.ContainsKey("Tanya"));
        Console.WriteLine("ContainsKey[\"George\"] = " + grades.ContainsKey("George"));
        Console.WriteLine("Grades[\"Tanya\"] = " + grades["Tanya"]);
        Console.WriteLine("--------------------");
    }
开发者ID:bobosam,项目名称:DataStructures,代码行数:43,代码来源:HashTableExample.cs

示例12: ContainsKeyFalseTest

        public void ContainsKeyFalseTest()
        {
            HashTable<string, int> studentMarks = new HashTable<string, int>();
            studentMarks.Add("Pesho", 4);
            studentMarks.Add("Lili", 6);
            studentMarks.Add("Gosho", 3);

            bool containsIvanStudent = studentMarks.ContainsKey("Ivan");

            Assert.IsFalse(containsIvanStudent);
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:11,代码来源:ContainsKeyOperation.cs

示例13: AddingAnItemShouldAddItemAndIncreaseCount

        public void AddingAnItemShouldAddItemAndIncreaseCount()
        {
            var table = new HashTable<double, int>();
            Assert.AreEqual(0, table.Count);

            for (int i = 1; i < 11; i++)
            {
                table.Add(i, i);
                Assert.AreEqual(i, table.Count);
                Assert.IsTrue(table.ContainsKey(i));
            }
        }
开发者ID:kidroca,项目名称:Data-Structures-And-Algorithms-2015,代码行数:12,代码来源:HashTableFunctionallity.cs

示例14: ProcessContactInfo

 private static void ProcessContactInfo(string line, HashTable<string, string> contactInfo)
 {
     var info = line.Split('-').Select(i => i.Trim()).ToList();
     if (!contactInfo.ContainsKey(info[0]))
     {
         contactInfo[info[0]] = info[1];
     }
     else
     {
         var message = string.Format("{0} allready exist in the dictionary!", info[0]);
         throw new ArgumentException(message);
     }
 }
开发者ID:peterkirilov,项目名称:SoftUni-1,代码行数:13,代码来源:Phonebook.cs

示例15: Main

 internal static void Main()
 {
     var myLetters = new HashTable<string, int>();
     myLetters.Add("a", 1);
     myLetters.Add("b", 1);
     myLetters.Add("c", 1);
     myLetters.Add("d", 1);
     myLetters.Add("e", 1);
     myLetters.Add("f", 1);
     myLetters.Add("g", 1);
     myLetters.Add("h", 1);
     myLetters.Add("i", 1);
     myLetters.Add("j", 1);
     myLetters.Add("k", 1);
     myLetters.Add("l", 1);
     myLetters.Add("m", 1);
     myLetters.Add("n", 1);
     myLetters.Add("o", 1);
     myLetters.Add("p", 1);
     myLetters.Add("q", 1);
     myLetters.Add("r", 1);
     myLetters.Add("s", 1);
     Console.WriteLine(string.Join(", ", myLetters));
     Console.WriteLine();
     myLetters["o"] = 6;
     Console.WriteLine("Now we change letter \"{0}\"  to {1} times.", "o", myLetters["o"]);
     Console.WriteLine(string.Join(", ", myLetters));
     Console.WriteLine();
     myLetters.Remove("q");
     Console.WriteLine("After removal of letter \"q\": {0}", string.Join(", ", myLetters));
     Console.WriteLine();
     Console.WriteLine("MyLetters contain \"z\"? {0}", myLetters.ContainsKey("z"));
     Console.WriteLine("MyLetters contain \"a\"? {0}", myLetters.ContainsKey("a"));
     Console.WriteLine();
     Console.WriteLine("Keys: {0}", string.Join(", ", myLetters.Keys()));
     Console.WriteLine();
     myLetters.Clear();
     Console.WriteLine("MyLetters after clear: {0}", string.Join(", ", myLetters));
 }
开发者ID:kalinalazarova1,项目名称:TelerikAcademy,代码行数:39,代码来源:HashTableTest.cs


注:本文中的HashTable.ContainsKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。