Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Static method hide my instance method?

Tags:

c#

.net

Should the following code give a warning?

class Foo { public void Do() { /*...*/ } /*...*/ }
class Bar : Foo { public static void Do()  { /*...*/ } /*...*/ }

It gives:

"Warning CS0108: 'Bar.Do()' hides inherited member 'Foo.Do()'. Use the new keyword if hiding was intended."

If I make a change to the code:

class Foo { public static void Do() { /*...*/ } /*...*/ }
class Bar : Foo { public void Do()  { /*...*/ } /*...*/ }

I get the same warning.

If I make the following change, however, the warning goes away.

class Foo { public void Do() { /*...*/ } /*...*/ }
class Bar : Foo { new public static void Do() { /*...*/ } /*...*/ }

Let me make a further change:

class Foo { public void Do() { /*...*/ } /*...*/ }
class Bar : Foo { 
    new public static void Do() 
    { new Bar().Do();/*...*/ } /*...*/ 
}

This does not compile:

"Error CS0176: Member 'Bar.Do()' cannot be accessed with an instance reference; qualify it with a type name instead."

So, I lose access to my inherited method via an instance reference from a static method!

What would be the logic behind it? Or did I make a typo somewhere?

I came across this when I was trying to define a static method 'Show' for my form derived from 'Form'.

like image 302
isntn Avatar asked Apr 16 '09 16:04

isntn


People also ask

Can a static method override hide an instance method?

3) An instance method cannot override a static method, and a static method cannot hide an instance method.

Can static methods Access instance methods?

static Methods Cannot Directly Access Instance Variables and Instance Methods. A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated.

Why instance method Cannot override static method?

Can we override a static method? No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time.

Why can we not access instance data from static methods?

Static methods can't access instance methods and instance variables directly. They must use reference to object. And static method can't use this keyword as there is no instance for 'this' to refer to.


1 Answers

Where do you think the bug is? The fact that there is a warning is absolutely right. From the C# 3.0 spec, section 10.3.4:

A class-member-declaration is permitted to declare a member with the same name or signature as an inherited member. When this occurs, the derived class member is said to hide the base class member. Hiding an inherited member is not considered an error, but it does cause the compiler to issue a warning. To suppress the warning, the declaration of the derived class member can include a new modifier to indicate that the derived member is intended to hide the base member.

The fact that your method invocation fails is subtler, but it's basically because the member lookup algorithm picks the static method, and then this part of section 7.5.5.1 is used:

Final validation of the chosen best method is performed:

The method is validated in the context of the method group: If the best method is a static method, the method group must have resulted from a simple-name or a member-access through a type. If the best method is an instance method, the method group must have resulted from a simple-name, a member-access through a variable or value, or a base-access. If neither of these requirements is true, a compile-time error occurs.

like image 200
Jon Skeet Avatar answered Nov 04 '22 15:11

Jon Skeet