Consider this code:

internal class Program
   {

      private static void Main(string[] args)
         {
            var student = new Student();
            student.ShowInfo(); //output --> "I am Student"
        }
    }
    public class Person
    {
        public void ShowInfo()
        {
            Console.WriteLine("I am person");
        }
    }

    public class Student : Person
    {
        public void ShowInfo()
        {
            Console.WriteLine("I am Student");
        }
    }

in above code we don't use method hiding.

when create instance of student and call showinfo method my output is I am Student i don't use new keyword.

Why does not call parent method when we don't use method hiding?

有帮助吗?

解决方案

No matter whether you are using the new keyword or not the base method is not virtual. So it is always the derived method that will get called. The only difference is that the compiler emits you a warning because the base method is hidden and you didn't explicitly used the new keyword:

'Student.ShowInfo()' hides inherited member 'Person.ShowInfo()'. Use the new keyword if hiding was intended.

The C# compiler is emitting this to warn you that you might have by mistake done that without even realizing it.

In order to fix the warning you should use the new keyword if this hiding was intentional:

public new void ShowInfo()
{
    Console.WriteLine("I am Student");
}

If not, you should make the base method virtual:

public class Person
{
    public virtual void ShowInfo()
    {
        Console.WriteLine("I am person");
    }
}

and then override it in the derived class:

public class Student : Person
{
    public override void ShowInfo()
    {
        Console.WriteLine("I am Student");
    }
}

Obviously in both cases you have the possibility to call the base method if you want that:

public override void ShowInfo()
{
    base.ShowInfo();
    Console.WriteLine("I am Student");
}

其他提示

Actually, the new modifier has no effect at all from a program logic perspective.

The only effect the new modifier has is to document your intentions so that:

  • the compiler can warn you when appropriate (such as when using the new modifier in a declaration that does not hide an inherited member - or the opposite, when not using the new modifier even if the declaration does hide an inherited member)

  • other developers easier understand your code

What really matters to hiding/not hiding is whether you use the override modifier or not (which you can only use if the inherited member is virtual). If you don't use the override modifier, the inherited member is always hidden. In your example you don't use the override modifier, so you do hide the inherited member.

Note: Don't confuse the 'new modifier' with the 'new operator' or the 'new constraint'

It is using method hiding, it is just implicitly used. The new keyword should be used to explicitly show that you know you are hiding the method, however, your code will still compile and hide the method. Although this is definitely not recommended.

Student.ShowInfo() hides inherited member Person.ShowInfo()

you better check the compiler warnings

'ConsoleApplication5.Program.Student.ShowInfo()' hides inherited member 'ConsoleApplication5.Program.Person.ShowInfo()'. Use the new keyword if hiding was intended.

Using the same method signature ShowInfo means that you are going to hide the parent classes method. The new keyword is not required, it just lets the developer know that the current method is hiding another method in the parent class.

It is hiding which is why you get a warning saying that if you intentionally meant to hide it, you should use the new keyword. Check section 3.7.1.2 of the C# Language Spec Hiding through inheritance.

Name hiding through inheritance occurs when classes or structs redeclare names that were inherited from base classes. This type of name hiding takes one of the following forms:

•A constant, field, property, event, or type introduced in a class or struct hides all base class members with the same name.

•A method introduced in a class or struct hides all non-method base class members with the same name, and all base class methods with the same signature (method name and parameter count, modifiers, and types).

•An indexer introduced in a class or struct hides all base class indexers with the same signature (parameter count and types).

The reason is, the base classes methods are hidden when you redeclare it. It is called hiding through inheritance.

As others said method hiding is implicit whether you used new keyword or not. You can check with this piece of code -

     private static void Main(string[] args)
     {
        Person student = new Student();
        student.ShowInfo(); //output --> "I am person"
     }

When you instantiate base class object with derived class, your derived method won't get called.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top