本文整理汇总了C#中HashTable.GetOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.GetOrDefault方法的具体用法?C# HashTable.GetOrDefault怎么用?C# HashTable.GetOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable.GetOrDefault方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
/* 4 Implement the data structure "hash table" in a class HashTable<K,T>. Keep the data in array of
* lists of key-value pairs (LinkedList<KeyValuePair<K,T>>[]) with initial capacity of 16. When the
* hash table load runs over 75%, perform resizing to 2 times larger capacity. Implement the following
* methods and properties: Add(key, value), Find(key)value, Remove( key), Count, Clear(), this[], Keys.
* Try to make the hash table to support iterating over its elements with foreach.
* */
static void Main(string[] args)
{
var ht = new HashTable<string, int>();
foreach (var word in new[] { "aaa", "aaa", "bbb", "ccc", "aaa", "bbb" })
ht.Add(word, 1 + ht.GetOrDefault(word, 0));
Debug.Assert(ht["aaa"] == 3);
Debug.Assert(ht["bbb"] == 2);
Debug.Assert(ht["ccc"] == 1);
var ht2 = new HashTable<MockHash, string>(4);
Debug.Assert(ht2.Count == 0);
Debug.Assert(ht2.OccupiedBuckets == 0);
Debug.Assert(ht2.Capacity == 4);
ht2[new MockHash(0, 0)] = "00";
Debug.Assert(ht2.Count == 1);
Debug.Assert(ht2.OccupiedBuckets == 1);
Debug.Assert(ht2.Capacity == 4);
ht2[new MockHash(0, 1)] = "01";
Debug.Assert(ht2.Count == 2);
Debug.Assert(ht2.OccupiedBuckets == 1);
Debug.Assert(ht2.Capacity == 4);
ht2[new MockHash(1, 2)] = "12";
Debug.Assert(ht2.Count == 3);
Debug.Assert(ht2.OccupiedBuckets == 2);
Debug.Assert(ht2.Capacity == 8);
ht2[new MockHash(1, 3)] = "13";
Debug.Assert(ht2.Count == 4);
Debug.Assert(ht2.OccupiedBuckets == 2);
Debug.Assert(ht2.Capacity == 8);
Debug.Assert(ht2.Remove(new MockHash(1, 3)));
Debug.Assert(!ht2.Remove(new MockHash(1, 3)));
Debug.Assert(ht2.Count == 3);
Debug.Assert(ht2.OccupiedBuckets == 2);
Debug.Assert(ht2.Capacity == 8);
Debug.Assert(ht2.Remove(new MockHash(1, 2)));
Debug.Assert(!ht2.Remove(new MockHash(1, 2)));
Debug.Assert(ht2.Count == 2);
Debug.Assert(ht2.OccupiedBuckets == 1);
Debug.Assert(ht2.Capacity == 8);
}