【问题标题】:Generic class and interface in c# [duplicate]c#中的泛型类和接口[重复]
【发布时间】:2014-08-22 16:47:30
【问题描述】:

如何在 C# 中做到这一点?

  public class SomeClass<T extends  SomeInterface>{}

这是T的泛型类,T必须实现接口SomeInterface

【问题讨论】:

    标签: c# .net generics interface generic-programming


    【解决方案1】:

    你需要使用where约束clause

    public class SomeClass<T>
       where T : SomeInterface
    {}
    

    【讨论】:

      【解决方案2】:

      有类型约束:

      public class SomeClass<T> where T : SomeInterface
      

      见:http://msdn.microsoft.com/en-us/library/d5x73970.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-19
        • 1970-01-01
        • 1970-01-01
        • 2019-06-04
        • 2013-03-16
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        相关资源
        最近更新 更多