I wouldlike to do something like this : ((I)typeof(mybject)).MyStaticFunction (with I, an interface with MyStaticFunction a static function of the interface) I know the syntax is incorrect but here is the point.
–
TotoAug 7 '09 at 9:57

9

Delphi has had the concept of Virtual Class members (aka virtual statics) since the 1990's. Since Delphi was created by Anders Hejlsberg & Co back in the 1990's, it naturally begs the question of why he never introduced it (or something similar) into C#. Yes, it then complicates matters when discussing constructors, but I'm confident an elegant solution exists. +1 to OP
–
Lee GrissomMay 17 '12 at 20:07

“virtual” and “static” are opposites! “virtual” means “determine the method to be called based on run time type information”, and “static” means “determine the method to be called solely based on compile time static analysis”

The contradiction between "static" and "virtual" is only a c# problem. If "static" were replaced by "class level", like in many other languages, no one would be blindfolded.

Too bad the choice of words made c# crippled in this respect. It is still possible to call the Type.InvokeMember method to simulate a call to a class level, virtual method. You just have to pass the method name as a string. No compile time check, no strong typing and no control that subclasses implement the method.

This is no "bad choice of words", the definition for static - “determine the method to be called solely based on compile time static analysis” as per Michael Stum's answer - is what it actually meant ever since its introduction in C. The feature request is effectively to change its meaning to "class-bound".
–
ivan_pozdeevFeb 8 at 9:33

Given all this, we now have a singleton class that writes out currency values and I can change the behavior of it. I've basically defined the behavior convention at compile time and can now change the behavior at either compile time (in the constructor) or run time, which is, I believe the effect you're trying to get. If you want inheritance of behavior, you can do that to by implementing back chaining (ie, have the new method call the previous one).

That said, I don't especially recommend the example code above. For one, it isn't thread safe and there really isn't a lot in place to keep life sane. Global dependence on this kind of structure means global instability. This is one of the many ways that changeable behavior was implemented in the dim dark days of C: structs of function pointers, and in this case a single global struct.

Guys who say that there is no sense in static virtual methods. If you don't understand how this could be possible, it does not means that it is impossible. There are languages that allow this!! Look at Delphi, for example.

In .NET, virtual method dispatch is (roughly) done by looking at the actual type of an object when the method is called at runtime, and finding the most overriding method from the class's vtable. When calling on a static class, there is no object instance to check, and so no vtable to do the lookup on.

This name was invented by an engineer in 1995 minimum 6 years after F-bounded polymorphism was mathematically formalized. bit.ly/1Ft54Ah At the time, there were no internet though so I can't blame him for not looking at that (Google was founded in 1999)
–
Mikaël MayerFeb 10 at 10:15

Wow, I didn't know that. Added this to the Wikipedia article.
–
ivan_pozdeevFeb 10 at 17:09

This is not a part of C# because in it, static means "not bound to anything at runtime" as it has ever since C (and maybe earlier). static entities are bound to the declaring type (thus are able to access its other static entities), but only at compile time.