How can I use interface as a C# generic type constraint?

前端 未结 11 2197
不思量自难忘°
不思量自难忘° 2020-11-27 12:27

Is there a way to get the following function declaration?

public bool Foo() where T : interface;

ie. where T is an interface type

11条回答
  •  遥遥无期
    2020-11-27 12:54

    The closest you can do (except for your base-interface approach) is "where T : class", meaning reference-type. There is no syntax to mean "any interface".

    This ("where T : class") is used, for example, in WCF to limit clients to service contracts (interfaces).

提交回复
热议问题