• C# 泛型相关.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Collections.Specialized;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList myList = new ArrayList(20);
                myList.Add(20);  //装箱操作.

                myList.Add(202);

                int i1 = (int)myList[0];  //拆箱操作

                foreach (int item in myList)
                { 
                    Console.WriteLine(item);  //这个程序是关于ArrayList的.
                }

                List<int> myListT = new List<int>(10);
                myListT.Add(412);    //不会装箱操作

                int i2 = myListT[0];   //不会拆箱.

                foreach (int item in myListT)
                {
                    Console.WriteLine(item);
                }
                //值得注意的是,如果在附值的时候有不同类型进去,但是用foreach循环的时候就会报错,
                //用泛型可以避免这种情况。在编译的时候就会报错.
               

              

            }
            public class MyOK  //下面是泛型的几个例子
            {
                public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);

                public delegate TOutput Converter<TOutput, TIntput>(TIntput from);

               
            }
            public class SortedList<Tkey, Tvalue>
            {
     
            }

        }

    }

    //////////////////////////////////////////////////////////////////////////////////////

    //以下是另外一个例子。关于List<T>的FindAll方法和Find//

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Collections.Specialized;

    namespace 泛型
    {
        public class Racer
        {
            private string name;

            public string Name
            {
                get { return name; }
               
            }
            private string car;

            public string Car
            {
                get { return car; }
              
            }

            public Racer(string name,string car)
            {
                this.name = name;
                this.car = car;

            }
            public override string ToString()
            {
                return string.Format("Name:{0,-20}Car:{1}", Name, Car);
            }
           
        }

        public class MainRun
        {
            static void Main()
            {
                List<Racer> racers = new List<Racer>();
                racers.Add(new Racer("张三","奥迪"));
                racers.Add(new Racer("李四", "烂车子"));

                //foreach (Racer item in racers)
                //{
                //    Console.WriteLine(item);
                //}
                FindRacer finder = new FindRacer("奥迪");

                foreach (Racer item in racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))  //这是泛型的List的FindAll.
                {
                    Console.WriteLine(item);
                }
               
              

            }

        }

        public class FindRacer
        {
            private string car;
            public FindRacer(string car)
            {
                this.car = car;

            }

            public bool DrivingCarPredicate(Racer racer)
            {
                return racer.Car == car;
            }
        }

    }

  • 相关阅读:
    SpringBoot+MyBatis通过ScriptRunner读取SQL文件
    Redis 分布式锁使用不当,酿成一个重大事故,超卖了100瓶飞天茅台!!!(转)
    better-scroll插件中导致fixed定位失效处理方便
    VUE SSR服务器端渲染NUXT采坑总结
    js的三种异步处理
    微信小程序支付功能讲解
    函数防抖与节流
    转:HTML5 History API 详解
    微信小程序 上拉刷新/下拉加载
    跨域你需要知道这些
  • 原文地址:https://www.cnblogs.com/fat_li/p/1834214.html
Copyright © 2020-2023  润新知