日期:2010-05-17  浏览次数:20454 次

1. 定义泛型类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
    class BList<T>
    {
        ArrayList arr = new ArrayList();

        public T this[int i]
        {
            get
            {
                return (T)arr[i];
            }
            set
            {
               arr.Add(value);
            }
        }

        public void Add(T p_obj)
        {
            arr.Add(p_obj);
        }

        public int Count
        {
            get
            {
                return arr.Count;
            }
        }

    }
}


2. 调用(放到任意的窗体事件中)
 private void button2_Click(object sender, EventArgs e)
        {

            BList<int> _list = new BList<int>(); // <int>里面可以替换任意已知类型
            for (int i = 0; i < 10; i++)
            {
                _list.Add( i);
            }

            for (int i = 0; i < _list.Count; i++)

                MessageBox.Show(_list[i].ToString());
        }