zoukankan      html  css  js  c++  java
  • 泛型

    泛型>什么也泛型

    泛型就像Word里的模板,在Word模板中,提供了基本的文档编辑内容,在定义Word模板时,对具体编辑哪种类型的文档时未知的。

    泛型>一个泛型示例

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections.ObjectModel;

    namespace CollectionGeneric
    {
        
    /// <summary>
        
    /// 集合类的泛型定义
        
    /// </summary>
        
    /// <typeparam name="T">类型参数</typeparam>
        public class GenericCollections<T> : Collection<T>
        {
        }
        
    class Program
        {
            
    static void Main(string[] args)
            {
                GenericCollections
    <string> gcl = new GenericCollections<string>();
                gcl.Add(
    "这是一个字符串的示例");
                DisplayResult
    <string>(gcl);
                GenericCollections
    <bool> gcl2 = new GenericCollections<bool>();
                gcl2.Add(
    true);
                DisplayResult
    <bool>(gcl2);
                Console.ReadLine();
            }
            
    static void DisplayResult<T>(GenericCollections<T> gcl)
            {
                
    foreach (T s in gcl)
                {
                    Console.WriteLine(s.ToString());
                }
            }
        }
    }


    泛型>泛型结合类和非泛型集合类的对比

    ArrayList->List

    Hashtable->Dictionary

    Queue->Queue

    Stack->Stack

    SortedList->SortedList

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace GenericListDemo
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "List泛型集合类举例");
                List
    <string> ls = new List<string>();
                ls.Add(
    "泛型集合元素一");
                ls.Add(
    "泛型集合元素二");
                ls.Add(
    "泛型集合元素三");
                
    foreach (string s in ls)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine(
    "Dictinary泛型集合类举例");
                Dictionary
    <stringstring> dct = new Dictionary<stringstring>();
                dct.Add(
    "键一""值一");
                dct.Add(
    "键二""值二");
                dct.Add(
    "键三""值三");
                
    foreach (KeyValuePair<stringstring> kvp in dct)
                {
                    Console.WriteLine(
    "{0}:{1}",kvp.Key, kvp.Value);
                }
                Console.WriteLine(
    "Queue泛型集合类举例");
                Queue
    <string> que = new Queue<string>();
                que.Enqueue(
    "这是队列元素值一");
                que.Enqueue(
    "这是队列元素值二");
                
    foreach (string s in que)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine(
    "Stack泛型集合类举例");
                Stack
    <string> stack = new Stack<string>();
                stack.Push(
    "这是堆栈元素值一");
                stack.Push(
    "这是堆栈元素值二");
                
    foreach (string s in stack)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine(
    "SortedList泛型集合举例");
                SortedList
    <stringstring> sl = new SortedList<stringstring>();
                sl.Add(
    "key1""value1");
                sl.Add(
    "key4""value4");
                sl.Add(
    "key3""value3");
                sl.Add(
    "key2""value2");
                
    foreach (KeyValuePair<stringstring> kv in sl)
                {
                    Console.WriteLine(
    "{0}:{1}", kv.Key, kv.Value);
                }
                Console.ReadLine();
            }
        }
    }


    泛型>使用泛型的建议

    如果需要对多种类型进行相同的操作处理,则应该使用泛型类。

    如果需要处理值类型,则使用泛型可以避免装箱拆箱带来的性能开销。

    使用泛型可以在应用程序编译时发现类型错误,增强程序的健壮性。

    减少不必要的重复编码,使代码结构更加清晰。


    合乎自然而生生不息。。。
  • 相关阅读:
    mysql 分库分表
    深度学习(四)转--入门深度学习的一些开源代码
    深度学习(三)转-可视化理解卷积神经网络 直接查看卷积神经网络的过程特征
    深度学习(二)神经网络中的卷积和反卷积原理
    深度学习(一)神经网络中的池化与反池化原理
    转-------基于R-CNN的物体检测
    vs2013下c++调用python脚本函数 出现的一些问题总结
    关于mfc作为上位机接收硬件端USB或串口数据显示成图片 解决串口接收数据丢字节丢包问题
    转-------CNN图像相似度匹配 2-channel network
    深度学习(五)基于tensorflow实现简单卷积神经网络Lenet5
  • 原文地址:https://www.cnblogs.com/samwu/p/1849150.html
Copyright © 2011-2022 走看看