static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); Console.WriteLine(oct); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)oct).Draw(); // Could also use the "as" keyword. if (oct is IDrawToMemory dtm) { dtm.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // This is now a compiler error! // oct.Draw(); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface varaible for later use. ((IDrawToPrinter)oct).Draw(); // Always a good practice to test // before extracting an interface. if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
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) { 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) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon octagon = new Octagon("Octagon"); Circle circle = new Circle("Circle"); // We now must use casting to access the Draw() // members. IDrawToForm drawToForm = (IDrawToForm)octagon; drawToForm.Draw(); drawToForm = (IDrawToForm)circle; drawToForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)octagon).Draw(); // Will draw twice just to show use of as keyword if (octagon is IDrawToMemory) { ((IDrawToMemory)octagon).Draw(); (octagon as IDrawToMemory).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) { Octagon oct = new Octagon(); //Для доступа к членам Draw() должно использоваться приведение. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); //Сокращенная форма, если переменная интерфейса не нужна. ((IDrawToMemory)oct).Draw(); }
static void ExampleManyInterfaces() { Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToPrinter)oct).Draw(); if (oct is IDrawToMemory dtm) { dtm.Draw(); } }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Claches *****\n"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToPrinter)oct).Draw(); if (oct is IDrawToMemory dtm) { dtm.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun With Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // We now myst use casting to access the Draw() members IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need the inerface // variable for later use ((IDrawToPrinter)oct).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToMemory)oct).Draw(); if (oct is IDrawToPrinter) { ((IDrawToPrinter)oct).Draw(); } Console.ReadLine(); }
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) { Console.WriteLine("**** Fun with interface name clashes ****"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToMemory)oct).Draw(); if (oct is IDrawToPrinter) { ((IDrawToPrinter)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(); //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(); }
public static void InterfaceClashes() { Console.WriteLine("****** Fun with Interface Name Clashes ****\n"); Octagon o = new Octagon(); //Long form IDrawToForm idrawToForm = (IDrawToForm)o; idrawToForm.Draw(); //Shorter form ((IDrawToPrinter)o).Draw(); //Using the 'is a' methodology if (o is IDrawToMemory dtm) { dtm.Draw(); } }
/// <summary> /// Explicitly Interface Implementation /// </summary> private void ExplicitInterfaceImplementation() { Console.WriteLine("=> Explicitly Interface Implementation: "); OctagonWithExplicitBinding oct = new OctagonWithExplicitBinding(); // We must use casting to access the Draw() method IDrawToForm iftForm = (IDrawToForm)oct; iftForm.Draw(); ((IDrawToPrinter)oct).Draw(); if (oct is IDrawToMemory dtm) { dtm.Draw(); } Console.WriteLine(); }
static void Main(string[] args) { Octogon oct = new Octogon(); //We not must use cast to access the draw() IDrawToForm itfForm = (IDrawToForm)oct; itfForm.draw(); //shorthand notation if you dont need the inerface //variable for later use ((IDrawToPrinter)oct).draw(); //Could as use the is keyword if (oct is IDrawToMemory) { ((IDrawToMemory)oct).draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Clashes *****"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); //Сокращенная форма ((IDrawToMemory)oct).Draw(); // Можно с is if (oct is IDrawToPrinter dtm) { dtm.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // 现在必须使用转换来访问Draw()成员 IDrawToForm itform = (IDrawToForm)oct; itform.Draw(); // 如果以后不需要接口变量,可以简化成这个形式 ((IDrawToPrinter)oct).Draw(); // 也可以用"is"关键字 if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****"); // All of these invocations call the same Draw() method! Octogon oct = new Octogon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // shorthand notation if variable not needed afterward ((IDrawToPrinter)oct).Draw(); // can also use the "is" keyword if (oct is IDrawToMemory dtm) { dtm.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(); #region Without Explicit Interface Implementation //IDrawToForm itfForm = (IDrawToForm)oct; //itfForm.Draw(); //IDrawToPrinter itfPriner = (IDrawToPrinter)oct; //itfPriner.Draw(); //IDrawToMemory itfMemory = (IDrawToMemory)oct; //itfMemory.Draw(); #endregion // We now must use casting to access the Draw() members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need the interface variable for later use. ((IDrawToPrinter)oct).Draw(); // Could also use the "is" keyword. if (oct is IDrawToMemory dtm) { dtm.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(); }