考虑以下课程,
class Foo
{
public Foo(int count)
{
/* .. */
}
public Foo(int count)
{
/* .. */
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码无效,不会编译.现在考虑以下代码,
class Foo<T>
{
public Foo(int count)
{
/* .. */
}
public Foo(T t)
{
/* .. */
}
}
static void Main(string[] args)
{
Foo<int> foo = new Foo<int>(1);
}
Run Code Online (Sandbox Code Playgroud)
以上代码有效且编译良好.它调用Foo(int count).
我的问题是,如果第一个无效,第二个如何有效?我知道类Foo <T>是有效的,因为T和int是不同的类型.但是当它像Foo <int> foo = new Foo <int>(1)一样使用时,T会得到整数类型,并且两个构造函数都具有相同的签名吗?为什么编译器不显示错误而不是选择执行重载?