using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Interace
{
class Program
{
static IPointy FindFirstPointyShape(Shape[] shapes) {
foreach (var s in shapes) {
if (s is IPointy) { return s as IPointy; }
}
return null;
}
static void Main(string[] args)
{
IPointy[] myPointObjs = { new Hexagon(), new Knife(), new Triangle() };
foreach (var i in myPointObjs){ Console.WriteLine(i.Points); }
Shape[] shapes = { new Triangle("Triangle"), new Hexagon("Hexagon"), new Circle("Circle") };
IPointy firstPointyItem = FindFirstPointyShape(shapes);
Console.WriteLine("Item has {0} points", firstPointyItem.Points);
Triangle t = new Triangle("Triangle");
t.Draw();
Hexagon h = new Hexagon("Hexagon");
h.Draw();
Circle c = new Circle("Circle");
c.Draw();
// another example
for (int i = 0; i < shapes.Length; i++)
{
if (shapes[i] is IPointy) Console.WriteLine("Points in " + shapes[i].Name + " " + (shapes[i] as IPointy).Points);
else Console.WriteLine("Ipointy not supported in " + shapes[i].Name);
}
Circle c1 = new Circle("Circle");
IPointy itf = null;
try { itf = (IPointy)c1; }
catch (InvalidCastException e) { Console.WriteLine(e.Message); }
Console.ReadLine();
}
// base class
public class Shape
{
public string Name { get; set; }
public Shape(string name) { Name = name; }
public Shape() { }
public virtual void Draw() { }
}
//declaring interface
public interface IPointy
{
byte Points { get; }
}
// another one
public interface IDraw3D
{
void Draw3D();
}
// Triangle - derived from shape and implementing IPointy
public class Triangle : Shape, IPointy
{
public Triangle() { }
public Triangle(string name) : base(name) { }
public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); }
public byte Points { get { return 3; } }
}
// Hexagon - derived from shape and implementing IPointy
public class Hexagon : Shape, IPointy, IDraw3D
{
public Hexagon() { }
public Hexagon(string name) : base(name) { }
public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); }
public byte Points { get { return 6; } } // << another implementation of IPointy
public void Draw3D() { Console.WriteLine("Drawing Hexagon in 3D"); }
}
//
// Circle - derived from shape and NOT implementing IPointy
public class Circle : Shape
{
public Circle() { }
public Circle(string name) : base(name) { }
public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); }
}
public class ThreeDCircle : Circle, IDraw3D
{
public void Draw3D() { Console.WriteLine("Draw Circle in 3D"); }
}
// knife
public class Knife : Shape, IPointy
{
public Knife() { }
public Knife(string name) : base(name) { }
public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); }
public byte Points { get { return 4; } } // << another implementation of IPointy
}
// fork
public class Fork : Shape, IPointy
{
public Fork() { }
public Fork(string name) : base(name) { }
public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); }
public byte Points { get { return 1; } } // << another implementation of IPointy
}
}
}