This is confusing to me please expalin me the behaviour of this?
A declaration of a new member hides an inherited member only within the scope of the new member.
Copy
**class Base
{
public static void F() {}
}
class Derived: Base
{
new private static void F() {} // Hides Base.F in Derived only
}
class MoreDerived: Derived
{
static void G() { F(); } // Invokes Base.F
}**
In the example above, the declaration of F in Derived hides the F that was inherited from Base, but since the new F in Derived has private access, its scope does not extend to MoreDerived. Thus, the call F() in MoreDerived.G is valid and will invoke Base.F.
I am not understanding that how static void G() { F(); } can access the base class f method when it can access all methods of it’s immediate super class and super class hides the f method of base class
MoreDerivedcannot access all methods of its super class; in particular, it cannot accessprivatemethods. In C#, anything markedprivatein a class is invisible to anything outside of that class. In other words, adding or removing aprivatemethod will not change how anything outside of that class compiles. Since thenew private static void Fis invisible to the outside world,MoreDerivedis unaffected by it.If the modifier was
protectedinstead,MoreDerivedwould see it.In the example you gave, the
newkeyword only changes the meaning of names in the namespace. It does not change what methods are available. A method ofDerivedcan still callBase.F()like so:It’s analogous to this example: