gpt4 book ai didi

c# - 嵌套通用接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 17:42:09 26 4
gpt4 key购买 nike

我有如下所示的接口(interface)模式 (C# .NET4)

interface A 
{

}

interface B
{
List<A> a;
}

interface C
{
List<B> b;
}

我是这样实现的:

public interface A 
{

}

public interface B<T> where T : A
{
List<T> a { get; set; }
}

public interface C<T> where T : B
{
List<T> b { get; set; } // << ERROR: Using the generic type 'B<T>' requires 1 type arguments
}

我不知道如何避免错误Using the generic type 'B' requires 1 type arguments

最佳答案

interface B<T>是泛型的,你需要在声明 interface C<T> 时为其提供一个正式的类型参数.换句话说,当前的问题是您没有告诉编译器接口(interface) B 接口(interface) C 从什么类型“继承”。

两个T s 不一定指同一类型。它们可以是同一类型,如

public interface C<T> where T : B<T>, A { ... }

或者它们可以是两种不同的类型:

public interface C<T, U> where T : B<U> where U : A { ... }

在第一种情况下,对类型参数的限制当然更严格。

关于c# - 嵌套通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com