Ray's playground

 

Item 34: Avoid Overloading Methods Defined in Base Classes(Effective C#)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace EffectiveCSharpItem34
 7 {
 8     public class B2 { }
 9     public class D2 : B2 { }
10 
11     public class B
12     {
13         public void Foo(D2 parm)
14         {
15             Console.WriteLine("In B.Foo");
16         }
17         public void Bar(B2 parm)
18         {
19             Console.WriteLine("In B.Bar");
20         }
21         public void Foo2(IEnumerable<D2> parm)
22         {
23             Console.WriteLine("In B.Foo2");
24         }
25     }
26 
27     public class D : B
28     {
29         public void Foo(B2 parm)
30         {
31             Console.WriteLine("In D.Foo");
32         }
33         public void Bar(B2 parm1, B2 parm2 = null)
34         {
35             Console.WriteLine("In D.Bar");
36         }
37         public void Foo2(IEnumerable<B2> parm)
38         {
39             Console.WriteLine("In D.Foo2");
40         }
41     }
42 
43     class Program
44     {
45         static void Main(string[] args)
46         {
47             var obj1 = new D();
48             obj1.Bar(new D2());
49 
50             var obj2 = new D();
51             obj2.Foo(new D2());
52             obj2.Foo(new B2());
53 
54             B obj3 = new D();
55             obj3.Foo(new D2());
56 
57             var obj4 = new D();
58             ((B)obj4).Foo(new D2());
59             obj4.Foo(new B2());
60 
61             var sequence = new List<D2> { new D2(), new D2() };
62             var obj5 = new D();
63             obj2.Foo2(sequence);
64 
65             Console.Read();
66         }
67     }
68 }

posted on 2011-02-23 20:08  Ray Z  阅读(180)  评论(0编辑  收藏  举报

导航