C#6's Improved overload resolution - clarification?

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

Among all the new features in C#6, the most mysterious feature (to me) is the "improved overload resolution".

Maybe it's because I couldn't find related info/examples/explanation about it.

The only two remaining features not discussed are support for defining a custom Add extension method to help with collection initializers, and some minor but improved overload resolution

Looking at the roslyn wiki

And so I ask:

Question:

How exactly do the Improved overload resolution comes into play in C#6? And how it is different from C#5 (Example? Documentation?)

回答1:

I believe what is meant here is the "better betterness" rules which are documented in the Roslyn github repo.

Sample code:

using System;  class Test {     static void Foo(Action action) {}     static void Foo(Func func) {}     static int Bar() { return 1; }      static void Main()     {         Foo(Bar);             } } 

Using the C# 5 compiler (e.g. in c:\Windows\Microsoft.NET\Framework\v4.0.30319\) this gives two errors:

Test.cs(11,9): error CS0121: The call is ambiguous between the following methods or properties:
     'Test.Foo(System.Action)' and 'Test.Foo(System.Func)'
Test.cs(11,13): error CS0407: 'int Test.Bar()' has the wrong return type

Using the C# 6 compiler, it compiles fine.

Likewise using exact matching for lambda expressions, this generates an ambiguous overload error with the C# 5 compiler, but not for C# 6:

using System;  class Test {     static void Foo(Func> func) {}     static void Foo(Func> func) {}      static void Main()     {         Foo(() => () => 7);     } } 


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