c# - Is there any priority when resolving method overloads? -


why execute testmethod<t>(params t[] input) method instead of execute testmethod(object input).i confusing why complier execute generic method.is there priority hierarchy on .net framework ?

class testclass {     static void main()     {         testmethod("hello world");     }      static void testmethod(object input)     {         console.writeline("object");     }      static void testmethod<t>(params t[] input)     {         console.writeline("params t[]");     } } 

the comment on question contains link overload resolution specs of c# contains infos need answer question in depth. it's not easy read, though , hard find actual path of resolution in case, here happens, far can tell:

parameter arrays:

first, need @ params keyword does: shortcut developers convenience, here identical to:

static void testmethod<t>(params t[] input) 

gets translated to

static void testmethod<t>(t[] item) 

and calls translated accordingly:

testmethod("string2", "string2"); 

gets translated to:

testmethod(new string[] { "string1", "string2" ); 

resolution:

with in mind, let's see compiler does: compiler evaluates options resolve call @ runtime.

your call is:

testmethod("string");  

the first option translate call to:

testmethod(new string[] { "string" });  

this allow call generic version, while using string t, result in 'resolved method signature':

testmethod(string[] item) 

so, required conversion actual required argument type is

string[] string[] 

option b interpret parameter "string" in our eyes, string. feasible call non-generic version.

string object 

the conversions evaluated according section 7.4.3.4 of c# spec , avoid downgrading of string object, first option chosen.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -