补讲: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: