• 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;
            }
        }

    }

  • 相关阅读:
    .Net WebApi接口之Swagger UI 隐藏指定接口类或方法
    .Net WebApi接口之Swagger集成详解
    SVN服务器的本地搭建和使用
    MemCache在.NET中使用Memcached.ClientLibrary详解
    Memcache分布式锁
    MemCache可视化客户端管理及监控工具TreeNMS
    Redis可视化客户端管理及监控工具TreeNMS
    MemCache服务安装配置及windows下修改端口号
    MemCache在win7上的可视化配置以及Nodejs/Net应用
    Redis分布式锁
  • 原文地址:https://www.cnblogs.com/fat_li/p/1834214.html
Copyright © 2020-2023  润新知