本文整理汇总了C#中Dictionary.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.Contains方法的具体用法?C# Dictionary.Contains怎么用?C# Dictionary.Contains使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PosTest1
public bool PosTest1()
{
bool retVal = true;
// Add your scenario description here
TestLibrary.TestFramework.BeginScenario("PosTest1: Verify method IDictionaryContains when specified key existed.");
try
{
IDictionary dictionary = new Dictionary<string, string>();
dictionary.Add("txt", "notepad.exe");
dictionary.Add("bmp", "paint.exe");
dictionary.Add("dib", "paint.exe");
dictionary.Add("rtf", "wordpad.exe");
bool testVerify = dictionary.Contains("txt") && dictionary.Contains("bmp") &&
dictionary.Contains("dib") && dictionary.Contains("rtf");
if (testVerify == false)
{
TestLibrary.TestFramework.LogError("001.1", "Method IDictionaryContains Err .");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例2: Dictionary_kvp_Contains_checks_key_and_value
public void Dictionary_kvp_Contains_checks_key_and_value()
{
IDictionary<int,string> dictionary = new Dictionary<int, string>();
dictionary[1] = "foo";
Assert.IsTrue(dictionary.Contains(new KeyValuePair<int, string>(1, "foo")));
Assert.IsFalse(dictionary.Contains(new KeyValuePair<int, string>(1, "bar")));
}
示例3: PosTest1
public bool PosTest1()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest1: The IDictionary contains the key");
try
{
IDictionary iDictionary = new Dictionary<object,object>();
Int32[] key = new Int32[1000];
Byte[] value = new Byte[1000];
for (int i = 0; i < 1000; i++)
{
key[i] = i;
}
TestLibrary.Generator.GetBytes(-55, value);
for (int i = 0; i < 1000; i++)
{
iDictionary.Add(key[i], value[i]);
}
int keyValue = this.GetInt32(0, 1000);
if (!iDictionary.Contains(keyValue))
{
TestLibrary.TestFramework.LogError("001", "The result is not the value as expected ");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例4: PosTest2
public bool PosTest2()
{
bool retVal = true;
// Add your scenario description here
TestLibrary.TestFramework.BeginScenario("PosTest2: Verify method IDictionaryRemove when the specified key is not exist.");
try
{
IDictionary dictionary = new Dictionary<string, string>();
dictionary.Remove("txt");
if (dictionary.Contains("txt") == true)
{
TestLibrary.TestFramework.LogError("002.1", "Method IDictionary.GetEnumerator Err .");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例5: PosTest2
public bool PosTest2()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest2: The key to be remove is a custom class");
try
{
IDictionary iDictionary = new Dictionary<object,object>();
MyClass mc = new MyClass();
int value = TestLibrary.Generator.GetInt32(-55);
iDictionary.Add(mc, value);
iDictionary.Remove(mc);
if (iDictionary.Contains(mc))
{
TestLibrary.TestFramework.LogError("003", "The result is not the value as expected ");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例6: PosTest1
public bool PosTest1()
{
bool retVal = true;
// Add your scenario description here
TestLibrary.TestFramework.BeginScenario("PosTest1: Verify method ICollectionContains when k/v existed.");
try
{
ICollection<KeyValuePair<String, String>> dictionary = new Dictionary<String, String>();
KeyValuePair<string, string> kvp1 = new KeyValuePair<String, String>("txt", "notepad.exe");
KeyValuePair<string, string> kvp2 = new KeyValuePair<String, String>("bmp", "paint.exe");
KeyValuePair<string, string> kvp3 = new KeyValuePair<String, String>("dib", "paint.exe");
KeyValuePair<string, string> kvp4 = new KeyValuePair<String, String>("rtf", "wordpad.exe");
dictionary.Add(kvp1);
dictionary.Add(kvp2);
dictionary.Add(kvp3);
dictionary.Add(kvp4);
bool actual = dictionary.Contains(kvp1) &&
dictionary.Contains(kvp1) &&
dictionary.Contains(kvp1) &&
dictionary.Contains(kvp1);
bool expected = true;
if (actual != expected)
{
TestLibrary.TestFramework.LogError("001.1", "Method ICollectionContains Err.");
TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actual = " + actual + ", expected = " + expected);
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例7: VersionSetsCorrectTagKeyAndValue
public void VersionSetsCorrectTagKeyAndValue()
{
IDictionary<string, string> tags = new Dictionary<string, string>();
var component = new ComponentContext(tags);
string componentVersion = "fakeVersion";
component.Version = componentVersion;
Assert.True(tags.Contains(new KeyValuePair<string, string>(ContextTagKeys.Keys.ApplicationVersion, componentVersion)));
}
示例8: CharHistogramTest
public void CharHistogramTest()
{
Dictionary<char, int> expectedDictionary = new Dictionary<char, int>()
{
// expected - 'P': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1, 'n': 1, '!': 2
{'P', 1},
{'y', 1},
{'t', 1},
{'h', 1},
{'o', 1},
{'n', 1},
{'!', 2},
};
/*Dictionary<char, int> resultDictionary = new Dictionary<char, int>() {
{'P', 1},
{'y', 1},
{'t', 1},
{'h', 1},
{'o', 1},
{'n', 1},
{'!', 2},};
*/
Histogram rclass = new Histogram();
var resultDictionary = rclass.CharHistogram("Phyton!!");
Assert.AreEqual(true,resultDictionary.All(e => expectedDictionary.Contains(e)));
/*Assert.AreEqual(resultDictionary,expectedDictionary);
Histogram rclass = new Histogram();
//var result = res.CharHistogram("Phyton!!");
foreach (var exp in expectedDictionary)
{
foreach (var res in resultDictionary)
{
if (exp.Key == res.Key)
{
if (exp.Value != res.Value)
{
Assert.Fail("exp.Val= {0} res.Val = {1} ", exp.Value, res.Value);
}
}
else
{
Assert.Fail("exp.Key= {0} res.Key = {1} ",exp.Key,res.Key);
}
}
}
*/
}
示例9: IsEquals
public static bool IsEquals(Dictionary<BaseSi, int> d1, Dictionary<BaseSi, int> d2)
{
if(d1.Count == d2.Count)
{
foreach(var item in d1)
{
if(!d2.Contains(item))
{
return false;
}
}
}
else
{
return false;
}
return true;
}
示例10: PosTest1
public bool PosTest1()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest1: Using Dictionary<string,Byte> to implemented the clear method");
try
{
IDictionary iDictionary = new Dictionary<string,Byte>();
string[] key = new string[1000];
Byte[] value = new Byte[1000];
for (int i = 0; i < 1000; i++)
{
key[i] = TestLibrary.Generator.GetString(-55, false, 1, 100);
}
TestLibrary.Generator.GetBytes(-55, value);
for (int i = 0; i < 1000; i++)
{
while (iDictionary.Contains(key[i]))
{
key[i] = TestLibrary.Generator.GetString(false, 1, 100);
}
iDictionary.Add(key[i], value[i]);
}
iDictionary.Clear();
if (iDictionary.Count != 0)
{
TestLibrary.TestFramework.LogError("001", "The result is not the value as expected ");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例11: UnionDictionariesTest
public void UnionDictionariesTest()
{
IDictionary<int, string> target = new Dictionary<int, string>();
IDictionary<int, string> source = new Dictionary<int, string>();
// simple add
target.Add(1, "one");
source.Add(2, "two");
CollectionUtil.UnionDictionaries(target, source);
Assert.IsTrue(target.ContainsKey(2));
Assert.IsTrue(target.Contains(new KeyValuePair<int,string>(2, "two")));
Assert.AreEqual(2, target.Count);
// skip existing
source.Add(3, "three");
CollectionUtil.UnionDictionaries(target, source);
Assert.AreEqual(3, target.Count);
// replace
source[3] = "new three";
CollectionUtil.UnionDictionaries(target, source);
Assert.AreEqual("new three", target[3]);
}
示例12: PosTest2
public bool PosTest2()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest2: The key is a string of white space");
try
{
IDictionary iDictionary = new Dictionary<object,object>();
iDictionary.Add(" ", true);
if (!iDictionary.Contains(" "))
{
TestLibrary.TestFramework.LogError("003", "The result is not the value as expected ");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例13: AssertCorrectParameters
private bool AssertCorrectParameters(Dictionary<string, string> expected, Dictionary<string, string> paramsToCheck)
{
return paramsToCheck.Count == expected.Count && paramsToCheck.All(p => expected.Contains(p));
}
示例14: RemoveSendStatuses
private void RemoveSendStatuses(Dictionary<int, int> temp_statuses)
{
Dictionary<int, int> temp_statuses2 = new Dictionary<int, int>();
try
{
lock (statuses)
{
foreach (KeyValuePair<int, int> o in statuses)
if (!temp_statuses.Contains(o))
temp_statuses2.Add(o.Key, o.Value);
statuses.Clear();
foreach (KeyValuePair<int, int> o in temp_statuses2)
statuses.Add(o.Key, o.Value);
}
}
catch (Exception ex) { Console.WriteLine("Произошла ошибка при очистке статусов "+ex.Message); }
finally
{
if (temp_statuses2 != null) { temp_statuses2.Clear(); GC.SuppressFinalize(temp_statuses2); }
}
}
示例15: FindPlayerAlt
/// <summary>
/// Returns an alt present in the passed dictionary
/// </summary>
/// <param name="nick"></param>
public string FindPlayerAlt(string nick, Dictionary<string,Player>.KeyCollection allNames)
{
try
{
StreamReader reader = new StreamReader(".//AlternateNames.txt");
string input;
string newNick;
do
{
// Get line of alts
input = reader.ReadLine();
input = input.ToUpper();
// See if the current line of alts contains the input nick
if (input.Contains(nick.ToUpper()))
{
int index = 0;
while (true)
{
// Parse a nick
int commaLocation = input.IndexOf(",", index);
if (commaLocation == -1)
{
// Get all remaining characters for a final match attempt
newNick = input.Substring(index, input.Length - index).Trim();
if (allNames.Contains(newNick))
{
errorwriter.Write("Match found: " + newNick);
reader.Close();
return newNick;
}
break;
}
else
{
newNick = input.Substring(index, commaLocation - index);
// If the new nick is found in the collection of players, return the new nick
if (allNames.Contains(newNick))
{
errorwriter.Write("Match found: " + newNick);
reader.Close();
return newNick;
}
// Advance past the comma
index = commaLocation + 1;
}
}
}
} while (!reader.EndOfStream);
// Search failed
errorwriter.Write("No match found");
reader.Close();
return string.Empty;
}
catch (FileNotFoundException e)
{
errorwriter.Write("Could not find AlternateNames.txt (" + e.Message + ")");
return string.Empty;
}
catch (Exception e)
{
errorwriter.Write("Error reading alternate name file (" + e.Message + ")");
return string.Empty;
}
}