Returning a default value. (C#)

后端 未结 3 1284
盖世英雄少女心
盖世英雄少女心 2020-12-03 14:42

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 parame

相关标签:
3条回答
  • 2020-12-03 14:58

    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;
        }
    
        ....
    
    }
    
    0 讨论(0)
  • 2020-12-03 15:10
    default(T)
    
    0 讨论(0)
  • 2020-12-03 15:10
    return default(int);
    
    return default(bool);
    
    return default(MyObject);
    

    so in your case you would write:

    class MyEmptyDictionary<K, V> : IDictionary<K, V>
    {
        bool IDictionary<K, V>.TryGetValue (K key, out V value)
        {
            ... get your value ...
            if (notFound) {
              value = default(V);
              return false;
            }
        }
    
    ....
    

    }

    0 讨论(0)
提交回复
热议问题