Use new keyword in c# [duplicate]

扶醉桌前 提交于 2020-01-13 09:29:21

问题


I have two classes

class  A
{
     public virtual void Metod1()
     {
         Console.WriteLine("A.Method1");
     }
}

class B : A
{
    public new void  Metod1()
    {
        Console.WriteLine("B.Method1");
    }
}

Then I write some code that declares instances of these classes, and calls their methods

     static void Main(string[] args)
    {
        A a = new B();
        a.Metod1();
        B b = new B();
        b.Metod1();
        Console.ReadKey();
    }

I gave following result:

A.Method1
B.Method1

But when I delete keyword new from signature method1 of class B and run Main method I got same result. Question:Is new keyword in signature of methods only for better readability?

Edit:Are there cases where we can not do without a new keyword?


回答1:


Yes, you are correct, it is for better readability in this situation.

But check carefully you should have a warning for not using new when hiding the base method.

You can read more here: http://msdn.microsoft.com/en-us/library/vstudio/435f1dw2.aspx




回答2:


The new keyword let the code reader be aware that this method will hide the method with the same name in its base. Even if you ommited it, the compiler would treat it in the same way, but warn you about that.

Since you declared the method as virtual, you would get this warning when you compile:

'B.Metod1()' hides inherited member 'A.Metod1()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

However, if you change the declaration by removing virtual as:

  • Declaration without virtual in A

    class A {
        public void Metod1() {
            Console.WriteLine("A.Method1");
        }
    }
    
    class B: A {
        public void Metod1() {
            Console.WriteLine("B.Method1");
        }
    }
    

Will still compile, but the warning message would be:

'B.Metod1()' hides inherited member 'A.Metod1()'. Use the new keyword if hiding was intended.

In either way as above, the following:

  • Test code

    var b=new B();
    b.Metod1();
    (b as A).Metod1();
    

will output:

B.Method1
A.Method1

But, if you declare as the following:

  • Declaration with override in B

    class A {
        public virtual void Metod1() {
            Console.WriteLine("A.Method1");
        }
    }
    
    class B: A {
        public override void Metod1() {
            Console.WriteLine("B.Method1");
        }
    }
    

Then the output would be:

B.Method1
B.Method1

That is because Method1 of the base class is not just hidden but overridden by the derived class.

A case that you cannot just use new is that if Method1 is a abstract method in a abstract class:

  • Code that must use override in derived classes

    abstract class A {
        public abstract void Metod1();
    }
    
    class B: A {
        public override void Metod1() { // use `new` instead of `override` would not compile
            Console.WriteLine("B.Method1");
        }
    }
    

In this case you cannot just use new, because A requires derived classes to override Method1.




回答3:


Knowing When to Use Override and New Keywords (C# Programming Guide)

In C#, a method in a derived class can have the same name as a method in the base class. You can specify how the methods interact by using the new and override keywords. The override modifier extends the base class method, and the new modifier hides it.

also

By using new, you are asserting that you are aware that the member that it modifies hides a member that is inherited from the base class.



来源:https://stackoverflow.com/questions/15803965/use-new-keyword-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!