C#泛型

winform应用程序代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ArrayDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //简单数组
            string[] strs = { "aaa","bbbb","cccc","dddd"};
            for (int i = 0; i < strs.Length; i++)
            {
                cb_list1.Items.Add(strs[i]);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Array strs = new string[] {"111","222","333","444","555"};
            for (int i = 0; i < strs.Length; i++)
            {
                cb_list2.Items.Add(strs.GetValue(i));
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Collections.ArrayList all = new System.Collections.ArrayList() { 11, "aa", 33m, "bb" };
            for (int i = 0; i < all.Count; i++)
            {
                cb_list3.Items.Add(all[i].ToString());
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //方法一
            List<int> li1 = new List<int>();
            li1.Add(32);
            li1.Add(43);
            //方法二
            List<int> li2 = new List<int>{1,2,3,4,5,7};
            li2.Add(40);

            for (int i = 0; i < li2.Count; i++)
            {
                cb_list4.Items.Add(li2[i].ToString());
            }
        }
    }
}

 

效果如图:

1

泛型是具有占位符(数据类型)的类、结构、接口和方法。

运行速度会因为类型转换的次数减少而加快

List<T>类是ArrayList类的泛型等效类。该类使用大小可按照需要动态增加的数组实现IList<T>泛型接口。

索引为整数,从零开始。

posted on 2013-11-07 15:19  神秘藏宝室  阅读(413)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<