zoukankan      html  css  js  c++  java
  • 泛型<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”是一个“类型形参”,这在给定的上下文中无效

    这是为什么???

  • 相关阅读:
    引用与指针的区别联系
    单链表热点面试题
    C语言实现的单链表
    C语言实现的顺序表
    自己实现的库函数2(memset,memcmp,memcpy,memmove)
    自己实现的库函数1(strlen,strcpy,strcmp,strcat)
    Python文件练习_注册
    Python文件练习_自动生成密码文件
    Python文件练习_读取文件并计算平均分
    Python学习笔记七_文件读写
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/3518201.html
Copyright © 2011-2022 走看看