1.找出数组中最大的值 protected void Page_Load(object sender, EventArgs e) { int[] array ={ 1, 56, 78, 23, 124, 54, 98, 34, 96 }; int index; string Res = GetMax<int>(array, out index); Response.Write(Res); } /// <summary> /// 找出数组中最大的值 /// </summary> /// <typeparam name="T">数组类型</typeparam> /// <param name="Array">数组名称</param> /// <param name="index">最大值所对应的索引值</param> /// <returns></returns> public static string GetMax<T>(T[] Array, out int index) where T : IComparable { if (Array == null) { throw new ArgumentException("参数错误!"); } T Max = Array[0]; index = 1; for (int i = 0; i < Array.Length; i++) { if (Array[i].CompareTo(Max) > 0) { Max = Array[i]; index = i; } } return string.Format("索引为:{0},对应的最大值是:{1}", index, Max); } 2.找出字符串中出现最多的字符以及次数 protected void Page_Load(object sender, EventArgs e) { string str = "CSDN GOD!"; Response.Write(GetInfo(str)); } public static string GetInfo(string str) { Dictionary<char, int> d = new Dictionary<char, int>(); for (int i = 0; i < str.Length; i++) { if (d.ContainsKey(str[i])) { d[str[i]]++; } else { d[str[i]] = 1; } } char Max = str[0]; foreach (KeyValuePair<char, int> temp in d) { if (temp.Value > d[Max]) { Max = temp.Key; } } return string.Format("出现次数最多的值是:{0},次数是:{1}", Max, d[Max]); }