C# virtual static method
Asked Answered
B

10

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.

Bernabernadene answered 7/8, 2009 at 9:31 Comment(5)
Can you split out your second question into a separate question?Eighty
Splitted to the following subject : C# interface static method call with genericsBernabernadene
Duplicate: #248763Fachanan
Possible duplicate of Why can't I have abstract static methods in C#?Newby
Possible duplicate of How to implement virtual static properties?Shashaban
I
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?

Illtempered answered 7/8, 2009 at 9:35 Comment(7)
we don't call them functions :) we call them methodsRoesch
@Yassir: Ah, thanks. Outing me as being the C++ guy here. :) I'll correct this.Illtempered
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.Bernabernadene
You cannot do that, as static methods aren't inherited.Endways
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 OPQuadrature
@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.Illtempered
@sbi, docwiki.embarcadero.com/RADStudio/en/… There are lots of valid scenarios, post a question on the Embarcadero forums to request some examples.Quadrature
K
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”

Kryska answered 7/8, 2009 at 12:25 Comment(1)
Downvoted because this feature is actually supported in other OO languages, notably Delphi.Tryst
H
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
Hedrick answered 13/9, 2011 at 16:40 Comment(1)
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".Hairsplitting
K
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.

Klemens answered 12/12, 2011 at 10:29 Comment(0)
U
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.

Uno answered 7/8, 2009 at 12:49 Comment(0)
A
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);
Amanda answered 15/12, 2014 at 16:10 Comment(4)
This is actually called "Curiously recurring template pattern".Hairsplitting
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)Joy
Wow, I didn't know that. Added this to the Wikipedia article.Hairsplitting
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<T>.Create(0) or B<T>.Create(2); plain A.Create(0) or B.Create(2) is not working.Holdup
R
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.

Retrochoir answered 7/8, 2009 at 12:35 Comment(1)
Extension methods are no different from plain virtual ones.Hairsplitting
R
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.

Rocha answered 7/8, 2009 at 12:20 Comment(0)
H
1

To summarize all the options presented:

Hairsplitting answered 10/2, 2015 at 17:49 Comment(0)
C
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

Circuitry answered 5/12, 2022 at 1:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.