123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IntfAsMethodParam { class Program { static void Main(string[] args) { Shape[] shapes = { new Triangle("Triangle"), new Circle("Circle") }; foreach (var shape in shapes) { if (shape is IDraw3D) { SomeMethod((IDraw3D)shape); } // <<< applying method } } static void SomeMethod(IDraw3D itf3d) // <<< IDraw3D interface as param { Console.WriteLine("3d drawing "); itf3d.Draw3D(); } // base class public class Shape { public string Name { get; set; } public Shape(string name) { Name = name; } public Shape() { } public virtual void Draw() { } } // Circle - derived from shape and NOT implementing IPointy public class Circle : Shape,IDraw3D { public Circle() { } public Circle(string name) : base(name) { } public override void Draw() { Console.WriteLine("Shape name is {0}", this.Name); } public void Draw3D() { Console.WriteLine("3D Drawing Shape name is {0}", this.Name); } } // 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; } } } //declaring interface public interface IDraw3D { void Draw3D(); } //declaring interface public interface IPointy { byte Points { get; } } } } |