IDictionary的扩展方法:方法的类型参数无法从用法中推断出来

发布于 2024-08-08 23:13:40 字数 644 浏览 11 评论 0原文

为了清理大量重复的代码,我尝试实现下面的扩展方法:

    public static void AddIfNotPresent(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
    {
        if (!dictionary.ContainsKey(key))
        {
            dictionary.Add(key, value);
        }
    }

    public static void Test()
    {
        IDictionary<string, string> test = new Dictionary<string, string>();
        test.AddIfNotPresent("hi", "mom");
    }

在扩展方法调用期间导致编译器错误:

方法 'Util.Test.AddIfNotPresent(this System.Collections.Generic) 的类型参数.IDictionary 字典、TKey 键、TValue 值)' 无法从用法中推断出来。尝试显式指定类型参数。

关于这个主题的任何说明将不胜感激!

In an attempt to clean up a lot of repeated code, I tried implementing the extension method below:

    public static void AddIfNotPresent(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
    {
        if (!dictionary.ContainsKey(key))
        {
            dictionary.Add(key, value);
        }
    }

    public static void Test()
    {
        IDictionary<string, string> test = new Dictionary<string, string>();
        test.AddIfNotPresent("hi", "mom");
    }

Results in a compiler error during the extension method call of:

The type arguments for method 'Util.Test.AddIfNotPresent(this System.Collections.Generic.IDictionary dictionary, TKey key, TValue value)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Any light on this subject would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-08-15 23:13:40

您的扩展方法不是通用的,但应该是通用的,因为扩展方法必须在非通用顶级类中定义。这是我将其设为通用方法后的相同代码:

// Note the type parameters after the method name
public static void AddIfNotPresent<TKey, TValue>
    (this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
    if (!dictionary.ContainsKey(key))
    {
        dictionary.Add(key, value);
    }
}

但是,尝试编译您实际发布的代码会给出与您指定的错误消息不同的错误消息。这表明您尚未发布真实代码......因此上述内容可能无法解决问题。但是,您发布的经过上述更改的代码工作正常。

Your extension method isn't generic, but should be, as extension methods must be defined in non-generic top level classes. Here's the same code after I've made it a generic method:

// Note the type parameters after the method name
public static void AddIfNotPresent<TKey, TValue>
    (this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
    if (!dictionary.ContainsKey(key))
    {
        dictionary.Add(key, value);
    }
}

However, trying to compile the code you actually posted gives a different error message from the one you specified. That suggests that you haven't posted the real code... and so the above may not fix things anyway. However, the code you posted with the above change works fine.

疯狂的代价 2024-08-15 23:13:40

简单的用这个就可以完成吗?

dictionary[key] = value;

如果键不存在,它会添加键/值对;如果存在,它会更新值。请参阅字典.Item

Can't this be done simply with this?

dictionary[key] = value;

It adds the key/value pair if the key doesn't exist, or updates the value if it does. See Dictionary<TKey,TValue>.Item.

顾忌 2024-08-15 23:13:40

试试这个:

public static void AddIfNotPresent<TKey, TValue>
       (this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)    
{       
    if (!dictionary.ContainsKey(key)) dictionary.Add(key, value);
}   

public static void Test()    
{        
     IDictionary<string, string> test = new Dictionary<string, string>(); 
     test.AddIfNotPresent("hi", "mom");    
}

Try this:

public static void AddIfNotPresent<TKey, TValue>
       (this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)    
{       
    if (!dictionary.ContainsKey(key)) dictionary.Add(key, value);
}   

public static void Test()    
{        
     IDictionary<string, string> test = new Dictionary<string, string>(); 
     test.AddIfNotPresent("hi", "mom");    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文