Created
July 6, 2012 05:46
-
-
Save ledpup/3058298 to your computer and use it in GitHub Desktop.
Demonstrates the use of virtual, override, sealed and new keywords on methods.
This file contains 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 Base | |
{ | |
public virtual void Method() | |
{ | |
Console.WriteLine("Base (can be overridden.)"); | |
} | |
public void CantOverride() | |
{ | |
Console.WriteLine("Can't be overridden (but can be newed)."); | |
} | |
} | |
public class Overrider : Base | |
{ | |
public override void Method() | |
{ | |
Console.WriteLine("Override (can be overridden.)"); | |
} | |
public new void CantOverride() | |
{ | |
Console.WriteLine("Can't be overridden (but can be newed)."); | |
} | |
} | |
public class SealedOverrider : Overrider | |
{ | |
public sealed override void Method() | |
{ | |
Console.WriteLine("Sealed override (can't be overridden.)"); | |
} | |
} | |
public class Newer : SealedOverrider | |
{ | |
public new void Method() | |
{ | |
Console.WriteLine("New"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment