Skip to content

Instantly share code, notes, and snippets.

@faridprogrammer
Created July 5, 2015 08:41
Show Gist options
  • Save faridprogrammer/4340d90592fff835255f to your computer and use it in GitHub Desktop.
Save faridprogrammer/4340d90592fff835255f to your computer and use it in GitHub Desktop.
class BaseShape
{
public virtual void Display()
{
Console.WriteLine("Displaying Base Class!");
}
}
class Circle : BaseShape
{
public override void Display()
{
Console.WriteLine("Displaying Circle Class!");
}
}
class Rectangle : BaseShape
{
public override void Display()
{
Console.WriteLine("Displaying Rectangle Class!");
}
}
class Result
{
public static List<BaseShape> Shapes = new List<BaseShape>();
public static void Main()
{
Shapes.Add(new Circle());
Shapes.Add(new Circle());
DisplayShapes_BAD();
}
public static void DisplayShapes_BAD(){
foreach(var item in Shapes)
{
if(typeof(Circle) == item.GetType())
{
((Circle)item).Display();
}
if(typeof(Rectangle) == item.GetType())
{
((Circle)item).Display();
}
}
}
public static void DisplayShapes_GOOD(){
foreach(var item in Shapes)
{
item.Display();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment