疯玩了两周了,今天是有关泛型的知识

 System.Collections.Generics命名空间中,包含大
量类和接口,其中的接口也都模拟了System.
Collections命名空间下相应的非泛型类型。如:
 ICollection<T>
 IComparer<T>
IDictionary<TKey,TValue>
IEnumerable<T>
IEnumerator<T>
IList<T>
泛型的定义:
public void Swap<T>(ref T a, ref T b)
    {
        T temp;
        temp = a;
        a = b;
        b = temp;
    }
一个泛型方法是在方法名称后、参数列表前定义
类型参数的。上面的方法中,可以操作任意两个
<T>类型的参数。
定义一个泛型类:
public class Point<T>{
    private T xPos;
    private T yPos;
    public Point(T x, T y) {
        xPos = x;
        yPos = y;
    }
    public T X{
        get { return xPos; }
        set { this.xPos = value; }
    }
    public T Y{
        get { return yPos; }
        set { this.yPos = value; }
    }
    public override string ToString()
    {
        return string.Format("x:{0};y:{1}"
, xPos, yPos);
    }
}
 
l泛型列表
List<T>
  泛型字典
前面学的散列表(Hashtable)是字典,散列表的泛型版本
是Dictionary<TKey,TValue>。
其它泛型集合Queue<T>和Stack<T>


        #region 泛型方法
        int a = 10;
        int b = 20;
        this.Swap(ref a, ref b);
        //Swap(ref a,ref b);
        Response.Write(string.Format("a:{0};b:{1}", a, b));
        #endregion

        #region 泛型类
        Point<int> p = new Point<int>(10, 20);
        Response.Write(p.ToString());
        Point<float> pf = new Point<float>(12.2f, 23.2f);
        Response.Write(pf.ToString());
        #endregion

        #region List<T>
        //List<Person> list = new List<Person>();
        //list.Add(new Person("张", "三"));
        //list.Add(new Person("李","四"));
        //list.Add(new Person("王", "五"));
       
        //Response.Write(list[0].FirstName + list[0].LastName);
        //Response.Write("<br/>-------------<br/>");
        //for (int i = 0; i < list.Count; i++)
        //{
        //    Response.Write(list[i].FirstName + list[i].LastName + "<br/>");
        //}
        #endregion

        #region dictionary<T>
        //Dictionary<string, Person> dic = new Dictionary<string, Person>();
        //dic.Add("zhang", new Person("张", "三"));
        //dic.Add("li", new Person("李", "四"));
        //dic.Add("wang", new Person("王", "五"));
        //Response.Write(dic["wang"].FirstName + dic["wang"].LastName+"<br/>");
        //foreach (string str in dic.Keys)
        //{
        //    Response.Write(str + "<br/>");
        //}
        //foreach (Person p in dic.Values)
        //{
        //    Response.Write(p.FirstName + p.LastName + "<br/>");
        //}
        #endregion

        Queue<int> qu = new Queue<int>();
        for (int i = 0; i < 10; i++){
            qu.Enqueue(i);
        }
        for (int i = 0; i < 10; i++){
            Response.Write(qu.Dequeue().ToString());
        }
        Response.Write("<br/>==================<br/>");
        Stack<int> st = new Stack<int>();
        for (int i = 0; i < 10; i++){
            st.Push(i);
        }
        for (int i = 0; i < 10; i++){
            Response.Write(st.Pop().ToString());
        }
    }
    //public T Add<T>(T a, T b)
    //{
    //    return a + b;
    //}
    public void Swap<T>(ref T a, ref T b)
    {
        T temp;
        temp = a;
        a = b;
        b = temp;
    }
    //public void Swap(ref int a, ref int b)
    //{
    //    int temp;
    //    temp = a;
    //    a = b;
    //    b = temp;
    //}
    //public void Swap(ref float a, ref float b)
    //{
    //    float temp;
    //    temp = a;
    //    a = b;
    //    b = temp;
    //}

集合和类表总结:
Array
ArrayList
Hashtable
SortedList
Queue
Stack
List<T>
Dictionary<TKey,TValue>
Queue<T>
Stack<T>
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值