[英]How can I restrict an Interface to be implemented by only a generic type
I've been in an interview recently where interviewer asked me this question- 我最近去过一次面试,面试官问我这个问题-
How to write an interface so that it restricts or enforces to be implemented by only a generic type or prevent providing implementation? 如何编写一个接口,以使其限制或强制仅由通用类型实现,或者阻止提供实现?
Could somebody please answer this question with some code sample or provide any reference of the exactly same question with sample snippets for understanding. 有人可以用一些代码示例回答这个问题,也可以通过示例代码片段提供对完全相同的问题的任何参考,以供理解。 Is it possible? 可能吗? If YES then how and If NO then why? 如果是,那么如何;如果否,为什么?
It is not possible to restrict who implements your interface. 无法限制谁实现您的界面。
The only restriction you can give is for the interface to be generic 您可以给出的唯一限制是接口是通用的
public interface IInterface<T> { }
例如, public interface IInterface<T> { }
And you can also restrict the generic T
of the interface to some type. 您还可以将接口的通用T
限制为某种类型。
public interface IInterface<T> where T : GenericConstraint { }
例如, public interface IInterface<T> where T : GenericConstraint { }
This GenericConstraint
restriction can also be the following: 此GenericConstraint
限制也可以是以下内容:
For more info on the constraints available see MS Docs - Constraints on type parameters 有关可用约束的更多信息,请参见MS Docs-类型参数约束
As @Saruman has pointed out, it is highly recommended to read: 正如@Saruman指出的那样,强烈建议阅读:
MS Docs - Generics (C# Programming Guide) MS Docs-泛型(C#编程指南)
If you use a specific class name: 如果使用特定的类名:
The type argument must be or derive from the specified base class. type参数必须是指定的基类或从指定的基类派生。
Therefore if I have the following classes: 因此,如果我有以下课程:
public class GenericConstraint { }
public class NewGenericConstraint : GenericConstraint{ }
I could provide both GenericConstraint
and NewGenericConstraint
to IInterface<T> where T : GenericConstraint
because both of those are or derive from GenericConstraint
. 我可以向IInterface<T> where T : GenericConstraint
同时提供GenericConstraint
和NewGenericConstraint
, IInterface<T> where T : GenericConstraint
因为它们都是GenericConstraint
或从GenericConstraint
派生的。
Therefore the following classes would be valid: 因此,以下类将有效:
public class Subject : IInterface<GenericConstraint> { }
public class Subject : IInterface<NewGenericConstraint> { }
public class Subject<T> : IInterface<T> where T : GenericConstraint { }
public class Subject<T> : IInterface<T> where T : NewGenericConstraint { }
The GenericConstraint
cannot be a sealed-class because if GenericConstraint
was sealed, eg: GenericConstraint
不能是密封类,因为如果GenericConstraint
是密封的,例如:
public sealed class GenericConstraint { }
You wouldn't be able to inherit from it, and the following would fail to compile: 您将无法从中继承,并且以下内容将无法编译:
public class NewGenericConstraint : GenericConstraint { }
Therefore, it would be pointless to provide a generic parameter of which constraint is a sealed class. 因此,提供约束为密封类的通用参数将毫无意义。 Therefore the compiler enforces you to constrain a generic type to a non-sealed class 因此,编译器强制您将泛型类型限制为非密封类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.