C# overloading with generic constraints

孤街浪徒 提交于 2020-02-24 16:13:28

问题


Why those two methods cannot have the same name? Is it because C# compiler does not take the generic type constraints into consideration for overloading? Could it be done in future releases of C#?

public static TValue GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
            where TValue : class
{
    TValue value;
    if (dictionary.TryGetValue(key, out value))
        return value;
    return null;
}

public static TValue? GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
        where TValue : struct
{
    TValue value;
    if (dictionary.TryGetValue(key, out value))
        return value;
    return null;
}

回答1:


Absolutely correct. See section 3.6 of the C# Language Spec (version 5):

The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter of the method that occurs in the type of a formal parameter is identified not by its name, but by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the params modifier that may be specified for the right-most parameter, nor the optional type parameter constraints.

(My emphasis)

So the signature of both methods is effectively:

GetValueOrNull<T1,T2>(IDictionary<T1,T2>,T1)

And:

Overloading of methods permits a class, struct, or interface to declare multiple methods with the same name, provided their signatures are unique within that class, struct, or interface.


Could it be done in future releases of C#?

I doubt it, unless or until type inference becomes an easier problem to solve. Type inference can already take large amounts of time because the compiler usually has to perform a brute-force approach. It having to also consider overload resolution at the same time may be prohibitively expensive given current machines.



来源:https://stackoverflow.com/questions/23359262/c-sharp-overloading-with-generic-constraints

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!