A
Andreas Mueller
Hi all,
I have a method where the compiler is unable to deduce the generic
arguments. Now I'm asking myself if this a bug, or if not, why it isn't
working. The method in question is Foo3:
using System;
using System.Collections.Generic;
namespace ConsoleApplication10
{
class Program
{
class A<T> { }
class B<T> { }
static A<T> Foo1<T, AA>(AA a, T t) where AA : A<T>
{
return default(A<T>);
}
static A<T> Foo2<T, AA>(AA a, B<T> t) where AA : A<T>
{
return default(A<T>);
}
static A<T> Foo3<T, AA>(AA a) where AA : A<T>
{
return default(A<T>);
}
static void Main(string[] args)
{
A<int> ai = new A<int>();
ai = Foo1(ai, 42);
ai = Foo2(ai, new B<int>());
// ... gives CS0411, can't deduce generic parameter. Why?
ai = Foo3(ai);
}
}
}
The compiler can deduce T for Foo1, as it is explicitly defined as a
parameter. It also can infer T & AA in Foo2, although T is only there
implicitly as an generic argument.
But if it can handle Foo2, why not Foo3?
TIA,
Andy
I have a method where the compiler is unable to deduce the generic
arguments. Now I'm asking myself if this a bug, or if not, why it isn't
working. The method in question is Foo3:
using System;
using System.Collections.Generic;
namespace ConsoleApplication10
{
class Program
{
class A<T> { }
class B<T> { }
static A<T> Foo1<T, AA>(AA a, T t) where AA : A<T>
{
return default(A<T>);
}
static A<T> Foo2<T, AA>(AA a, B<T> t) where AA : A<T>
{
return default(A<T>);
}
static A<T> Foo3<T, AA>(AA a) where AA : A<T>
{
return default(A<T>);
}
static void Main(string[] args)
{
A<int> ai = new A<int>();
ai = Foo1(ai, 42);
ai = Foo2(ai, new B<int>());
// ... gives CS0411, can't deduce generic parameter. Why?
ai = Foo3(ai);
}
}
}
The compiler can deduce T for Foo1, as it is explicitly defined as a
parameter. It also can infer T & AA in Foo2, although T is only there
implicitly as an generic argument.
But if it can handle Foo2, why not Foo3?
TIA,
Andy