using System;
namespace ConsoleView
{
//This holds only original versions of the methods
public class Base
{
//This method will be overriden by Child, so, it'll never execute
public virtual void View()
{
Console.WriteLine("Viewing Base, Original virtual View");
}
//This method will be hidden only from inside Child (using 'new' modifier)
//Note that I could make it virtual too, this wouldn't change anything
public void ViewNew()
{
Console.WriteLine("Viewing Base, Original OLD ViewNew ;)");
}
}
//this class holds all the edited versions of methods
public class Child:Base
{
//Here I use override to permanently replace the original method
//Whatever the reference type is,
// this'll always be the method executed when you call View()
public override void View()
{
Console.WriteLine("Viewing Child, override View");
}
//this hides the method only if the refernce type is Child or inherited from Child
//a call from a reference of type Base will execute the OLD method, not thiis one
public virtual new void ViewNew()
{
Console.WriteLine("Viewing Child, new View");
}
}
/// Used to hold the 'Main' method only!
/// to show up the result of using each in the Console
class Implementor
{
/// The main entry point for the application.
/// Used to show up what's the expected result.
[STAThread]
static void Main(string[] args)
{
//This part is very typical
//Creating a Child reference, and it referes to (its value is) a child
Child myChild=new Child();
//Indicating reference and value types in the output
Console.WriteLine
("The reference now is a Child that refers to a child. Typical, ha?!");
//Will show up the overriden version
//as it has been totally overriden using the override modifier
myChild.View();
//Shows up the new version,
//as the new modifier hid the original version
myChild.ViewNew();
//The difference comes here
//The reference is a Base, but it refers to (its value is) a Child
Base myBase=(Base)new Child();
//Indicating the previous change in the output
Console.WriteLine
("\n\nThe reference now is a Base, yet it still refers to a child");
//Will show the overriden version,
//even though the reference is of type Base,
//it's been totally overriden, not just hdden
myBase.View();
//Will show the original version NOT the new one
//as the new hides the method in Child only
//it doesn't override it totally
myBase.ViewNew();
//Just to halt the screen so that I can read the output
Console.Read();
}
}
}