接口使用的新关键字

时间:2018-09-21 00:32:28

标签: c# interface new-operator

尽管我已经浏览了有关此问题的几个答案,但仍不确定是否知道这行代码在做什么:

public class SomeClass<P> : SomeInterface where P : AnotherInterface, new(){...}

new()在做什么?

1 个答案:

答案 0 :(得分:1)

虽然您通常会看到where T : Whatever[, Whatever2]是指其他接口和类,但它也可以是these constraints中的任何一个:

  • where T : struct-T必须是值类型
  • where T : class-T必须是引用类型
  • where T : unmanaged-T及其所有变量必须是值类型,以及这些值类型具有的变量...等等。
  • where T : new()-T必须具有无参数构造函数。此约束必须最后指定。
  • where T : U-T必须是为U提供的自变量或从中获得的自变量