On Oct 6, 11:00*am, puzzlecracker <ironsel2...@gmail.comwrote:
Are static method inheritable under csharp rules, if so how can they
be used? Can they be virtual?
They reason I am asking is because operators are static, and I don't
understand how can we apply operators to base and derive classes
simultaneously?
Static Methods are available to derived classes via class name. All
the inheritance rules (public, protected, etc) apply as in normal
case. Only difference is that you need to access them via class name.
However, a static method can not be virtual or abstract. Because
static methods are class level methods not object dependent methods.
So it makes sense not to virtualize them.
They reason I am asking is because operators are static, and I don't
understand how can we apply operators to base and derive classes
simultaneously?
I think operators overloaded in base class are not much useful for the
derived class.
Consider the following code
public class A
{
public int i = 10;
public static A operator +(A aObj)
{
aObj.i++;
return aObj;
}
}
public class B : A
{
}
If I write code below
B bObj = new B();
B bObj2 = new B();
B bObj3 = bObj + bObj2;
compiler will comply that + is not defined on Class B.
-Cnu