You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, static items aren't allowed in interfaces. This is not a technical limitation, rather a semantic one that it doesn't make sense to have it. However, this was a decision that was made before Generics.
There are many uses case that it'd make immensely better, if present, both with and without generics. For example, there's really no way currently to allow generics to work with operator overloads.
This is a surprisingly simple code, that just won't work today. If you have IRectangles, that take short, int, float, decimal, for example, there simply is no other way than to write very redundant code for each class over and over again.
However, allowing static in interfaces would allow this very naturally.
The definition of the IOperatorPlus would look something like this:
Statics in interfaces would also be very useful in cases, where you can write code that depends on having contractually defined static methods.
publicclassX:IStaticFactory{publicstatic IX Create(){return X(new DefaultY());}publicX(SomeDependencyIY){}}interfaceIStaticFactory{static IX Create();}
This is a pattern that would allow a high-performance constructor, for many different classes that implement the IStaticFactory, without relying on Activator which uses reflection. This also includes the generic construction new T().
Static entities in interface allow scenarios such as this to be enabled, instead of jumping through hoops.
The text was updated successfully, but these errors were encountered:
While the CLR does allow for interfaces to contain static members it has no concept of virtual static members. There is no static or type-wide vtable through which to virtually dispatch a method call. As such it's not possible to define required static members of an interface, only completely concrete members.
@HaloFour,
Scenario 1 - the resolution for all this can be done in compile time. So, v-table should be irrelevant.
Scenario 2 - I think you're spot on there. That's a challenging scenario on how to implement.
However, sorry about the duplication. Not sure how I missed in the search. Closing this.
Currently, static items aren't allowed in interfaces. This is not a technical limitation, rather a semantic one that it doesn't make sense to have it. However, this was a decision that was made before Generics.
There are many uses case that it'd make immensely better, if present, both with and without generics. For example, there's really no way currently to allow generics to work with operator overloads.
Example: Use Case 1
This is a surprisingly simple code, that just won't work today. If you have
IRectangle
s, that take short, int, float, decimal, for example, there simply is no other way than to write very redundant code for each class over and over again.However, allowing static in interfaces would allow this very naturally.
The definition of the
IOperatorPlus
would look something like this:Example: Use Case 2
Statics in interfaces would also be very useful in cases, where you can write code that depends on having contractually defined static methods.
This is a pattern that would allow a high-performance constructor, for many different classes that implement the
IStaticFactory
, without relying onActivator
which uses reflection. This also includes the generic constructionnew T()
.Static entities in interface allow scenarios such as this to be enabled, instead of jumping through hoops.
The text was updated successfully, but these errors were encountered: