zoukankan      html  css  js  c++  java
  • c#泛型编程

    为什么要使用泛型:

    编写程序时,经常遇到两个模块功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们只能分类写多个方法去处理每个数据类型,因为方法的参数类型不同。

    上面的一种解决方法:如果我写一个object类型的类,它可以接收任何数据类型! <----缺点:当他处理值类型时,会出现装箱拆箱操作,这会导致托管堆上分配和回收了大量的变量,若数据量大,对性能损失非常严重;处理引用类型时,强制转换操作,也大大增加了处理器的负担。

    泛型就是在方法中传入通用的数据类型,调用它时再确定具体的数据类型是什么,这样就解决了相似代码的冗余......实现代码复用!<----他不涉及到数据类型转换,这是跟上面最大的区别,也是最本质的区别

    泛型:using System.Collections.Generic;

    它是一个通用类型,例如:List<...>,它就在上面这个命名空间中。

    List<int> intlist = new List<int>();
    intlist.Add(3);
    intlist.Add(4);
    intlist.ForEach((item) =>
    {
        Console.WriteLine(item);
    });
    
    List<string> stringlist = new List<string>();
    //....同上操作

    如上,List一套方法,可以操作多种数据类型的数据!!!

    class Program
    {
        static void Main(string[] args)
        {
            compare<int> c = new compare<int>();
            Console.WriteLine(c.Res(1, 5));
    
            compare<string> s = new compare<string>();
            Console.WriteLine(s.Res("asdf", "retre"));
    
            Console.ReadKey();
        }
    }
    
    public class compare<T> where T : IComparable
    {
        public T Res(T n1, T n2)
        {
            return n1.CompareTo(n2) > 0 ? n1 : n2;
        }
    }

     上面使用泛型(也使用了泛型约束,目的就是为了使用接口中的方法CompareTo)

    到底使用泛型和不使用泛型在执行时间上的优势:

    static void Main(string[] args)
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
    
        List<int> intlist = new List<int>();
        for (int i = 0; i < 10000000; i++)
        {
            intlist.Add(i);
        }
        stopwatch.Stop();
    
        TimeSpan t = stopwatch.Elapsed;
    
        string totaltime = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
        Console.WriteLine("使用泛型时,总用时:{0}", totaltime);//不到1s
    
        //----------------------------------------------------------------------------------//
    
        Console.WriteLine("正在计算.....");
        stopwatch.Start();
        ArrayList arr = new ArrayList();
        for (int i = 0; i < 10000000; i++)
        {
            arr.Add(i);
        }
        stopwatch.Stop();
    
        TimeSpan t2 = stopwatch.Elapsed;
    
        string totaltime2 = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", t2.Hours, t2.Minutes, t2.Seconds, t2.Milliseconds);
        Console.WriteLine("不使用泛型时,总用时:{0}", totaltime2);//2s多
    
        Console.ReadKey();
    }

     上面需要引入的命名空间:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
  • 相关阅读:
    PAT (Advanced Level) 1086. Tree Traversals Again (25)
    PAT (Advanced Level) 1085. Perfect Sequence (25)
    PAT (Advanced Level) 1084. Broken Keyboard (20)
    PAT (Advanced Level) 1083. List Grades (25)
    PAT (Advanced Level) 1082. Read Number in Chinese (25)
    HDU 4513 吉哥系列故事――完美队形II
    POJ Oulipo KMP 模板题
    POJ 3376 Finding Palindromes
    扩展KMP
    HDU 2289 Cup
  • 原文地址:https://www.cnblogs.com/stickcsharp/p/11133188.html
Copyright © 2011-2022 走看看