I
I
ikutin6662021-07-06 17:09:08
C++ / C#
ikutin666, 2021-07-06 17:09:08

What is the point of using new in methods?

abstract class A
    {

        public  void hello()
        {
            Console.WriteLine("hello A" );
            Console.ReadLine();
        }
    }
    class B : A
    {

        public new  void hello()
        {
            Console.WriteLine("hello B");
            Console.ReadLine();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var b = new B();
            b.hello();
        }
    }


what is the difference of use and if the result is the same for both, I understand that it is correct to use with new, but I am wondering why we do this? public new void hello()public void hello()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-07-06
@ikutin666

what is the difference between using public new void hello() and public void hello()

No difference.
if the result is the same for both, I understand that it is correct to use with new, but I'm wondering why we do this?

To clearly show that you are hiding the old method, and not just forgot to write override

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question