How this code work(C# 2.0)? It shouldn't!

What makes you think that, there is no ambiguity possible, the compiler can directly deduce the best function member to call, 3 is an int parameter and there is a non-generic method F1 taking an int as argument, non-generics takes precedence over generic parameters, overload resolution solved, point. make your call g1a.F1(3L); and you will get a compiler error, as now there aint no method taking a long. change it to G1 g1a = new G1(); g1a.F1(3L); And the first method will be called. Willy
[505 byte] By [MVPUser] at [2008-2-15]
# 1


using System;
using System.Collections.Generic;
using System.Text;

namespace Generics
{
class Program
{
class G1<U>
{
public void F1(U u) { Console.WriteLine("F1_U"); return; } // Invalid overload, G<int> would have two

public void F1(int i) { Console.WriteLine("F1_int"); return; } // members with the same signature
}

static void Main()
{
G1<int> g1a = new G1<int>();

g1a.F1(3);
}
}
}



I think there is MUST be ambiguity in F1 method call. But in reality no problem - it compiles and it works. But how?

Smarty at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
I was under the impression this would generate a compiler error. I too am interested to see what Microsoft has to say.
GaryD at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 3

Hi,

Puzzled... really puzzled...
I wonder which function would govern? IMO, it would be the void F1(int i)...
But I guess it becomes a guessing game...

could you post it as a suggestion or bug?
http://lab.msdn.microsoft.com/productfeedback/

And please keep us posted...
cheers,
Paul June A. Domag

PaulDomag at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

wrote in message news:f6a2ae3f-727d-40ac-a190-8892f33ab344@discussions.microsoft.com...

This works because the design has changed. According to a newsgroup post back in early april by Rick Byers (msft):,

The behavior has indeed changed. You can expect the final version of the

C# 2.0 spec to describe the behavior you are seeing. The C# team has made

a number of changes to their internal copy of the spec (including this),

but as far as I know it has not been released publicly since June 2004.

Thanks,

Rick

If you look at the intellisense menus when trying out those overloads, you'll see that if parameters match, you'll get only 1 method whereas if you use a different type, you'll get two overloads.

This is really great that they allowed generic overloads, and while it's not quite the same as partial specialization, it's a step in the right direction. It's very possible that you might want to have special handling of a function for a particular type, and this new behavior allows that.

--Oren

Oren,

Saw Rick's post and it's blog http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx.

The latest specs (ECMA final draft published April 2005 - http://www.plumhall.com/ecma/index.html) shows a tiny sample that illustrates this new behavior, and also clearly states that non generics parameters take precedence over generics when selecting the "best method".

Thanks for sharing.

Willy

MVPUser at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
This works because the design has changed. According to a newsgroup post back in early april by Rick Byers (msft):,

The behavior has indeed changed. You can expect the final version of the
C# 2.0 spec to describe the behavior you are seeing. The C# team has made
a number of changes to their internal copy of the spec (including this),
but as far as I know it has not been released publicly since June 2004.

Thanks,
Rick

If you look at the intellisense menus when trying out those overloads, you'll see that if parameters match, you'll get only 1 method whereas if you use a different type, you'll get two overloads.

This is really great that they allowed generic overloads, and while it's not quite the same as partial specialization, it's a step in the right direction. It's very possible that you might want to have special handling of a function for a particular type, and this new behavior allows that.

--Oren

OrenNovotny at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# General...