Item 23: Understand How Interface Methods Differ from Virtual Methods(Effective C#)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EffectiveCSharpItem23
7 {
8 interface IMsg
9 {
10 void Message();
11 }
12
13 public class MyClass : IMsg
14 {
15 public void Message()
16 {
17 Console.WriteLine("MyClass");
18 }
19 }
20
21 public class MyDerivedClass : MyClass, IMsg
22 {
23 public new void Message()
24 {
25 Console.WriteLine("MyDerivedClass");
26 }
27 }
28
29 class Program
30 {
31 static void Main(string[] args)
32 {
33 MyDerivedClass d = new MyDerivedClass();
34 d.Message();
35 IMsg m = d as IMsg;
36 m.Message();
37
38 MyClass c = d as MyClass;
39 c.Message();
40 }
41 }
42 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EffectiveCSharpItem23
7 {
8 interface IMsg
9 {
10 void Message();
11 }
12
13 public class MyClass : IMsg
14 {
15 public void Message()
16 {
17 Console.WriteLine("MyClass");
18 }
19 }
20
21 public class MyDerivedClass : MyClass, IMsg
22 {
23 public new void Message()
24 {
25 Console.WriteLine("MyDerivedClass");
26 }
27 }
28
29 class Program
30 {
31 static void Main(string[] args)
32 {
33 MyDerivedClass d = new MyDerivedClass();
34 d.Message();
35 IMsg m = d as IMsg;
36 m.Message();
37
38 MyClass c = d as MyClass;
39 c.Message();
40 }
41 }
42 }