本文整理汇总了C#中ReadOnlyDictionary.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# ReadOnlyDictionary.TryGetValue方法的具体用法?C# ReadOnlyDictionary.TryGetValue怎么用?C# ReadOnlyDictionary.TryGetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReadOnlyDictionary
的用法示例。
在下文中一共展示了ReadOnlyDictionary.TryGetValue方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadOnlyDictionaryWithStuffRetrievedTest
public void ReadOnlyDictionaryWithStuffRetrievedTest()
{
var dictionary = new Dictionary<string, Tuple<string, Type, TimeSpan>>();
var input = new Tuple<string, Type, TimeSpan>("John", typeof(string), new TimeSpan(0, 1, 0));
dictionary.Add("John", input);
ReadOnlyDictionary<string, Tuple<string, Type, TimeSpan>> target = new ReadOnlyDictionary<string, Tuple<string, Type, TimeSpan>>(dictionary);
Assert.IsInstanceOfType(target, typeof(ReadOnlyDictionary<string, Tuple<string, Type, TimeSpan>>));
var actual = new Tuple<string, Type, TimeSpan>("", typeof(string), new TimeSpan());
target.TryGetValue("John", out actual);
Assert.AreEqual(input, actual);
}
示例2: PopulateRuntimeTypes
private static void PopulateRuntimeTypes(ReadOnlyDictionary<string, TypeDefinitionAst> parseTimeTypes, ReadOnlyDictionary<string, TypeDefinitionAst> runtimeTypes)
{
foreach (KeyValuePair<string, TypeDefinitionAst> parseTypePair in parseTimeTypes)
{
// We only need to populate types, if ASTs are not reused. Otherwise it's already populated.
if (parseTypePair.Value.Type == null)
{
TypeDefinitionAst typeDefinitionAst;
if (!runtimeTypes.TryGetValue(parseTypePair.Key, out typeDefinitionAst))
{
throw InterpreterError.NewInterpreterException(parseTypePair.Value, typeof(RuntimeException),
parseTypePair.Value.Extent,
"TypeNotFound", ParserStrings.TypeNotFound, parseTypePair.Value.Name);
}
parseTypePair.Value.Type = typeDefinitionAst.Type;
}
}
}
示例3: TestReadOperations
public void TestReadOperations()
{
var dic = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>() {
{"key1", "val1"},
{"key2", "val2"},
{"key3", "val3"},
});
Assert.AreEqual(3, dic.Count);
Assert.AreEqual("val1", dic["key1"]);
Assert.IsTrue(dic.Contains(new KeyValuePair<string, string>("key2", "val2")));
Assert.IsTrue(dic.ContainsKey("key3"));
foreach (var pair in dic) {
}
foreach (var key in dic.Keys) {
}
foreach (var val in dic.Values) {
}
var pairs = new KeyValuePair<string, string>[3];
dic.CopyTo(pairs, 0);
string outval = null;
Assert.IsTrue(dic.TryGetValue("key1", out outval));
Assert.AreEqual(outval, "val1");
}
示例4: ReadOnlyDictionary_Unit_TryGetValue_Optimal
public void ReadOnlyDictionary_Unit_TryGetValue_Optimal()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
String key = dictionary.Keys.First();
String value;
Boolean actual = target.TryGetValue(key, out value);
Assert.AreEqual(true, actual);
Assert.AreEqual(dictionary[key], value);
}
示例5: ReadOnlyDictionary_Unit_TryGetValue_KeyIsNull
public void ReadOnlyDictionary_Unit_TryGetValue_KeyIsNull()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
String key = null;
String value;
target.TryGetValue(key, out value);
}
示例6: ReadOnlyDictionary_Unit_TryGetValue_KeyDoesNotExist
public void ReadOnlyDictionary_Unit_TryGetValue_KeyDoesNotExist()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
String key = "MyKey";
String value;
Boolean actual = target.TryGetValue(key, out value);
Assert.AreEqual(false, actual);
Assert.IsNull(value);
}