泛型<T>,是你肿莫了,还是我错了...
委托自定义数组排序 项目一共三个文件如下。
CSort.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 委托自定义数组排序 { class CSort<T> { public CSort() { } /// <summary> /// /// </summary> /// <param name="arrObj"></param> internal void Show(T[] arrObj) { foreach (T item in arrObj) { Console.Write(item.ToString() + " "); } } /// <summary> /// /// </summary> /// <param name="arrObj"></param> /// <param name="comp"></param> internal void Sort(T[] arrObj, Comp<T> comp) { for (int i = 0; i < arrObj.Length; i++) {///arrObj.Length-i-1:至数组的最后一个元素 for (int j = 0; j < arrObj.Length - i - 1; j++) { if (comp(arrObj[j], arrObj[j + 1]) > 0) { T obj = arrObj[j]; arrObj[j] = arrObj[j + 1]; arrObj[j + 1] = obj; } } } } /// <summary> /// /// </summary> /// <param name="one"></param> /// <param name="two"></param> /// <returns></returns> public int CompObj(T one, T two) { if (typeof(int) is T) { return int.Parse(one.ToString()) - int.Parse(two.ToString()); } if (typeof(string) is T) { return (one.ToString()).Length - (two.ToString()).Length; } //if (typeof(Person) is T) // return ((Person)one).Age - ((Person)two).Age; return 0; } //public int CompObj(string one, string two) //{ // return one.Length - two.Length; //} //public int CompObj(int one, int two) //{ // return one - two; //} } //class Person //{ // /// <summary> // /// 年龄 // /// </summary> // private int age; // /// <summary> // /// 年龄 // /// </summary> // public int Age // { // get { return age; } // set { age = value; } // } // /// <summary> // /// 名字 // /// </summary> // private string name; // /// <summary> // /// 名字 // /// </summary> // public string Name // { // get { return name; } // set { name = value; } // } // /// <summary> // /// 性别 // /// </summary> // private string sex; // /// <summary> // /// 性别 // /// </summary> // public string Sex // { // get { return sex; } // set { sex = value; } // } //} }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 委托自定义数组排序 { class Program { static void Main(string[] args) { int[] arrInt = new int[] { 1, 3, 1, 2, 3, 5, 6, 2, 4 }; string[] arrString = new string[] { "ab", "abc", "a", "abcd", "abc", "abcdef", "abcde" }; //Person[] arrPerson ={new Person(){Age=18,Name="ab",Sex=null}, // new Person(){Age=17,Name="a",Sex=null}}; CSort<string> s = new CSort<string>(); CSort<int> i = new CSort<int>(); //CSort<Person> p = new CSort<Person>(); s.Sort(arrString, s.CompObj); i.Sort(arrInt, i.CompObj); i.Show(arrInt); //p.Sort(arrPerson, p.CompObj); Console.WriteLine(); s.Show(arrString); //Type type2= arrInt.GetType(); //do //{ // type2 = type2.BaseType; ////Console.WriteLine(type2.ToString()); //} while (type2 != null); ////Console.WriteLine(arrInt.GetType().BaseType.BaseType.BaseType.ToString()); Console.ReadKey(); } } }
CSortDelegate.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 委托自定义数组排序 { public delegate int Comp<T>(T one, T two); }
运行没有错误!!!可是却也没有结果!!!
监视 T 却出现:
T “T”是一个“类型形参”,这在给定的上下文中无效
这是为什么???

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述