C# Generic overloaded method dispatching ambiguous?

It follows the rules in section 7.5.3.2 of the C# 4 specification ("Better function member") First (well, after seeing that both methods are applicable ) we need to check the conversions from argument types to parameter types. In this case it's reasonably simple because there's only one argument. Neither conversion of argument type to parameter type is "better" because both are converting from ConcreteA to IfaceA It therefore moves on to the next set of criteria, including this: Otherwise, if MP has more specific parameter types than MQ, then MP is better than MQ.

Let {R1, R2, …, RN} and {S1, S2, …, SN} represent the uninstantiated and unexpanded parameter types of MP and MQ. MP’s parameter types are more specific than MQ’s if, for each parameter, RX is not less specific than SX, and, for at least one parameter, RX is more specific than SX:specific than SX: A type parameter is less specific than a non-type parameter So even though the conversion is equally good, the overload using IfaceA directly (rather than via delegates) is deemed "better" because a parameter of type IfaceA is more specific than a parameter of type T There's no way of getting the compiler to warn on this behaviour - it's just normal overload resolution.

It follows the rules in section 7.5.3.2 of the C# 4 specification ("Better function member"). First (well, after seeing that both methods are applicable) we need to check the conversions from argument types to parameter types. In this case it's reasonably simple because there's only one argument.

Neither conversion of argument type to parameter type is "better" because both are converting from ConcreteA to IfaceA. It therefore moves on to the next set of criteria, including this: Otherwise, if MP has more specific parameter types than MQ, then MP is better than MQ. Let {R1, R2, …, RN} and {S1, S2, …, SN} represent the uninstantiated and unexpanded parameter types of MP and MQ.

MP’s parameter types are more specific than MQ’s if, for each parameter, RX is not less specific than SX, and, for at least one parameter, RX is more specific than SX:specific than SX: A type parameter is less specific than a non-type parameter. ... So even though the conversion is equally good, the overload using IfaceA directly (rather than via delegates) is deemed "better" because a parameter of type IfaceA is more specific than a parameter of type T. There's no way of getting the compiler to warn on this behaviour - it's just normal overload resolution.

Ok thanks a million. I guess that means I will read that specification. I am just a Java convert and have to use C# for this project.

Although in fact I think its more useful, but any feature I feel adds also some edge cases more ... – Sebastian Godelet May 18 '10 at 7:34.

Because the compiler chooses the most specific first. What happens if you call like so: void code() { var concreteB = new ConcreteB(); IfaceA x = concreteB. Add(new ConcreteA()); }.

– Sebastian Godelet May 18 '10 at 7:16 @sebgod: you can use the #warning directive to issue a compiler warning. – Tomas Lycken May 18 '10 at 7:21 The code will not compile if I choose IfaceA x = concreteB. Add(new ConcreteA()); : cannot implicitly convert between IFaceA and void – Sebastian Godelet May 18 '10 at 7:21 @sebgod: interesting.

Perhaps one for the Skeet... – Mitch Wheat May 18 '10 at 7:24 it looks like the compiler kind of does only consider the interface based method and does not check the generic overload, btw, what does "one for the Skeet" mean? – Sebastian Godelet May 18 '10 at 7:26.

This somewhat reminds me of the "Type inference a-go-go" in Jon Skeet's BrainTeaser. If you do not want to trust the compiler, you might want to force its choice by calling Add(new ConcreteA()).

Thats what my teacher always told me, never trust your compiler – Sebastian Godelet May 18 '10 at 7:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions