索引

 1     class Program
 2     {
 3 
 4         static void Main()
 5         {
 6             var pair = new Pair<int>(10,12);
 7 
 8             Console.WriteLine($"this is {pair.First} and {pair[PairItem.Second]} other {pair[0]}");
 9             Console.ReadKey();
10         }
11     }
12 
13     interface IPair<T>
14     {
15         T First { get; }
16         T Second { get; }
17         T this[PairItem index] { get; }
18     }
19 
20     internal enum PairItem
21     {
22         First,
23         Second
24     }
25 
26     internal struct Pair<T> : IPair<T>
27     {
28         public Pair(T first,T second)
29         {
30             First = first;
31             Second = second;
32         }
33 
34         public T this[PairItem index]
35         {
36             get
37             {
38                 switch (index)
39                 {
40                     case PairItem.First:
41                         return First;
42                     case PairItem.Second:
43                         return Second;
44                     default:
45                         throw new NotImplementedException();
46                 }
47             }
48             set
49             {
50                 switch (index)
51                 {
52                     case PairItem.First:
53                         First = value;
54                         break;
55                     case PairItem.Second:
56                         Second = value;
57                         break;
58                     default:
59                         throw new NotImplementedException();
60                 }
61             }
62         }
63 
64         public T First{ get; private set; }
65 
66         public T Second { get; private set; }
67     }
View Code

 

 

重点在于这个

 

posted on 2018-05-03 14:15  不会非的猪  阅读(120)  评论(0编辑  收藏  举报

导航