Created
January 6, 2023 10:16
-
-
Save jskeet/171df7bb64042855f4b3bab3b62e9c9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Math | |
{ | |
public virtual int Sum(int a , int b) | |
{ | |
return a+b; | |
} | |
} | |
public class Class1 : Math | |
{ | |
public int Sum(int a, int b) | |
{ | |
return a + b + 10; | |
} | |
} | |
public class Class2 : Math | |
{ | |
public override int Sum(int a, int b) | |
{ | |
return a + b + 10; | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
Math math1 = new Class1(); | |
Math math2 = new Class2(); | |
Console.WriteLine(math1.Sum(5, 3)); // Prints 8 | |
Console.WriteLine(math2.Sum(5, 3)); // Prints 18 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks Jon Skeet I get your Idea that If we write the same method as base class the compiler ignore the derived class . thanks a lot and I delete my question