Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class Foo
- {
- public virtual printMe()
- {
- Console.Writeline("FOO");
- }
- }
- Class Bar : Foo
- {
- public override printMe()
- {
- Console.Writeline("BAR");
- }
- }
- List<Foo> list = new List<Foo>();
- // then populate this list with various 'Bar' and other overriden Foos
- foreach (Foo foo in list)
- {
- foo.printMe(); // prints FOO.. Would like it to print BAR
- }
- class Foo
- {
- public virtual void virtualPrintMe()
- {
- nonVirtualPrintMe();
- }
- public void nonVirtualPrintMe()
- {
- Console.Writeline("FOO");
- }
- }
- class Bar : Foo
- {
- public override void virtualPrintMe()
- {
- Console.Writeline("BAR");
- }
- }
- List<Foo> list = new List<Foo>();
- // then populate this list with various 'Bar' and other overriden Foos
- foreach (Foo foo in list)
- {
- foo.virtualPrintMe(); // prints BAR or FOO
- foo.nonVirtualPrintMe(); // always prints FOO
- }
- foo.printMe(); // prints FOO.. Would like it to print BAR
- (Foo)foo.printMe(); // foo can be any derived class of Foo.
Add Comment
Please, Sign In to add comment