We have been gives these classes as an example. In the student class the new keyword that is used on the print method...

is it actually needed because you get the same result if it is taken out?

class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student("Dave", 1234);

            s.Print();

            s.Birthday();

            s.Print();

            Console.ReadLine();
            
        }
    }
class Person
    {
        protected String name;
        protected int age;

        public void Birthday()
        {
            age++;
        }

        public void Print()
        {
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("Age: {0}", age);
        }
    }
class Student : Person
    {
        int id;

        public Student(string n, int ID)
        {
            age = 0;
            name = n;
            id = ID;
        }

        public new void Print() //new keyword here
        {
            base.Print();
            Console.WriteLine("ID: {0}", id);
        }
    }

The new keyword is used to tell the compiler that you really do want to replace the method in the base class. If you don't include it, you get this compiler warning:

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

In my book it states that hiding should be avoided because it does not allow for polymorphism, using virtual methods instead?

That is a best practice, but sometimes you don't have the base class as source so you can't make the method virtual.

What do you mean by as source? You have to inherit the base class in order to be able to use the new keyword to hide it?

I have changed the code a bit, with using virtual and override. And did some small changes also. Take a look:

class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student("Dave", 1234);
            s.Birthday();
            s.Print();            
            Console.ReadLine();

        }
    }
    class Person
    {
        protected string name;
        protected int age;

        public void Birthday()
        {
            age = 28;
        }

        public virtual void Print()
        {
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("Age: {0}", age);
        }
    }
    class Student : Person
    {
        int id;
        public Student(string N, int ID)
        {
            name = N;
            id = ID;
        }

        public override void Print() //new keyword here
        {
            base.Print();
            Console.WriteLine("ID: {0}", id);
        }
    }

Mitja

What do you mean by as source? You have to inherit the base class in order to be able to use the new keyword to hide it?

What I mean is that some classes you want to derive from are in DLLs that you have no control over. You can't change the definition of Form, for example, you have to work with what they provide you. If they didn't mark a method virtual, there is no way for you to do so. Thus the 'new' keyword in the method signature to make sure you know that you are hiding a base class method and that you really want to do so.

This article has been dead for over six months. Start a new discussion instead.