本文整理匯總了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));
}