加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 不可能的递归泛型类定义?

发布时间:2020-12-15 04:00:32 所属栏目:百科 来源:网络整理
导读:挑战: 请创建以下类的实例(使用任何类型为T): class FooT where T : FooT{} 你可以使用任何你喜欢的技术;简单的“新MyClass …”,使用反射,黑客MSIL,无论如何. 解决方法 static class Program { static void Main() { FooBar foo = new FooBar(); }}class
挑战:
请创建以下类的实例(使用任何类型为T):
class Foo<T>
    where T : Foo<T>
{
}

你可以使用任何你喜欢的技术;简单的“新MyClass …”,使用反射,黑客MSIL,无论如何.

解决方法

static class Program {
    static void Main() {
        Foo<Bar> foo = new Foo<Bar>();
    }
}
class Foo<T> where T : Foo<T> {}
class Bar : Foo<Bar> {}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读