code_junkie

C# Interface Implementation relationship is just “Can-Do” Relationship?

Nov 12th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. Foo myFoo = new Foo();
  2. return myFoo is IBar;
  3.  
  4. IBar bar = myArrayList[3] as IBar;
  5. Foo foo = bar as Foo;
  6.  
  7. void DoSomething(IBar bar) {
  8. }
  9.  
  10. static void Main() {
  11. Foo myFoo = new Foo();
  12. DoSomething(myFoo);
  13. }
  14.  
  15. interface IBar { void happy(); }
  16. class Foo : IBar
  17. {
  18. void happy()
  19. {
  20. Console.Write("OH MAN I AM SO HAPPY!");
  21. }
  22. }
  23. class Program
  24. {
  25. static void Main()
  26. {
  27. IBar myBar = new Foo();
  28. myBar.happy();
  29. }
  30. }
  31.  
  32. // Your class implements an interface
  33. class Concrete : ISomeInterface {}
  34.  
  35. // New up an instance of that class
  36. var myClass = new Concrete();
  37.  
  38. // You have a method somewhere that takes ISomeInterface as a parameter
  39. public void MyMethod(ISomeInterface blah){}
  40.  
  41. // You can pass your instance to that method
  42. MyMethod(myClass);
Add Comment
Please, Sign In to add comment