泛型>什么也泛型
泛型就像Word里的模板,在Word模板中,提供了基本的文档编辑内容,在定义Word模板时,对具体编辑哪种类型的文档时未知的。
泛型>一个泛型示例
代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
namespace CollectionGeneric
{
/// <summary>
/// 集合类的泛型定义
/// </summary>
/// <typeparam name="T">类型参数</typeparam>
public class GenericCollections<T> : Collection<T>
{
}
class Program
{
static void Main(string[] args)
{
GenericCollections<string> gcl = new GenericCollections<string>();
gcl.Add("这是一个字符串的示例");
DisplayResult<string>(gcl);
GenericCollections<bool> gcl2 = new GenericCollections<bool>();
gcl2.Add(true);
DisplayResult<bool>(gcl2);
Console.ReadLine();
}
static void DisplayResult<T>(GenericCollections<T> gcl)
{
foreach (T s in gcl)
{
Console.WriteLine(s.ToString());
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
namespace CollectionGeneric
{
/// <summary>
/// 集合类的泛型定义
/// </summary>
/// <typeparam name="T">类型参数</typeparam>
public class GenericCollections<T> : Collection<T>
{
}
class Program
{
static void Main(string[] args)
{
GenericCollections<string> gcl = new GenericCollections<string>();
gcl.Add("这是一个字符串的示例");
DisplayResult<string>(gcl);
GenericCollections<bool> gcl2 = new GenericCollections<bool>();
gcl2.Add(true);
DisplayResult<bool>(gcl2);
Console.ReadLine();
}
static void DisplayResult<T>(GenericCollections<T> gcl)
{
foreach (T s in gcl)
{
Console.WriteLine(s.ToString());
}
}
}
}
泛型>泛型结合类和非泛型集合类的对比
ArrayList->List
Hashtable->Dictionary
Queue->Queue
Stack->Stack
SortedList->SortedList
代码
using System;
using System.Collections.Generic;
using System.Text;
namespace GenericListDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("List泛型集合类举例");
List<string> ls = new List<string>();
ls.Add("泛型集合元素一");
ls.Add("泛型集合元素二");
ls.Add("泛型集合元素三");
foreach (string s in ls)
{
Console.WriteLine(s);
}
Console.WriteLine("Dictinary泛型集合类举例");
Dictionary<string, string> dct = new Dictionary<string, string>();
dct.Add("键一", "值一");
dct.Add("键二", "值二");
dct.Add("键三", "值三");
foreach (KeyValuePair<string, string> kvp in dct)
{
Console.WriteLine("{0}:{1}",kvp.Key, kvp.Value);
}
Console.WriteLine("Queue泛型集合类举例");
Queue<string> que = new Queue<string>();
que.Enqueue("这是队列元素值一");
que.Enqueue("这是队列元素值二");
foreach (string s in que)
{
Console.WriteLine(s);
}
Console.WriteLine("Stack泛型集合类举例");
Stack<string> stack = new Stack<string>();
stack.Push("这是堆栈元素值一");
stack.Push("这是堆栈元素值二");
foreach (string s in stack)
{
Console.WriteLine(s);
}
Console.WriteLine("SortedList泛型集合举例");
SortedList<string, string> sl = new SortedList<string, string>();
sl.Add("key1", "value1");
sl.Add("key4", "value4");
sl.Add("key3", "value3");
sl.Add("key2", "value2");
foreach (KeyValuePair<string, string> kv in sl)
{
Console.WriteLine("{0}:{1}", kv.Key, kv.Value);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace GenericListDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("List泛型集合类举例");
List<string> ls = new List<string>();
ls.Add("泛型集合元素一");
ls.Add("泛型集合元素二");
ls.Add("泛型集合元素三");
foreach (string s in ls)
{
Console.WriteLine(s);
}
Console.WriteLine("Dictinary泛型集合类举例");
Dictionary<string, string> dct = new Dictionary<string, string>();
dct.Add("键一", "值一");
dct.Add("键二", "值二");
dct.Add("键三", "值三");
foreach (KeyValuePair<string, string> kvp in dct)
{
Console.WriteLine("{0}:{1}",kvp.Key, kvp.Value);
}
Console.WriteLine("Queue泛型集合类举例");
Queue<string> que = new Queue<string>();
que.Enqueue("这是队列元素值一");
que.Enqueue("这是队列元素值二");
foreach (string s in que)
{
Console.WriteLine(s);
}
Console.WriteLine("Stack泛型集合类举例");
Stack<string> stack = new Stack<string>();
stack.Push("这是堆栈元素值一");
stack.Push("这是堆栈元素值二");
foreach (string s in stack)
{
Console.WriteLine(s);
}
Console.WriteLine("SortedList泛型集合举例");
SortedList<string, string> sl = new SortedList<string, string>();
sl.Add("key1", "value1");
sl.Add("key4", "value4");
sl.Add("key3", "value3");
sl.Add("key2", "value2");
foreach (KeyValuePair<string, string> kv in sl)
{
Console.WriteLine("{0}:{1}", kv.Key, kv.Value);
}
Console.ReadLine();
}
}
}
泛型>使用泛型的建议
如果需要对多种类型进行相同的操作处理,则应该使用泛型类。
如果需要处理值类型,则使用泛型可以避免装箱拆箱带来的性能开销。
使用泛型可以在应用程序编译时发现类型错误,增强程序的健壮性。
减少不必要的重复编码,使代码结构更加清晰。