If you expect "ClassC", then replace new with override:
public class classC : classB
{
public override string Print( )
{
return "ClassC";
}
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I Working on console application I ask why result is
ClassB
IT must be ClassC
i executed app on console and it give me ClassB
why final result ClassB i expect it will be ClassC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace apptest
{
class Program
{
static void Main(string[] args)
{
classA a = new classC();
Console.WriteLine(a.Print());
Console.ReadKey();
}
public class classA
{
public virtual string Print()
{
return "ClassA";
}
}
public class classB: classA
{
public override string Print()
{
return "ClassB";
}
}
public class classC: classB
{
public new string Print()
{
return "ClassC";
}
}
}
}
If you expect "ClassC", then replace new with override:
public class classC : classB
{
public override string Print( )
{
return "ClassC";
}
}
Classes really implement an interface. A virtual override replaces the virtual method, so if you downcast it’s the same method. The new operator replaces the method only for an instance is cast as that class.
So
classA a = new classC();
Console.WriteLine(((classC)a).Print());
Would print ClassC
Hi @ahmed salah ,
Take a look at the following document:
Knowing When to Use Override and New Keywords (C# Programming Guide)
Here's an example in the document which can help you understand the difference between 'override' and 'new'.
class Program
{
static void Main(string[] args)
{
BaseClass bc = new BaseClass();
DerivedClass dc = new DerivedClass();
BaseClass bcdc = new DerivedClass();
// The following two calls do what you would expect. They call
// the methods that are defined in BaseClass.
bc.Method1();
bc.Method2();
// Output:
// Base - Method1
// Base - Method2
// The following two calls do what you would expect. They call
// the methods that are defined in DerivedClass.
dc.Method1();
dc.Method2();
// Output:
// Derived - Method1
// Derived - Method2
// The following two calls produce different results, depending
// on whether override (Method1) or new (Method2) is used.
bcdc.Method1();
bcdc.Method2();
// Output:
// Derived - Method1
// Base - Method2
}
}
class BaseClass
{
public virtual void Method1()
{
Console.WriteLine("Base - Method1");
}
public virtual void Method2()
{
Console.WriteLine("Base - Method2");
}
}
class DerivedClass : BaseClass
{
public override void Method1()
{
Console.WriteLine("Derived - Method1");
}
public new void Method2()
{
Console.WriteLine("Derived - Method2");
}
}
So if you want your code give you 'ClassC', just:
classC c = new classC();
Console.WriteLine(c.Print());
Hope it could be helpful.
Best Regards,
Xingyu Zhao
*
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.