Can this method overload be resolved?

C

Christof Nordiek

Given the following code
Is there any way to call one of the Bar methods.

using System;

class Program
{
static void Main()
{
Foo<string, string> foo = new Foo<string,string>();
string s = "";

foo.Bar(s); // Compiler error CS0121

}
}

class Foo<T, S>
{
public void Bar(T a)
{
Console.WriteLine("Bar(T)");
}

public void Bar(S a)
{
Console.WriteLine("Bar(S)");
}
}
 
C

Christopher Ireland

Christof Nordiek wrote:
| Given the following code
| Is there any way to call one of the Bar methods.

I don't think there is, unless T and S are different types (e.g. Foo<string,
bool> foo = new Foo<string,bool>();)

--
Christopher Ireland

"I am always doing that which I can not do, in order that I may learn how to
do it."
Pablo Picasso
 
T

Tom Porterfield

Christof said:
Given the following code
Is there any way to call one of the Bar methods.

using System;

class Program
{
static void Main()
{
Foo<string, string> foo = new Foo<string,string>();
string s = "";

foo.Bar(s); // Compiler error CS0121

}
}

class Foo<T, S>
{
public void Bar(T a)
{
Console.WriteLine("Bar(T)");
}

public void Bar(S a)
{
Console.WriteLine("Bar(S)");
}
}

Per the spec, it cannot be resolved and should properly fail. See this
example in the spec:

class G2<U,V>
{
void F3(U u, V v); // Valid, but overload resolution
for
void F3(V v, U u); // G2<int,int>.F3 will fail

void F4(U u, I1<V> v); // Valid, but overload resolution for
void F4(I1<V> v, U u); // G2<I1<int>,int>.F4 will fail

void F5(U u1, I1<V> v2); // Valid overload
void F5(V v1, U u2);

void F6(ref U u); // valid overload
void F6(out V v);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top