c# - C#6's Improved overload resolution - clarification? -
among new features in c#6, mysterious feature (to me) "improved overload resolution".
maybe it's because couldn't find related info/examples/explanation it.
the 2 remaining features not discussed support defining custom add extension method collection initializers, and minor improved overload resolution
looking @ roslyn wiki
there number of small improvements overload resolution, result in more things working way you’d expect them to. improvements relate “betterness” – way compiler decides of 2 overloads better given argument.
and ask:
question:
how improved overload resolution comes play in c#6? , how different c#5 (example? documentation?)
i believe meant here "better betterness" rules documented in roslyn github repo.
sample code:
using system; class test { static void foo(action action) {} static void foo(func<int> func) {} static int bar() { return 1; } static void main() { foo(bar); } }
using c# 5 compiler (e.g. in c:\windows\microsoft.net\framework\v4.0.30319\
) gives 2 errors:
test.cs(11,9): error cs0121: call ambiguous between following methods or properties:
'test.foo(system.action)' , 'test.foo(system.func)'
test.cs(11,13): error cs0407: 'int test.bar()' has wrong return type
using c# 6 compiler, compiles fine.
likewise using exact matching lambda expressions, generates ambiguous overload error c# 5 compiler, not c# 6:
using system; class test { static void foo(func<func<long>> func) {} static void foo(func<func<int>> func) {} static void main() { foo(() => () => 7); } }
Comments
Post a Comment