C# virtual static method

前端 未结 9 1272
盖世英雄少女心
盖世英雄少女心 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 02:07

    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 other static entities), but only at compile time.

      • This is possible in other languages where a 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:

      1. Use runtime binding
        • Static methods with a singleton object or lookalike
        • Virtual method that returns the same for all instances
          • Redefined in a derived type to return a different result (constant or derived from static members of the redefining type)
          • Retrieves the type object from the instance
      2. Use compile-time binding
        • Use a template that modifies the code for each derived type to access the same-named entities of that type, e.g. with the CRTP

提交回复
热议问题