本文整理汇总了C#中HashTable.AddOrReplace方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.AddOrReplace方法的具体用法?C# HashTable.AddOrReplace怎么用?C# HashTable.AddOrReplace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable.AddOrReplace方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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("--------------------");
}
示例2: 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();
}
}
示例3: 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("--------------------");
}
示例4: AddOrReplace_WithDuplicates_ShouldWorkCorrectly
public void AddOrReplace_WithDuplicates_ShouldWorkCorrectly()
{
// Arrange
var hashTable = new HashTable<string, int>();
// Act
hashTable.AddOrReplace("Peter", 555);
hashTable.AddOrReplace("Maria", 999);
hashTable.AddOrReplace("Maria", 123);
hashTable.AddOrReplace("Maria", 6);
hashTable.AddOrReplace("Peter", 5);
// Assert
var expectedElements = new KeyValue<string, int>[]
{
new KeyValue<string, int>("Peter", 5),
new KeyValue<string, int>("Maria", 6)
};
var actualElements = hashTable.ToList();
CollectionAssert.AreEquivalent(expectedElements, actualElements);
}
示例5: Count_Empty_Add_Remove_ShouldWorkCorrectly
public void Count_Empty_Add_Remove_ShouldWorkCorrectly()
{
// Arrange
var hashTable = new HashTable<string, int>();
// Assert
Assert.AreEqual(0, hashTable.Count);
// Act & Assert
hashTable.Add("Peter", 555);
hashTable.AddOrReplace("Peter", 555);
hashTable.AddOrReplace("Ivan", 555);
Assert.AreEqual(2, hashTable.Count);
// Act & Assert
hashTable.Remove("Peter");
Assert.AreEqual(1, hashTable.Count);
// Act & Assert
hashTable.Remove("Peter");
Assert.AreEqual(1, hashTable.Count);
// Act & Assert
hashTable.Remove("Ivan");
Assert.AreEqual(0, hashTable.Count);
}