泛型的内容

补讲:C#中的泛型Generic       .net 2.0引入
 
   数组:
       缺点:数组一旦固定,将不能再更其大小
       优点:在数组中保存的元素往往具有特定类型
             在保存时以及取出时不需要做任何转换

   集合类:System.Collections.ArrayList
           System.Collections.Hashtable
           System.Collections.Stack
           System.Collections.Queue
      
       缺点:因为集合类需要以object的方式来保存所有元素,在取出
的时候也是以object的方式取出,所以涉及到转换。效率降低,拆箱可能
会带来转换异常。
       优点:可以不断向里追加元素

       System.Collections.Generic
一、C#中泛型集合类
    泛型就是让集合类能够以一种特定的数据类型来保存所有元素,在取出元素时,也是以相同特定的数据类型来取出元素。元素不再以object类型来保存,同时它也具有集合类的优点,可以不断向里追加元素。

1、System.Collections.Generic.Dictionary(字典泛型集合类)
   字典泛型集合类的特征是:元素可以使用任意类型做下标,值当然也任意类型。<TKey,TValue>
  
 Dictionary<string, int> dict = new Dictionary<string, int>();
 dict.Add("三星手机", 10);
 dict.Add("三星电视", 5);
 dict.Add("三星显示器", 6);
 dict.Remove("三星手机");
 foreach (KeyValuePair<string, int> o in dict)
 {
       Console.WriteLine(o.Key + "    " + o.Value);
 }

2、System.Collections.Generic.List(泛型列表集合类)

 List<double> list = new List<double>();
            list.Add(3.5);
            list.Add(6.7);
            list.Add(7.8);

            double sum = 0;
            for (int i = 0; i < list.Count; i++)
            {
                sum += list[i];
            }
            Console.WriteLine(sum);


3、System.Collections.Generic.Queue(队列泛型集合类)

   Queue<double> queue = new Queue<double>();
            queue.Enqueue(3.5);
            queue.Enqueue(31.5);
            queue.Enqueue(311.5);

            while (queue.Count > 0)
            {
               Console.WriteLine(  queue.Dequeue());

            }


4、System.Collections.Generic.Stack(堆栈泛型集合类)
    Stack<decimal> stack = new Stack<decimal>();
            stack.Push(3.5m);
            stack.Push(31.5m);
            stack.Push(311.5m);

            while (stack.Count > 0)
            {
               Console.WriteLine( stack.Pop());

            }


二、C#中自定义泛型(了解)
 
   自定义泛型类

   自定义泛型函数


   <>         类名后或函数名后

   泛型从本质上来理解是把___类型____当成了参数进行传递。
   代理从本质上来理解是把___函数____当成了参数进行传递。

   常量

   变量

   对象

   类型(基本类型  string object   class  struct enum  delegate)

   函数(Method)


 

补讲String对象的属性及方法:
1、.Length:长度
2、.EndsWith:
3、.StartsWith:
4、.IndexOf:-1
5、.LastIndexOf:-1
6、.Replace:
7、.Substring:
8、.ToLower:
9、.ToUpper:
10、.Trim:
11、.Split:
12、.Insert:
13、.Remove:

 

 

 

 

 

 

补讲:C#中的泛型Generic       .net 2.0引入
 
   数组:
       缺点:数组一旦固定,将不能再更其大小
       优点:在数组中保存的元素往往具有特定类型
             在保存时以及取出时不需要做任何转换

   集合类:System.Collections.ArrayList
           System.Collections.Hashtable
           System.Collections.Stack
           System.Collections.Queue
      
       缺点:因为集合类需要以object的方式来保存所有元素,在取出
的时候也是以object的方式取出,所以涉及到转换。效率降低,拆箱可能
会带来转换异常。
       优点:可以不断向里追加元素

       System.Collections.Generic
一、C#中泛型集合类
    泛型就是让集合类能够以一种特定的数据类型来保存所有元素,在取出元素时,也是以相同特定的数据类型来取出元素。元素不再以object类型来保存,同时它也具有集合类的优点,可以不断向里追加元素。

1、System.Collections.Generic.Dictionary(字典泛型集合类)
   字典泛型集合类的特征是:元素可以使用任意类型做下标,值当然也任意类型。<TKey,TValue>
  
 Dictionary<string, int> dict = new Dictionary<string, int>();
 dict.Add("三星手机", 10);
 dict.Add("三星电视", 5);
 dict.Add("三星显示器", 6);
 dict.Remove("三星手机");
 foreach (KeyValuePair<string, int> o in dict)
 {
       Console.WriteLine(o.Key + "    " + o.Value);
 }

2、System.Collections.Generic.List(泛型列表集合类)

 List<double> list = new List<double>();
            list.Add(3.5);
            list.Add(6.7);
            list.Add(7.8);

            double sum = 0;
            for (int i = 0; i < list.Count; i++)
            {
                sum += list[i];
            }
            Console.WriteLine(sum);


3、System.Collections.Generic.Queue(队列泛型集合类)

   Queue<double> queue = new Queue<double>();
            queue.Enqueue(3.5);
            queue.Enqueue(31.5);
            queue.Enqueue(311.5);

            while (queue.Count > 0)
            {
               Console.WriteLine(  queue.Dequeue());

            }


4、System.Collections.Generic.Stack(堆栈泛型集合类)
    Stack<decimal> stack = new Stack<decimal>();
            stack.Push(3.5m);
            stack.Push(31.5m);
            stack.Push(311.5m);

            while (stack.Count > 0)
            {
               Console.WriteLine( stack.Pop());

            }


二、C#中自定义泛型(了解)
 
   自定义泛型类

   自定义泛型函数


   <>         类名后或函数名后

   泛型从本质上来理解是把___类型____当成了参数进行传递。
   代理从本质上来理解是把___函数____当成了参数进行传递。

   常量

   变量

   对象

   类型(基本类型  string object   class  struct enum  delegate)

   函数(Method)


 

补讲String对象的属性及方法:
1、.Length:长度
2、.EndsWith:
3、.StartsWith:
4、.IndexOf:-1
5、.LastIndexOf:-1
6、.Replace:
7、.Substring:
8、.ToLower:
9、.ToUpper:
10、.Trim:
11、.Split:
12、.Insert:
13、.Remove:

 

 

 

 

 

 

 

 

 


 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值