泛型方法

 1 namespace CSharp2._0
 2 {
 3     public class Finder
 4     {
 5         //泛型方法
 6         public static int Find<T>(T[] items, T item)
 7         {
 8             for (int i = 0; i < items.Length; i++)
 9             {
10                 if (item.Equals(items[i]))
11                 {
12                     return i;
13                 }
14             }
15             return -1;
16         }
17     }
18     //耦合
19     class Program
20     {
21 
22         string sss;
23         List<string> ss;
24        
25         static void Main(string[] args)
26         {
27             NoGenericStatc nst = new NoGenericStatc(5);
28             nst.Push(1);
29             nst.Push(2);
30             nst.Push("你奶奶的");
31             nst.Push(3);
32             nst.Push("你妹的");
33 
34             Console.WriteLine(nst.Pop());
35             Console.WriteLine(nst.Pop());
36             Console.WriteLine(nst.Pop());
37 
38             GenericStac<int> intstac = new GenericStac<int>(4);
39             intstac.Push(1);
40             //intstac.Push("abc");
41 
42             GenericStac<Goods> gss = new GenericStac<Goods>(10);
43             gss.Push(new Goods());
44            
45             //可以做一个学生类,然后来查找某个同学。
46             string[] stuNames = new string[] {"abc","someone", "cba" };
47             int index = Finder.Find<string>(stuNames, "someone");
48 
49             if (index > -1)
50             {
51                 Console.WriteLine("someone在数组中的位置是:{0}", index);
52             }
53 
54             int[] ages = new int[] { 18, 17, 20, 69, 5 };
55 
56             int findNum = 89;
57             int indexB = Finder.Find<int>(ages, findNum);
58 
59             if (indexB > -1)
60             {
61                 Console.WriteLine("{0}在数组中的位置是:{1}",findNum, index);
62             }
63             else
64             {
65                 Console.WriteLine("{0}在数组并不存在", findNum);
66             }
67 
68             string[] names = new string[] { "a", "abc", "c" };
69             string findName = "c";
70 
71             int findPos = Finder.Find<string>(names, findName);
72 
73             Add(23, 32);
74         }
75 
76         //参数
77         static int Add(int a, int b)
78         {
79             return a + b;
80         }
81     }
82 }

 

posted @ 2018-11-10 08:50  冬夜的火  阅读(135)  评论(0编辑  收藏  举报