Generic parameter deduction
- From: Andreas Mueller <me@xxxxxxxxxxx>
- Date: Sat, 24 Dec 2005 20:21:13 +0100
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 -- To email me directly, please remove the *NO*SPAM* parts below: *NO*SPAM*xmen40@*NO*SPAM*gmx.net .
- Follow-Ups:
- Re: Generic parameter deduction
- From: Jon Skeet [C# MVP]
- Re: Generic parameter deduction
- Prev by Date: Re: How to get html to string
- Next by Date: Re: Thread safety and events
- Previous by thread: Non rectangular form
- Next by thread: Re: Generic parameter deduction
- Index(es):
Relevant Pages
|