0% found this document useful (0 votes)
41 views

004 Method Hiding in C Example

This C# code example demonstrates method hiding by defining a Display() method in multiple classes related by inheritance. The FC base class defines a Display() method that writes "FC::Display" to the console. The SC and TC derived classes both define their own Display() methods that hide the base method and instead write "SC::Display" and "TC::Display" respectively. When an instance of the most derived TC class calls Display(), it invokes the TC version of the method rather than those defined in the base classes.

Uploaded by

duskoruzicic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

004 Method Hiding in C Example

This C# code example demonstrates method hiding by defining a Display() method in multiple classes related by inheritance. The FC base class defines a Display() method that writes "FC::Display" to the console. The SC and TC derived classes both define their own Display() methods that hide the base method and instead write "SC::Display" and "TC::Display" respectively. When an instance of the most derived TC class calls Display(), it invokes the TC version of the method rather than those defined in the base classes.

Uploaded by

duskoruzicic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/*************************************************************************

* Method Hiding in C# Example


*
*************************************************************************/

using System;
namespace MyProject.Examples
{
class FC
{
public void Display()
{
System.Console.WriteLine("FC::Display");
}
}
class SC : FC
{
public new void Display()
{
//base.Display();
System.Console.WriteLine("SC::Display");
}
}
class TC : SC
{
public new void Display()
{
//base.Display();
System.Console.WriteLine("TC::Display");
}
}
class ExampleOne
{
public static void Main()
{
TC obj = new TC();
// SC obj = new FC();
// ((FC)obj).Display();
obj.Display();

Console.ReadKey();
}
}
}

You might also like