1

Why are virtual, abstract and override keywords not valid for Static function? What's the logic behind it?

KennyLog_ins
  • 157
  • 1
  • 15
Talha Majid
  • 117
  • 2
  • 12

5 Answers5

3

Polymorphism via virtual dispatch is done by using the actual runtime type of the target instance, i.e. what the actual obj is in obj.Bar(...args...). Now: if you don't have a obj. instance, it makes no sense to discuss polymorphism.

Static methods are invoked with static-call, not virtual-call; the decision on what method to call is made entirely at compile time. There is no decision left to make. It comes down to the SomeType in SomeType.SomeMethod(...args...). You can of course still call between methods in a virtual method - you still have access to SomeBaseType.SomeOtherStaticMethod(...).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Virtual, override and abstract keywords are used with polymorphic behavoir and this behavoir applied to objects. So since static methods arent invoked by object call, therefore there is no use of these keywords with static function. Am i right? – Talha Majid Jun 05 '12 at 05:07
  • 1
    @TalhaMajid exactly. How would it know which is the "right" method to call? – Marc Gravell Jun 05 '12 at 06:23
1

There are, in fact, languages out there that extend the polymorphism concept to static methods. C# isn't one of those languages, though. You are not allowed to call static functions through some kind of class reference, so you don't actually have a chance to do anything "polymorphic" there.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
0

A static method is only really part of a particular class as a placeholder for the method. Static functions do not share lifetime or scope with the class they are declared in, and they are not really part of the object model.

These keywords (abstract, virtual, override) are related to an inheritance model and would have no meaning in a static context.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • what does this statement mean **Static functions do not share lifetime or scope with the class they are declared in.** – Talha Majid Jun 04 '12 at 11:16
  • I suppose it means the static method is actually a global function not associated with any object. It just happens to have access to a special set of global variables - the static variables of the class the static method is declared in. – Wormbo Jun 04 '12 at 15:55
0

You shouldn't be accessing static methods through an instance. When you do, C# doesn't care about what type of object is in your variable; it goes by the type of the variable. That is, MyType obj = null; obj.StaticMethod(); works, because the object is never consulted. Scratch all that; in my version of Visual Studio, with default settings, you aren't even allowed to use a static method via an instance. Either way, that means virtual static wouldn't do anything worthwhile; virtual and override work by consulting the object to decide what method to use, and with static, you only have the type (which is effectively hard-coded, and thus rather useless with polymorphism). abstract being basically "requires an override in your class", it's not useful either.

I guess it wouldn't have been hard for the C# people to let you pretend static stuff could be overridden...but it can't. In order to call it, you have to specify the type (explicitly like MyType.StaticMethod(), or implicitly as mentioned above). They opted to make it clear that you can't have virtual static methods and have it work like you're expecting, by making it so you can't do it at all.

cHao
  • 84,970
  • 20
  • 145
  • 172
0

The keywords virtual, abstract and override are used with methods that are inherited. Static methods aren't inherited.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    No, static methods can be inherited, static inheritance works just like instance inheritance. Except you are not allowed to make static methods virtual or abstract. Am i not right??? – Talha Majid Jun 04 '12 at 17:41
  • @TalhaMajid: No, static methods can not be inherited. A static method is accessible from within a derived class, or using the derived type, but it's not part of the derived class. – Guffa Jun 04 '12 at 18:11
  • i am sorry but its quite alot confusing. Can you please explain me with example?? – Talha Majid Jun 04 '12 at 19:02