Created
November 15, 2018 03:51
-
-
Save csharpforevermore/1e6eda94c3d79dd72c3c4d4f0fee768f to your computer and use it in GitHub Desktop.
An example of the keywords virtual and sealed
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
public class ClassA | |
{ | |
public virtual void Method1() { Console.WriteLine("ClassA"); } | |
} | |
public class ClassB : ClassA | |
{ | |
public override sealed void Method1() { Console.WriteLine("ClassB"); } | |
public void Method2() { Console.WriteLine("ClassB"); } | |
} | |
public class ClassC : ClassB | |
{ | |
public override void Method1() { Console.WriteLine("ClassC"); } | |
public override void Method2() { Console.WriteLine("ClassC"); } | |
} |
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
public class ClassA | |
{ | |
public virtual void Method1() { Console.WriteLine("ClassA"); } | |
} | |
public class ClassB : ClassA | |
{ | |
public override sealed void Method1() { Console.WriteLine("ClassB"); } | |
public void Method2() { Console.WriteLine("ClassB"); } | |
} | |
public class ClassC : ClassB | |
{ | |
new public void Method1() { Console.WriteLine("ClassC"); } | |
new public void Method2() { Console.WriteLine("ClassC"); } | |
} |
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
public void Run() | |
{ | |
ClassC classC = new ClassC(); | |
ClassB classB = classC; | |
ClassA classA = classC; | |
classC.Method1(); | |
classB.Method1(); | |
classA.Method1(); | |
classC.Method2(); | |
classB.Method2(); | |
} |
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
public class ClassA | |
{ | |
public virtual void Method1() { Console.WriteLine("ClassA"); } | |
} | |
public class ClassB : ClassA | |
{ | |
new public void Method1() { Console.WriteLine("ClassB"); } | |
public void Method2() { Console.WriteLine("ClassB"); } | |
} | |
public class ClassC : ClassB | |
{ | |
new public void Method1() { Console.WriteLine("ClassC"); } | |
new public void Method2() { Console.WriteLine("ClassC"); } | |
} |
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
ClassC | |
ClassB | |
ClassB | |
ClassC | |
ClassB |
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
Now, when you try to compile this, you'll get not one, but two compiler errors: | |
'MyNamespace.ClassC.Method1()': cannot override inherited member 'GetPropertiesTest.ClassB.Method1()' because it is sealed | |
'MyNamespace.ClassC.Method2()': cannot override inherited member 'GetPropertiesTest.ClassB.Method2()' because it is not marked virtual, abstract, or override | |
Either way, you can't override the methods. However, you CAN implement a new Method1 and Method2: |
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
This compiles just fine. When using the "new" keyword, you are essentially saying "These methods have nothing to do with the methods that are inherited. I just really want to implement methods that use the same name." | |
So here's how they would execute. Let's use this method |
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
We'll get the following output: | |
ClassC | |
ClassB | |
ClassA | |
ClassC | |
ClassB | |
Hopefully this will clear some stuff up. | |
David Morton - http://blog.davemorton.net/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from https://social.msdn.microsoft.com/Forums/vstudio/en-US/58888c86-47bc-4f70-a01f-c5441a7f77da/nonoverridable-method?forum=csharpgeneral