返回的默认值。 (C#) [英] Returning a default value. (C#)

查看:169
本文介绍了返回的默认值。 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造我自己的字典,我有麻烦实施 TryGetValue 功能。如果没有找到钥匙,我没有什么要分配给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#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆