本文整理汇总了C#中TempDataDictionary.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# TempDataDictionary.Clear方法的具体用法?C# TempDataDictionary.Clear怎么用?C# TempDataDictionary.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TempDataDictionary
的用法示例。
在下文中一共展示了TempDataDictionary.Clear方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TempDataIsADictionary
public void TempDataIsADictionary()
{
// Arrange
TempDataDictionary tempData = new TempDataDictionary();
// Act
tempData["Key1"] = "Value1";
tempData.Add("Key2", "Value2");
((ICollection<KeyValuePair<string, object>>)tempData).Add(new KeyValuePair<string,object>("Key3", "Value3"));
// Assert (IDictionary)
Assert.AreEqual(3, tempData.Count, "tempData should contain 3 items");
Assert.IsTrue(tempData.Remove("Key1"), "The key should be present");
Assert.IsFalse(tempData.Remove("Key4"), "The key should not be present");
Assert.IsTrue(tempData.ContainsValue("Value2"), "The value should be present");
Assert.IsFalse(tempData.ContainsValue("Value1"), "The value should not be present");
Assert.IsNull(tempData["Key6"], "The key should not be present");
IEnumerator tempDataEnumerator = tempData.GetEnumerator();
tempDataEnumerator.Reset();
while (tempDataEnumerator.MoveNext()) {
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)tempDataEnumerator.Current;
Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair), "The key/value pair should be present");
}
// Assert (ICollection)
foreach (string key in tempData.Keys) {
Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(new KeyValuePair<string, object>(key, tempData[key])), "The key/value pair should be present");
}
foreach (string value in tempData.Values) {
Assert.IsTrue(tempData.ContainsValue(value));
}
foreach (string key in ((IDictionary<string, object>)tempData).Keys) {
Assert.IsTrue(tempData.ContainsKey(key), "The key should be present");
}
foreach (string value in ((IDictionary<string, object>)tempData).Values) {
Assert.IsTrue(tempData.ContainsValue(value));
}
KeyValuePair<string, object>[] keyValuePairArray = new KeyValuePair<string, object>[tempData.Count];
((ICollection<KeyValuePair<string, object>>)tempData).CopyTo(keyValuePairArray, 0);
Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)tempData).IsReadOnly, "The dictionary should not be read-only.");
Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)tempData).Remove(new KeyValuePair<string, object>("Key5", "Value5")), "The key/value pair should not be present");
IEnumerator<KeyValuePair<string, object>> keyValuePairEnumerator = ((ICollection<KeyValuePair<string, object>>)tempData).GetEnumerator();
keyValuePairEnumerator.Reset();
while (keyValuePairEnumerator.MoveNext()) {
KeyValuePair<string, object> pair = keyValuePairEnumerator.Current;
Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair), "The key/value pair should be present");
}
// Act
tempData.Clear();
// Assert
Assert.AreEqual(0, tempData.Count, "tempData should not contain any items");
IEnumerator y = ((IEnumerable)tempData).GetEnumerator();
while (y.MoveNext()) {
Assert.Fail("There should not be any elements in tempData");
}
}
示例2: TempDataIsADictionary
public void TempDataIsADictionary()
{
// Arrange
TempDataDictionary tempData = new TempDataDictionary();
// Act
tempData["Key1"] = "Value1";
tempData.Add("Key2", "Value2");
((ICollection<KeyValuePair<string, object>>)tempData).Add(new KeyValuePair<string, object>("Key3", "Value3"));
// Assert (IDictionary)
Assert.Equal(3, tempData.Count);
Assert.True(tempData.Remove("Key1"));
Assert.False(tempData.Remove("Key4"));
Assert.True(tempData.ContainsValue("Value2"));
Assert.False(tempData.ContainsValue("Value1"));
Assert.Null(tempData["Key6"]);
IEnumerator tempDataEnumerator = tempData.GetEnumerator();
tempDataEnumerator.Reset();
while (tempDataEnumerator.MoveNext())
{
KeyValuePair<string, object> pair = (KeyValuePair<string, object>)tempDataEnumerator.Current;
Assert.True(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair));
}
// Assert (ICollection)
foreach (string key in tempData.Keys)
{
Assert.True(((ICollection<KeyValuePair<string, object>>)tempData).Contains(new KeyValuePair<string, object>(key, tempData[key])));
}
foreach (string value in tempData.Values)
{
Assert.True(tempData.ContainsValue(value));
}
foreach (string key in ((IDictionary<string, object>)tempData).Keys)
{
Assert.True(tempData.ContainsKey(key));
}
foreach (string value in ((IDictionary<string, object>)tempData).Values)
{
Assert.True(tempData.ContainsValue(value));
}
KeyValuePair<string, object>[] keyValuePairArray = new KeyValuePair<string, object>[tempData.Count];
((ICollection<KeyValuePair<string, object>>)tempData).CopyTo(keyValuePairArray, 0);
Assert.False(((ICollection<KeyValuePair<string, object>>)tempData).IsReadOnly);
Assert.False(((ICollection<KeyValuePair<string, object>>)tempData).Remove(new KeyValuePair<string, object>("Key5", "Value5")));
IEnumerator<KeyValuePair<string, object>> keyValuePairEnumerator = ((ICollection<KeyValuePair<string, object>>)tempData).GetEnumerator();
keyValuePairEnumerator.Reset();
while (keyValuePairEnumerator.MoveNext())
{
KeyValuePair<string, object> pair = keyValuePairEnumerator.Current;
Assert.True(((ICollection<KeyValuePair<string, object>>)tempData).Contains(pair));
}
// Act
tempData.Clear();
// Assert
Assert.Empty(tempData);
}