c#重载具有不同参数约束的泛型函数

c#重载具有不同参数约束的泛型函数,c#,generics,overloading,C#,Generics,Overloading,我有以下职能: public static V AddIfNotPresent<K, V>( this Dictionary<K, V> store, K key ) where V : new() public static V AddIfNotPresent<K, V>( this Dictionary<K, V> store, K key ) public static V AddIfNotPresent(此字典存储,K键),其中V:ne

我有以下职能:

public static V AddIfNotPresent<K, V>( this Dictionary<K, V> store, K key ) where V : new()

public static V AddIfNotPresent<K, V>( this Dictionary<K, V> store, K key )
public static V AddIfNotPresent(此字典存储,K键),其中V:new()
公共静态V AddIfNotPresent(此字典存储,K键)
首先。。。是否有可能以这种方式使功能过载

如果无法实现重载,我是否可以更具体地使用我的函数和实现,例如:

public static string Foo<K, string>( this Dictionary<K, string> store, K key )
公共静态字符串Foo(此字典存储,K键)

作为一个小历史,我有一个带有字符串值的字典,我希望字典有一个一致的“Foo”扩展,允许我添加新的()对象或空字符串(视情况而定)。

不,不能按类型参数约束重载。但是,可以通过类型参数的数量来重载:

public static string Foo<TKey>(this Dictionary<TKey, string> store, TKey key)
公共静态字符串Foo(此字典存储,TKey)
(请注意,
string
不在Foo的泛型类型参数列表中;它本身不是一个类型参数,它只是用作
Dictionary
的类型参数)

Foo
是不必要的,因为它已经是一个字符串,您不需要第二个泛型参数。