Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use new keyword in c# [duplicate]

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?

like image 872
vborutenko Avatar asked Apr 04 '13 06:04

vborutenko


People also ask

Can you use the new keyword in C?

Here in the example an object is created for the class using the new. The following is an example. Calculate c = new Calculate(); You can also use the new keyword to create an instance of the array.

What is the new keyword?

New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with 'new' operator, the following actions are taken: A new empty object is created.

Does C++ have a new keyword?

new keywordThe new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.

What is use of new keyword in Java?

The new keyword in Java instantiates a class by allocating desired memory for an associated new object. It then returns a reference to that memory. Many times, the new keyword in Java is also used to create the array object. The new keyword is followed by a call to a constructor, which instantiates the new object.


3 Answers

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.

like image 123
Ken Kin Avatar answered Oct 06 '22 00:10

Ken Kin


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.

like image 34
Habib Avatar answered Oct 05 '22 23:10

Habib


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

like image 44
G.Y Avatar answered Oct 05 '22 23:10

G.Y