c# - Generic and non-generic method overload rules with type inference -
given following 2 overloaded method signatures:
public b dosomething<a,b>(a objone, b objtwo) public object dosomething(object objone, type objtype);
i expect calling call second signature, not:
var obj = new sometype(); var type = typeof(someothertype); dosomething(obj, type);
it seems me looking @ spec, second overload more applicable (has 1 exact type , lesser arity). but, not called. instead, first overload called type object
, b type type
. why , there way call method aside either renaming method or using named parameters?
edit:
here parts of spec i'm referring in 7.5.3.2:
in case parameter type sequences {p1, p2, …, pn} , {q1, q2, …, qn} equivalent (i.e. each pi has identity conversion corresponding qi), following tie-breaking rules applied, in order, determine better function member.
- if mp non-generic method , mq generic method, mp better mq.
- ...
- otherwise, if mp has more specific parameter types mq, mp better mq. let {r1, r2, …, rn} , {s1, s2, …, sn} represent uninstantiated , unexpanded parameter types of mp , mq. mp’s parameter types more specific mq’s if, each parameter, rx not less specific sx, and, @ least 1 parameter, rx more specific sx:
- a type parameter less specific non-type parameter.
- ...
after edit here methods compiler choose between
dosomething<sometype,type>(sometype o, type t) dosomething(object o,type t);
as specification says picking better function member:
given argument list set of argument expressions { e1, e2, ..., en } , 2 applicable function members mp , mq parameter types { p1, p2, ..., pn } , { q1, q2, ..., qn }, mp defined better function member mq if
• @ least 1 argument, conversion ex px better conversion ex qx.
generic method have better conversion sometype
sometype
non-generic's method conversion sometype
object
. if non-generic method defined
dosomething(sometype o, type t);
then there not arguments better conversion, , you'll fall case:
in case parameter type sequences {p1, p2, …, pn} , {q1, q2, …, qn} equivalent
where first rule pick non-generic method:
• if mp non-generic method , mq generic method, mp better mq.
note: before edited question, there first argument of type object
, , generic method was
dosomething<object,type>(object o, type t)
and again, parameter sequences equivalent in case, , non-generic method picked.
Comments
Post a Comment