Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constrain interface implementations to structs?

An interface in C# can inherit another interface, e.g.

interface IFoo : IComparable { }

On the other hand, the following declaration is illegal:

interface IBar : struct { } // Invalid syntax

Is there any way an interface can be declared so that the implementing type is constrained to be a struct?

like image 684
Anders Gustafsson Avatar asked Dec 24 '22 19:12

Anders Gustafsson


1 Answers

Is there any way an interface can be declared so that the implementing type is constrained to be a struct?

No, that is currently not possible and neither is the inverse (ensuring an interface is implemented by a class).


As far as documentation goes the closest thing I was able to find was this Interfaces, Interfaces (c#), Inheritance - Interfaces. I doubt there will be anything on an official MS site simply because (in most cases) there is no documentation on non-existing features (ignoring feature requests or features in progress) and this could be considered a non-existent feature.

Closest excerpt I could find

A class or struct can implement multiple interfaces. ...

like image 79
Igor Avatar answered Dec 28 '22 07:12

Igor