Why is static virtual impossible? Is C# dependent or just don't have any sense in the OO world?
I know the concept has already been underlined but I did not find a simple answer to the previous question.
Why is static virtual impossible? Is C# dependent or just don't have any sense in the OO world?
I know the concept has already been underlined but I did not find a simple answer to the previous question.
To summarize all the options presented:
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 otherstatic
entities), but only at compile time.static
equivalent (if needed at all) means "bound to a type object at runtime" instead. Examples include Delphi, Python, PHP.This can be emulated in a number of ways which can be classified as:
Eric Lippert has a blog post about this, and as usual with his posts, he covers the subject in great depth:
http://blogs.msdn.com/b/ericlippert/archive/2007/06/14/calling-static-methods-on-type-parameters-is-illegal-part-one.aspx
Yes it is possible.
The most wanted use case for that is to have factories which can be "overriden"
In order to do this, you will have to rely on generic type parameters using the F-bounded polymorphism.
Example 1 Let's take a factory example:
You also want
createB
to be accessible and returning B objects in the B class. Or you might like A's static functions to be a library that should be extensible by B. Solution:Example 2 (advanced): Let's define a static function to multiply matrices of values.
Now you can also use the static
MultiplyMatrix
method to return a matrix of complex numbers directly from ComplexNumberWhile technically its not possible to define a static virtual method, for all the reasons already pointed out here, you can functionally accomplish what I think your trying using C# extension methods.
From MSDN:
Check out C# Extension Methods (C# Programming Guide) for more details.
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.
I'm going to be the one who naysays. What you are describing is not technically part of the language. Sorry. But it is possible to simulate it within the language.
Let's consider what you're asking for - you want a collection of methods that aren't attached to any particular object that can all be easily callable and replaceable at run time or compile time.
To me that sounds like what you really want is a singleton object with delegated methods.
Let's put together an example:
in use:
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.