C# virtual static method

前端 未结 9 1298
盖世英雄少女心
盖世英雄少女心 2020-12-14 01:27

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

9条回答
  •  温柔的废话
    2020-12-14 01:54

    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
    

提交回复
热议问题