OKIEWARDOYO

C#.NET No.8-2

Dec 5th, 2012
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. // versioning.cs
  2. // CS0114 expected
  3. public class MyBase
  4. {
  5.    public virtual string Meth1()
  6.    {
  7.       return "MyBase-Meth1";
  8.    }
  9.    public virtual string Meth2()
  10.    {
  11.       return "MyBase-Meth2";
  12.    }
  13.    public virtual string Meth3()
  14.    {
  15.       return "MyBase-Meth3";
  16.    }
  17. }
  18.  
  19. class MyDerived : MyBase
  20. {
  21.    // Overrides the virtual method Meth1 using the override keyword:
  22.    public override string Meth1()
  23.    {
  24.       return "MyDerived-Meth1";
  25.    }
  26.    // Explicitly hide the virtual method Meth2 using the new
  27.    // keyword:
  28.    public new string Meth2()
  29.    {
  30.       return "MyDerived-Meth2";
  31.    }
  32.    // Because no keyword is specified in the following declaration
  33.    // a warning will be issued to alert the programmer that
  34.    // the method hides the inherited member MyBase.Meth3():
  35.    public string Meth3()
  36.    {
  37.       return "MyDerived-Meth3";
  38.    }
  39.  
  40.    public static void Main()
  41.    {
  42.       MyDerived mD = new MyDerived();
  43.       MyBase mB = (MyBase) mD;
  44.  
  45.       System.Console.WriteLine(mB.Meth1());
  46.       System.Console.WriteLine(mB.Meth2());
  47.       System.Console.WriteLine(mB.Meth3());
  48.    }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment