本文整理汇总了C#中ObservableDictionary.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# ObservableDictionary.TryGetValue方法的具体用法?C# ObservableDictionary.TryGetValue怎么用?C# ObservableDictionary.TryGetValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObservableDictionary
的用法示例。
在下文中一共展示了ObservableDictionary.TryGetValue方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryGetRetrievesExistingValue
public void TryGetRetrievesExistingValue()
{
// given
var initialKvPs = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "One")
};
using (var observableDictionary = new ObservableDictionary<int, string>(initialKvPs))
{
// when
string retrievedValue;
var tryGetResult = observableDictionary.TryGetValue(1, out retrievedValue);
// then check whether all items have been accounted for
tryGetResult.Should().Be(true);
retrievedValue.Should().Be("One");
}
}
示例2: TryGetThrowsOnNullKey
public void TryGetThrowsOnNullKey()
{
// given
using (var observableDictionary = new ObservableDictionary<string, string>())
{
// when
string value;
Action retrieval = () => observableDictionary.TryGetValue((string) null, out value);
// then
retrieval
.ShouldThrow<ArgumentNullException>()
.WithMessage("Value cannot be null.\r\nParameter name: key");
observableDictionary.Count.Should().Be(0);
}
}
示例3: ObservableDictionaryTryGetValueTest
public void ObservableDictionaryTryGetValueTest()
{
ObservableDictionary<GenericParameterHelper, GenericParameterHelper> target = new ObservableDictionary<GenericParameterHelper, GenericParameterHelper>();
KeyValuePair<GenericParameterHelper, GenericParameterHelper> itemAdded = new KeyValuePair<GenericParameterHelper, GenericParameterHelper>(new GenericParameterHelper(1), new GenericParameterHelper(1));
KeyValuePair<GenericParameterHelper, GenericParameterHelper> itemNonAdded = new KeyValuePair<GenericParameterHelper, GenericParameterHelper>(new GenericParameterHelper(2), new GenericParameterHelper(2));
target.Add(itemAdded);
GenericParameterHelper expectedItemAdded, expectidItemNonAdded;
bool expectedAdded = true;
bool expectedNotAdded = false;
bool actualAdded = target.TryGetValue(new GenericParameterHelper(1), out expectedItemAdded);
bool actualNotAdded = target.TryGetValue(new GenericParameterHelper(2), out expectidItemNonAdded);
Assert.AreEqual(expectedAdded, actualAdded);
Assert.AreEqual(expectedNotAdded, actualNotAdded);
Assert.AreEqual(expectedItemAdded, itemAdded.Value);
Assert.IsNull(expectidItemNonAdded);
}