c# - 是否可以为 HashSet 设置泛型?

标签 c#

如果可能的话,我想做这样的事情。

internal sealed class BufferPool<T, K> : K<Buffer<T>>
{
    public BufferPool() : base() {}
    //etc...
}

然后像这样调用这个类:

BufferPool<byte, HashSet> buffers;
buffers = new BufferPool<byte, HashSet>(bufferCapacity);

BufferPool<byte, List> buffers;
buffers = new BufferPool<byte, List>(bufferCapacity);

最佳答案

您不能从类型参数继承(参见 How to inherit from a generic parameter? )。您可以做的是将类型参数限制为有用的类型和普通构造函数,并在您的类中实例化一个实例:

internal sealed class BufferPool<T, K> : IEnumerable<T> where K : IEnumerable<T>, new()
{
    K _bufferImpl = new K();

    public BufferPool() : base() { }
    //etc...
}

关于c# - 是否可以为 HashSet 设置泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899479/

相关文章:

c# console 从用户输入中获取带有索引的数组值

c# - 从其父页面调用 UserControl 方法

c# - Entity Framework 在哪里存储属性名称和它在 SQL 中选择的列之间的映射?

c#:使用 Handler(.ashx) 使用 javascript 获取帖子发送

c# - 无法在 C# 中的按钮文本中显示 "&&"

c# - 处理 (2 raise to n) -1 条件

c# - 如何使用 ASP.NET Core 从查询字符串中读取值?

c# - 如何反转括号?

c# - 如何使用 Xamarin 中的 CallDirectoryExtension 创建一个提供来电显示的应用程序?

c# - 如何在任何基于 COLOR 的语言汇编中找到给定类型的所有类型依赖项?