Can someone explain what is the main difference between those two part of code, and why or when I should use a reference to a base class, as for me it is the same thing.
internal class MyBaseClass
{
public string Field = "Hello";
public void Print()
{
Console.WriteLine("This is the base class.");
}
}
internal class MyDerivedClass : MyBaseClass
{
public string FieldDerived = "Coucou";
public new void Print()
{
Console.WriteLine("This is the derived class.");
}
public new void Print2()
{
Console.WriteLine("This is the derived class.");
}
}
internal class Program
{
private static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = (MyBaseClass) derived;
// ↑
// Cast to base clas
}
}
And this is the code, that for me do the exactly same thing :
internal class MyBaseClass
{
public string Field = "Hello";
public void Print()
{
Console.WriteLine("This is the base class.");
}
}
internal class MyDerivedClass : MyBaseClass
{
public string FieldDerived = "Coucou";
public new void Print()
{
Console.WriteLine("This is the derived class.");
}
public new void Print2()
{
Console.WriteLine("This is the derived class.");
}
}
internal class Program
{
private static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc = new MyBaseClass();
// ↑
// Use the base class
}
}