c# – 为什么是通用函数成员选择非通用函数成员?

c# – 为什么是通用函数成员选择非通用函数成员?,第1张

概述public enum EnumTest{ EnumEntry}public class TestClass{ public string FunctionMember(string s, EnumTest t = EnumTest.EnumEntry) { return "Normal"; } public string Fun
public enum EnumTest{    EnumEntry}public class TestClass{    public string FunctionMember(string s,EnumTest t = EnumTest.EnumEntry)    {        return "normal";    }    public string FunctionMember<T>(T t)    {        return "Generic";    }}class Program{    static voID Main(string[] args)    {        TestClass t = new TestClass();        Console.Writeline(t.FunctionMember("a"));    }}

这将打印“通用”.删除,枚举t = EnumTest.EnumEntry使其打印“正常”.

而标准似乎很清楚,从14.4.2.2更好的功能成员第一个鉴别器应用是:

>如果MP和MQ之一是非通用的,而另一个是通用的,则非泛型更好.

我错过了一些或编译错误?

解决方法 你错过了一些东西这就是以下几点:

您使用一个参数调用该方法.只有一个方法有一个参数,一个是泛型​​的.那就是选择的那个.

只有当它没有找到匹配的方法时,才会看到具有可选参数的其他方法.

参考文献:

> C# 4.0 Specification,最后一段在21.4:

As a tIE breaker rule,a function member for which all arguments where explicitly given is better than one for which default values were supplIEd in lIEu of explicit arguments.

> MSDN,标题“超载分辨率”,最后一个项目符号:

If two candIDates are judged to be equally good,preference goes to a candIDate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candIDates that have fewer parameters.

> The C# Language Specification,“7.5.3.2更好的功能成员”一章:

Parameter Lists for each of the candIDate function members are constructed in the following way:

The expanded form is used if the function member was applicable only in the expanded form. Optional parameters with no corresponding arguments are removed from the parameter List

它继续这样:

Given an argument List A with a set of argument Expressions { E1,E2,…,EN } and two applicable function members MP and MQ with parameter types { P1,P2,PN } and { Q1,Q2,QN } […]

在这一点上,具有可选参数的方法已经不在游戏中. N为1,但该方法有两个参数.

总结

以上是内存溢出为你收集整理的c# – 为什么是通用函数成员选择非通用函数成员?全部内容,希望文章能够帮你解决c# – 为什么是通用函数成员选择非通用函数成员?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1260427.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存