本文整理汇总了C#中BinarySearchTree.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.TryGetValue方法的具体用法?C# BinarySearchTree.TryGetValue怎么用?C# BinarySearchTree.TryGetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.TryGetValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Simple
public void Simple()
{
var tree = new BinarySearchTree<int, string>();
var dictionary = new Dictionary<int, string>();
var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));
for (var i = 0; i < 50; i++)
{
var gen = rand.Next(2000);
while (dictionary.ContainsKey(gen))
{
gen = rand.Next(2000);
}
dictionary.Add(gen, null);
tree.Add(gen, gen.ToString());
string val;
Assert.AreEqual(tree.Count, i + 1);
tree.TryGetValue(gen, out val);
Assert.AreEqual(val, gen.ToString());
Assert.AreEqual(tree[gen], gen.ToString());
}
string val2;
tree.TryGetValue(2001, out val2);
Assert.IsNull(val2);
}
示例2: TryGetValueExample
public void TryGetValueExample()
{
// Build a simple tree.
BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>
{
new KeyValuePair<string, int>("cat", 1),
new KeyValuePair<string, int>("dog", 2),
new KeyValuePair<string, int>("canary", 3)
};
int value;
// We'll get the value for cat successfully.
Assert.IsTrue(tree.TryGetValue("cat", out value));
// And the value should be 1.
Assert.AreEqual(1, value);
// But we won't get a horse
Assert.IsFalse(tree.TryGetValue("horse", out value));
}