-1

I was browsing through Github when I noticed an interface in C# that had the following:

public interface IAction : IPrototype<IAction>

I have never seen this before. So I was curious what this exactly means or what it does and if this is applicable to things other than interfaces?

Is this a C# specific syntax for a specific behavior? (Is it useful in other OOP languages)

Sorry, if this is a really noob question but, I don't even know what this is called so I couldn't figure out exactly how to simply google it :P

Dtb49
  • 1,211
  • 2
  • 19
  • 48
  • [Interface with generic parameter vs Interface with generic methods](https://stackoverflow.com/q/17596186/205233) might hold some clues. – Filburt Aug 13 '20 at 20:23
  • Which part are you asking about: the generic type parameter (``) or that `IAction` appears on both sides of the `:`? It sounds like your knowledge of C# includes interfaces but it's not clear if it's generics that are unknown to you or this specific usage of them. – Lance U. Matthews Aug 13 '20 at 20:27

3 Answers3

4

That means that IAction inherits a generic IPrototype<T> interface where the type is IAction. IPrototype<T> may define a member to consume or produce a T, in this case it would be a IAction.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • On top of this, I would like to refer to the Prototype pattern and the Curiously Recursive Template Pattern. I believe that was the intention behind the code. Hopefully this complements your explanation of the language constructs. – Jason Aug 13 '20 at 21:26
1

It's an interface that inherits from a generic interface.

0

To me this looks like an interface that enforces the prototype pattern by implementing the curiously recursive template pattern. More info can be found here https://zpbappi.com/curiously-recurring-template-pattern-in-csharp/.

Essentially, you are able to define an interface that contains methods that return or consume strongly typed instances of the implementor. Without the pattern the best you could do is return an instance of the base interface.

The prototype pattern is a pattern that allows for a class to be cloned, so I guess that the IPrototype interface has a method called Clone that returns T. In this case it would return IAction.

public interface IPrototype<T> where T : IPrototype<T>
{
    // enforces a clone method returning the sub class
    T Clone();
}
Jason
  • 1,505
  • 5
  • 9