c# - Ambiguous method call with Action<T> parameter overload -


i encountered unexpected compiler behaviour when calling overloaded method different action<t> variations.

let's have class test , i'm creating instance in calltest constructor.

public class test {     public test(action<long> arg)     {      }      public test(action<decimal> arg)     {      } }  public class calltest {     public calltest()     {         test t = new test(testdecimal);     }      public void testdecimal(decimal arg)     {      }      public void testlong(long arg)     {      }     } 

when calling test constructor either testdecimal or testlong parameter i'm receiving following error:

the call ambiguous between following methods or properties: 'test(system.action<long>)' , 'test(system.action<decimal>)'

my guess there's implicit conversion going on between long , decimal, have other idea have done wrong? there workaround?

when pass testdecimal or testlong parameter you're in fact passing method group (after all, there more 1 testdecimal method - have been overloaded). in both cases implicit conversion occurs - method group particular delegate type. both methods applicable candidates (section 7.4.2). applicable candidates overload resolution algorithm tries find best candidate. however, rules of comparing conversions when matching parameter lists state, if both candidates implicit conversion occurs neither of them better:

section 7.4.2.3:

[...]

otherwise, neither conversion better.

that's why in case there ambiguity.


the workaround of course first cast parameter explicitly:

new test(new action<decimal>(testdecimal)) 

this way 1 case there no need implicit conversion during overload resolution (as after cast action<t> type match exactly), , other have converted (action<long> action<decimal>), , section mentioned above states that:

[...]

if s t1, c1 better conversion.

if s t2, c2 better conversion.

[...]


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -