36

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Toto
  • 7,491
  • 18
  • 50
  • 72
  • Can you split out your second question into a separate question? – EFraim Aug 07 '09 at 09:38
  • Splitted to the following subject : C# interface static method call with generics – Toto Aug 07 '09 at 09:55
  • Duplicate: http://stackoverflow.com/questions/248263/why-cant-i-declare-c-methods-virtual-and-static – Lasse V. Karlsen Aug 07 '09 at 12:28
  • Possible duplicate of [Why can't I have abstract static methods in C#?](https://stackoverflow.com/questions/3284/why-cant-i-have-abstract-static-methods-in-c) – Ken Kin Jan 10 '18 at 04:57
  • Possible duplicate of [How to implement virtual static properties?](https://stackoverflow.com/questions/15346631/how-to-implement-virtual-static-properties) – peterh Jan 10 '18 at 11:13

10 Answers10

51

virtual means the method called will be chosen at run-time, depending on the dynamic type of the object. static means no object is necessary to call the method.

How do you propose to do both in the same method?

sbi
  • 219,715
  • 46
  • 258
  • 445
  • we don't call them functions :) we call them methods – Hannoun Yassir Aug 07 '09 at 09:47
  • 2
    @Yassir: Ah, thanks. Outing me as being the C++ guy here. :) I'll correct this. – sbi Aug 07 '09 at 09:55
  • 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. – Toto Aug 07 '09 at 09:57
  • You cannot do that, as static methods aren't inherited. – Dykam Aug 07 '09 at 10:02
  • 15
    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 Grissom May 17 '12 at 20:07
  • @Lee: Interesting, I didn't know that. Now, given that "static" means no object necessary, how do I invoke a static virtual function in Delphi? I can't even imagine how to do that. – sbi May 18 '12 at 09:33
  • 2
    @sbi, http://docwiki.embarcadero.com/RADStudio/en/Methods#Class_Static_Methods There are lots of valid scenarios, post a question on the Embarcadero forums to request some examples. – Lee Grissom Jun 18 '12 at 01:17
24

Eric Lippert has a blog post about this, and as usual with his posts, he covers the subject in great depth:

https://learn.microsoft.com/en-us/archive/blogs/ericlippert/calling-static-methods-on-type-parameters-is-illegal-part-one

“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”

Pang
  • 9,564
  • 146
  • 81
  • 122
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
10

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.

Some Delphi beauty:

type
  TFormClass = class of TForm;
var
  formClass: TFormClass;
  myForm: TForm;
begin
  ...
  formClass = GetAnyFormClassYouWouldLike;
  myForm = formClass.Create(nil);
  myForm.Show;
end
Pang
  • 9,564
  • 146
  • 81
  • 122
Johan Nilsson
  • 101
  • 1
  • 2
  • 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](http://stackoverflow.com/a/1244431/648265) - is what it actually meant ever since its introduction in C. The feature request is effectively to change its meaning to "class-bound". – ivan_pozdeev Feb 08 '15 at 09:33
10

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 mean that it is impossible. There are languages that allow this!! Look at Delphi, for example.

Pang
  • 9,564
  • 146
  • 81
  • 122
Sergey Krusch
  • 1,928
  • 16
  • 17
5

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:

public interface ICurrencyWriter {
    string Write(int i);
    string Write(float f);
}

public class DelegatedCurrencyWriter : ICurrencyWriter {
    public DelegatedCurrencyWriter()
    {
        IntWriter = i => i.ToString();
        FloatWriter = f => f.ToString();
    }
    public string Write(int i) { return IntWriter(i); }
    public string Write(float f) { return FloatWriter(f); }
    public Func<int, string> IntWriter { get; set; }
    public Func<float, string> FloatWriter { get; set; }
}

public class SingletonCurrencyWriter {
    public static DelegatedCurrencyWriter Writer {
        get {
            if (_writer == null)
               _writer = new DelegatedCurrencyWriter();
            return _writer;
        }
    }
}

in use:

Console.WriteLine(SingletonCurrencyWriter.Writer.Write(400.0f); // 400.0

SingletonCurrencyWriter.Writer.FloatWriter = f => String.Format("{0} bucks and {1} little pennies.", (int)f, (int)(f * 100));

Console.WriteLine(SingletonCurrencyWriter.Writer.Write(400.0f); // 400 bucks and 0 little pennies

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.

plinth
  • 48,267
  • 11
  • 78
  • 120
3

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:

class A: { public static A Create(int number) { return ... ;} }
class B: A { /* How to override the static Create method to return B? */}

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:

class A<T> where T: A<T> { public static T Create(int number) { return ...; } }
class B: A<B>  { /* no create function */ }
B theb = B.Create(2);       // Perfectly fine.
A thea = A.Create(0);       // Here as well

Example 2 (advanced): Let's define a static function to multiply matrices of values.

public abstract class Value<T> where T : Value<T> {
  //This method is static but by subclassing T we can use virtual methods.
  public static Matrix<T> MultiplyMatrix(Matrix<T> m1, Matrix<T> m2) {
    return // Code to multiply two matrices using add and multiply;
  }
  public abstract T multiply(T other);
  public abstract T add(T other);
  public abstract T opposed();
  public T minus(T other) {
    return this.add(other.opposed());
  }
}
// Abstract override
public abstract class Number<T> : Value<T> where T: Number<T> {
  protected double real;

  /// Note: The use of MultiplyMatrix returns a Matrix of Number here.
  public Matrix<T> timesVector(List<T> vector) {
    return MultiplyMatrix(new Matrix<T>() {this as T}, new Matrix<T>(vector));
  }
}
public class ComplexNumber : Number<ComplexNumber> {
  protected double imag;
  /// Note: The use of MultiplyMatrix returns a Matrix of ComplexNumber here.
}

Now you can also use the static MultiplyMatrix method to return a matrix of complex numbers directly from ComplexNumber

Matrix<ComplexNumber> result = ComplexNumber.MultiplyMatrix(matrix1, matrix2);
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
  • This is actually called "[Curiously recurring template pattern](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern)". – ivan_pozdeev Feb 08 '15 at 09:27
  • 2
    This name was invented by an engineer in 1995 minimum 6 years after F-bounded polymorphism was mathematically formalized. http://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 Mayer Feb 10 '15 at 10:15
  • Wow, I didn't know that. Added this to the Wikipedia article. – ivan_pozdeev Feb 10 '15 at 17:09
  • Just applied Example 1 to my tool avoiding tons of boilerplate code. One small pitfall I came across was the call to the 'Create' method; this has to be called as A.Create(0) or B.Create(2); plain A.Create(0) or B.Create(2) is not working. – Daniel Feb 24 '21 at 09:26
2

While technically it's not possible to define a static virtual method, for all the reasons already pointed out here, you can functionally accomplish what I think you're trying using C# extension methods.

From Microsoft Docs:

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Check out Extension Methods (C# Programming Guide) for more details.

Pang
  • 9,564
  • 146
  • 81
  • 122
Zach Bonham
  • 6,759
  • 36
  • 31
1

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.

thecoop
  • 45,220
  • 19
  • 132
  • 189
1

To summarize all the options presented:

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
1

The 2022+ answer, if you are running .Net 7 or above, is that now static virtual members is now supported in interfaces. Technically it's static abstract instead of "static virtual" but the effect is that same. Standard static methods signatures can be defined in an interface and implemented statically.

Here are a few examples on the usage and syntax in .Net 7

Michael Bond
  • 131
  • 3