24 Jun 2015 @ 7:06 AM 

This is part of a series of posts covering new C# 6 features. Currently there are posts covering the following:
String Interpolation
Expression-bodied Members
Improved Overload resolution
The Null Conditional operator
Auto-Property Initializers

I previously posted about the new Expression-bodied Members and String Interpolation features of C# 6.0. There are quite a few features to cover, but today I think I’ll cover one that perhaps get a bit less coverage overall. This feature is listed as “Improved Overload resolution”.

So what is it?

While I’m perhaps not aware of the full breadth of the revisions made to overload resolution, one of the definite improvements was that C# 6.0 will now take into account the return type of method-group arguments. It is not really immediately obvious what this changes, so an example really works best. Take the following code:

In Visual Studio 2015, this code fails to compile. The compiler issues an Error- “The call is ambiguous between the following methods or properties: ‘testoverloadresolution.TakesItem(System.Action)’ and ‘testoverloadresolution.TakesItem(System.Func<int>)’ There is also an error that somefunction has the wrong return type, and that the void cannot be implicitly converted to int. In C# 6, however, this compiles without issue.

Basically, in the C# 5.0 case, when we called the TakesItem function, the compiler would try to find the appropriate method to match the usage- however, it wasn’t taking the return type of the somefunction argument into account when matching the signature. As a result, Action and Func become indistinguishable to the compiler. C# 6 however how includes return type as part of function overload resolution, which allows it to unambiguously find the TakesItem(Func) matches the usage.

Realistically, it will prevent these sorts of errors and reduce the need to make a “fake” lambda in order to make return type be considered. A small revision but one I think should not be overshadowed too much by some of the more heavyweight and easier to demonstrate improvements such as those that I’ve already discussed.

Have something to say about this post? Comment!

Posted By: BC_Programming
Last Edit: 11 Jul 2015 @ 06:11 PM

EmailPermalink
Tags
Categories: .NET, C#, Programming


 

Responses to this post » (None)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Tags
Comment Meta:
RSS Feed for comments

 Last 50 Posts
 Back
Change Theme...
  • Users » 43332
  • Posts/Pages » 362
  • Comments » 106
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

PP



    No Child Pages.

Windows optimization tips



    No Child Pages.

Software Picks



    No Child Pages.

BC’s Todo List



    No Child Pages.