Advertisement
ivandrofly

New Vs Override ?

May 30th, 2014
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.
  2.  
  3. public class A
  4. {
  5.    public virtual void One();
  6.    public void Two();
  7. }
  8.  
  9. public class B : A
  10. {
  11.    public override void One();
  12.    public new void Two();
  13. }
  14.  
  15. B b = new B();
  16. A a = b as A;
  17.  
  18. a.One(); // Calls implementation in B
  19. a.Two(); // Calls implementation in A
  20. b.One(); // Calls implementation in B
  21. b.Two(); // Calls implementation in B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement