I don't have access to the C# 4.0 preview yet. But I am curious, what does the C# 4.0 runtime do when invoking an overloaded method in the following case. Does it resolve to the generic overload ... or the specialized overload.
public class Foo<T>
{
protected string BarImpl( T value ) { return "Bar(T) says: " + value.ToString(); }
protected string BarImpl( int value ) { return "Bar(int) says: " + value.ToString(); }
public string Bar( T value )
{
dynamic foo = this;
return foo.BarImpl( value );
}
}
public static void Main( string args[] )
{
var f = new Foo<int>();
Console.WriteLine( f.Bar( 0 ) );
}
The process of selecting the most appropriate overloaded function or operator is called overload resolution. Suppose that f is an overloaded function name. When you call the overloaded function f() , the compiler creates a set of candidate functions.
Function overloading is a feature of a programming language that allows one to have many functions with same name but with different signatures. This feature is present in most of the Object Oriented Languages such as C++ and Java.
Function overloading is a feature of Object Oriented programming languages like Java and C++. As we know, C is not an Object Oriented programming language. Therefore, C does not support function overloading.
There are two ways to resolve this ambiguity: Typecast char to float. Remove either one of the ambiguity generating functions float or double and add overloaded function with an int type parameter.
In general, my understanding that the result should (whenever possible) be the same as the result would be if you compiled the same code with just the dynamic expressions replaced by expressions of the type that the dynamic value has at execution time. (Statically-known types are preserved in the call site info.)
In this particular case, having just your code with .NET 4.0b1, the result is:
Bar(int) says: 0
However, having looked at this again (and checked which bit is actually dynamic) I'm slightly confused. I think it's one of those situations where I'd have to look very carefully at the spec to understand what the correct behaviour is. Unfortunately I don't know when the C# 4.0 spec will be available.
It's a tricky one to reason about, and I suspect the key part is whether at execution time the binder is able to work out that the value is of type T
for the same T
as the receiver, rather than type int
. Because the receiver is dynamic in this case, the compiler doesn't do any overload resolution at all. Hmm. Tricky one, definitely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With