virtual static method in interfaces & generics
I want to be able to write code like this:
public T Create<T>() where T : new(string), operator +
return new T("something") + new T("something");
public interface IConverter<Tin, Tout>
static Tout Convert(Tin value);
public static Tout Convert<TConverter, Tin, Tout>(Tin value)
where TConverter : IConverter<Tin, Tout>
Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.
- The Visual Studio Team
Jon Smit commented
Besides making mathematics a lot easier, this will make it easy to force all types inheriting from a interface to be convertible to a specific data type.
Generic Constrain is what the most benefit from this feature
When the interface can tell static things then you can use it for constraint your own method and create a function for middleman operation such as Math class that can work with any kind of number if there are INumber interface which could used for implement
There are many things that need to access by static. Such as every operator overloading or Min/Max Value or any other kind of global variable
And another very important thing is the factory function for creation. Instead of making another factory class map for each class. You may just use static interface that can pass readonly value to the constructor by making each class implement their own static logic for creation and just have interface to constraint that then we have a factory that can create everything in the same pattern
Mark Seemann commented
What would the purpose of that be?
Some people mention this in Microsoft Connect but no move yet
Uhh, comment editor eated all generic parameters in < and >
see http://seermindflow.blogspot.com/2009/09/my-virtual-static-implementation.html for more details