【发布时间】:2019-03-03 17:46:51
【问题描述】:
我有这个带有两个构造函数的通用类Foo<>。其中之一使用params 关键字接受可变数量的参数。另一个非泛型类Bar 派生自Foo<>。问题是Bar 不接受使用具有params 的构造函数进行实例化。
这是一个示例:
using System;
namespace ConsoleApp1
{
class Foo<T> where T : IComparable<T>
{
private Foo() { }
public Foo(params T[] list) : this() { }
}
class Bar : Foo<int> { }
class Program
{
static void Main(string[] args)
{
Foo<int> foo = new Foo<int>(1, 2); // it compiles
Bar bar = new Bar(1, 2); // but CS1729 here!
}
}
}
它给出了一个编译器错误:
Error CS1729: 'Bar' 不包含带 2 个参数的构造函数
我在这里错过了什么?
【问题讨论】:
标签: c# generics constructor