static void Main(string[] args) { Console.WriteLine("*** Fun with intarface name calshes ***\n"); Octagon oct = new Octagon(); IDrowToForm idtf = (IDrowToForm)oct; idtf.Draw(); IDrawToMemory idtm = (IDrawToMemory)oct; idtm.Draw(); IDrawToPrinter idtp = (IDrawToPrinter)oct; idtp.Draw(); ((IDrowToForm)oct).Draw(); if (oct is IDrawToPrinter dp) { dp.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("****** Fun with Interface Name Clashes ******\n"); // All of these invocations call // the same Draw() method. Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); // We now must use casting to acces the Draw() members. Console.WriteLine(); ((IDrawToPrinter)oct).Draw(); // Could also use the is keyword if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); // All of these invocations call the // same Draw() method. Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToForm)oct).Draw(); // Could also use the "is" keyword. if (oct is IDrawToForm dtm) { dtm.Draw(); } IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); }
static void Main(string[] args) { Octagon octagon = new Octagon(); IDrawToForm form = octagon; form.Draw(); IDrawToMemory memory = octagon; memory.Draw(); IDrawToPrinter printer = octagon; printer.Draw(); // Сокращенная нотация, если переменная интерфейса не нужна ((IDrawToPrinter)octagon).Draw(); // Можно было бы также использовать ключевое слово as if (octagon is IDrawToMemory) { ((IDrawToMemory)octagon).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Octagon o = new Octagon(); IDrawToForm x = (IDrawToForm)o; IDrawToBuffer y = (IDrawToBuffer)o; IDrawToPrinter z = (IDrawToPrinter)o; x.Draw(); //--| y.Draw(); //--|==> CALL TO SAME METHOD z.Draw(); //--| Pentagon p = new Pentagon(); IDrawToForm a = (IDrawToForm)p; IDrawToBuffer b = (IDrawToBuffer)p; IDrawToPrinter c = (IDrawToPrinter)p; a.Draw(); //--==> CALL TO IDrawToForm.Draw() b.Draw(); //--==> CALL TO IDrawToBuffer.Draw() c.Draw(); //--==> CALL TO IDrawToPrinter.Draw() Console.ReadKey(); }
static void Main(string[] args) { Octagon oct = new Octagon(); IDrawToForm octF = (IDrawToForm)oct; octF.Draw(); IDrawToMemory octM = (IDrawToMemory)octF; octM.Draw(); IDrawToPrinter octP = (IDrawToPrinter)octF; octM.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Octogon octo = new Octogon(); octo.Draw(); ((IDrawToForm)octo).Draw(); ((IDrawToMemory)octo).Draw(); // ((IDrawToPrinter)octo).Draw(); IDrawToPrinter itfPrinter = octo as IDrawToPrinter; if (octo != null) { ((IDrawToPrinter)octo).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Octagon octagon = new Octagon(); IDrawForm itfForm = (IDrawForm)octagon; itfForm.Draw(); IDrawMemory itfMemory = (IDrawMemory)octagon; itfMemory.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)octagon; itfPrinter.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); // All of these invocations call the // same Draw() method! Octagon oct = new Octagon(); //oct.Draw() is no longer available w/o cast IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToPrinter itfPriner = (IDrawToPrinter)oct; itfPriner.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); Console.ReadLine(); }
static void FunWithInterfaceNameClash_implicit() { System.Console.WriteLine("=> fun with interface name clash:"); Octagon0 oct = new Octagon0(); IDrawForm iform = (IDrawForm)oct; iform.Draw(); IDrawInMemory iMemo = (IDrawInMemory)oct; iMemo.Draw(); IDrawToPrinter iPrin = (IDrawToPrinter)oct; iPrin.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); // All of these invocations call the // same Draw() method! Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); // Must use casting to access the Draw() when explicitly implemented. IDrawToForm itfForm2 = (IDrawToForm)oct; itfForm2.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToMemory)oct).Draw(); // Could also use the "is" keyword. if (oct is IDrawToPrinter) { ((IDrawToPrinter)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); /* * ((IDrawToPrinter)oct).Draw(); * * if (oct is IDrawToMemory) * ((IDrawToMemory)oct).Draw(); */ IDrawToPrinter itfPriner = (IDrawToPrinter)oct; itfPriner.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); Console.ReadLine(); }