通过泛型查找不同类型数组中的值

实现效果:

      

知识运用:(泛型方法)

  

实现代码:

        public int Finder<T>(T[] arr, T item)   //定义泛型方法
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].Equals(item))
                    return i;       //找到返回索引
            }
            return -1;             //没有则返回-1
        }

  

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10088488.html