本帖最后由 brucel50 于 2013-8-17 16:42 编辑
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace test1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] ia1 = { 1, 2, 3, 4, 5 };
- string[] sa2 = { "1", "2", "3", "4", "5", "6" };
- int iwhere1 = Where<int>(ia1, 2);
- int swhere2 = Where<string>(sa2, "4");
- Console.WriteLine("{0}的位置是{1}",2,iwhere1);
- Console.WriteLine("{0}的位置是{1}","4",swhere2);
- Console.ReadKey();
- }
- /// <summary>
- /// 从一个数组中找出具体的位置
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="arrays"></param>
- /// <param name="array"></param>
- /// <returns></returns>
- static int Where<T>(T[] arrays, T array)
- {
- int itemp = 0;
- for (int i = 0; i < arrays.Length; i++)
- {
- if (arrays[i].ToString() == array.ToString())
- {
- itemp = i;
- }
- }
- return itemp;
- }
- }
- }
复制代码 |