zoukankan      html  css  js  c++  java
  • 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;
            }
        }

    }

  • 相关阅读:
    python float转为decimal
    python 断言大全
    python如何判断一个字符串是中文,还是英文。
    分享:selenium(一) xpath
    接口测试——带token请求post接口(postman学习)
    git stash命令
    我的爹娘(一)
    appium自动化测试 环境搭建
    linux下的定时任务
    php面向对象3
  • 原文地址:https://www.cnblogs.com/fat_li/p/1834214.html
Copyright © 2011-2022 走看看