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?
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.
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.
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.
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.
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
.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With