问题描述
我创造我自己的字典,我有麻烦实施功能。如果没有找到钥匙,我没有什么要分配给OUT参数,所以我把它原样。这将导致以下错误:输出参数价值必须分配到控制离开当前方法之前,
所以,基本上,我需要一种方式来获得默认值(0,虚假或nullptr根据类型)。我的code是类似以下内容:
类MyEmptyDictionary< K,V> :IDictionary的< K,V>
{
布尔的IDictionary< K,V> .TryGetValue(K键,走出V值)
{
返回false;
} ....}
您正在寻找关键字。
例如,在你给的例子,你想是这样的:
类MyEmptyDictionary< K,V> :IDictionary的< K,V>
{
布尔的IDictionary< K,V> .TryGetValue(K键,走出V值)
{
值=默认值(V);
返回false;
} ....}
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function. When the key isn't found, I don't have anything to assign to the out parameter, so I leave it as is. This results in the following error: "The out parameter 'value' must be assigned to before control leaves the current method"
So, basically, I need a way to get the default value (0, false or nullptr depending on type). My code is similar to the following:
class MyEmptyDictionary<K, V> : IDictionary<K, V>
{
bool IDictionary<K, V>.TryGetValue (K key, out V value)
{
return false;
}
....
}
You are looking for the default
keyword.
For example, in the example you gave, you want something like:
class MyEmptyDictionary<K, V> : IDictionary<K, V>
{
bool IDictionary<K, V>.TryGetValue (K key, out V value)
{
value = default(V);
return false;
}
....
}
这篇关于返回的默认值。 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!