本文会讨论泛型接口、泛型类、泛型方法、泛型约束,以及两种常用的泛型集合类型List<T>和Dictionary<K, V>。
泛型(generic)是指通过相同的代码处理不同类型的数据,如交换两个对象(变量)的值,下面的代码(cfx/Comm.cs)演示了交换数据的泛型方法。
using System; namespace cfx { public static class Comm { // public static void Swap<T>(ref T x, ref T y) { T tmp = x; x = y; y = tmp; } } }
与普通方法不同的是,泛型方法会在方法名后面使用一对尖括号指定类型标识,多个类型时使用逗号(,)分隔。代码中定义的Swap()方法只使用一个泛型类型,类型标识为T,在参数、返回值和方法体中可以使用此类型标识定义参数和变量(对象)。Swap()方法的两个参数都定义为按引用传递,方法体中交换了两个参数的数据。下面的代码(Program.cs),在Main()方法中测试Swap()方法的使用。
using System; using cfx; namespace csfx_demo { class Program { static void Main(string[] args) { int x = 10, y = 99; Console.WriteLine("x={0}, y={1}", x, y); Comm.Swap(ref x, ref y); Console.WriteLine("x={0}, y={1}", x, y); } } }
本例会使用Comm.Swap()方法交换x和y的值,执行结果如下图所示。
这里定义的Comm.Swap()泛型方法可交换两个相同类型的变量或对象的数据,代码中可以修改变量x和y的类型和数据,并观察代码执行结果。
泛型接口需要泛型类来实现,与泛型方法的定义类似,在泛型接口和泛型类的名称后需要使用一对尖括号定义泛型的类型标识,多个类型标识使用逗号分隔。下面的代码演示了泛型接口和泛型类的基本应用。
using System; namespace csfx_demo { // interface I1<T> { T Value { get; set; } string ToString(); } // class C1<T> : I1<T> { public T Value { get; set; } // new public string ToString() { return Value.ToString(); } } // class Program { static void Main(string[] args) { C1<string> s = new C1<string>(); s.Value = "abc"; Console.WriteLine(s.Value); // C1<int> n = new C1<int>(); n.Value = 99; Console.WriteLine(n.ToString()); } } }
代码中,首先定义了泛型接口I1,使用了一个泛型类型标识T,接口中定义了Value属性和ToString()方法,其中,Value属性的类型为T,ToString()方法的返回值为string类型。接下来使用C1泛型类实现了I1接口,其中的ToString()使用new关键字覆盖了Object类中的ToString()方法,并返回Value属性的ToString()方法返回值。Main()方法中,首先使用C1<string>类型,即Value属性为string类型,然后使用C1<int>类型,即Value属性为int类型。执行代码会显示abc和99。
普通的泛型类型中,类型标识可以表示任何类型,但有时,可能需要对泛型的类型范围做一些限制,此时就需要使用泛型约束。定义泛型类型的约束时,应在泛型标识后使用where关键字指定泛型类型应实现的接口,如下面的代码。
using System; namespace csfx_demo { // interface IFactory { string GetValue(); } // class Factory1 : IFactory { public string GetValue() { return "工作A"; } } // class Factory2 : IFactory { public string GetValue() { return "工作B"; } } // class C1<T> where T: IFactory { public T Factory { get; set; } // new public string ToString() { return Factory.GetValue(); } } // class Program { static void Main(string[] args) { C1<Factory1> c1 = new C1<Factory1>(); c1.Factory = new Factory1(); Console.WriteLine(c1.ToString()); // C1<Factory2> c2 = new C1<Factory2>(); c2.Factory = new Factory2(); Console.WriteLine(c2.ToString()); } } }
代码中,首先定义了IFactory接口,其中只包含GetValue()方法,然后,使用Factory1和Factory2两个类实现了此接口。接下来的C1类定义为泛型类,在约束中指定泛型类型必须实现IFactory接口,类中定义了Factory属性,在ToString()方法中则调用了IFactory接口中的GetValue()方法返回信息。Main()方法中,分别定义了C1<Factory1>和C1<Factory2>类型对象c1和c2,两个对象的Factory属性分别设置为Factory1和Factory2类型,然后显示对象的ToString()方法返回的信息,执行结果会显示“工作A”和“工作B”。
下面了解两种常用的泛型集合类型,List<T>和Dictionary<K, V>泛型类。
前面的文章中讨论过数组的应用,早期的.NET Framework类库中还定义了ArrayList类动态处理数组,其元素为object类型;当.NET Framework环境引入泛型支持后,使用List<T>泛型类可以更好地处理数据集合。下面的代码,先来看List<T>类的基本应用。
using System; using System.Collections.Generic; namespace csfx_demo { class Program { static void Main(string[] args) { List<int> lst = new List<int>(); lst.Add(1); lst.AddRange(new int[] { 1, 2, 3, 8 }); lst.Insert(4, 5); Console.WriteLine(lst[2]); // 2 Console.WriteLine(string.Join(",", lst)); } } }
代码中,首先创建了List<int>对象,其中,列表元素的类型为int,接下来使用三个方法添加列表元素,分别是:
接下来,第一个输出中使用索引显示第3个元素(索引2)。第二个输出使用string.Join()方法将列表元素转换为使用逗号连接的字符串,显示结果为“1,1,2,3,5,8”。
下面是List<T>泛型类的一些常用成员。
下面是一些根据条件操作的方法。
下面的代码会使用FindAll()方法查找列表元素中的所有偶数。
using System; using System.Collections.Generic; namespace csfx_demo { class Program { static void Main(string[] args) { List<int> lst = new List<int>( new int[] { 1, 2, 3, 5, 6, 7, 8 }); List<int> lstEven = lst.FindAll(x => x % 2 == 0); foreach (int n in lstEven) { Console.WriteLine(n); } } } }
执行代码会分行显示2、6、8。
ForEach()方法通过委托对所有元素执行相同的操作,如下面的代码会显示所有元素的平方。
using System; using System.Collections.Generic; namespace csfx_demo { class Program { static void Main(string[] args) { List<int> lst = new List<int>(new int[] { 1, 2, 3, 4, 5 }); lst.ForEach(x => Console.WriteLine(x * x)); } } }
执行代码会分行显示1、4、9、16、25。
ConvertAll()方法,将列表中所有元素转换类型,并返回新类型元素组成的列表对象,参数需要指定转换方法,如下面的代码。
using System; using System.Collections.Generic; namespace csfx_demo { class Program { static void Main(string[] args) { List<string> lstStr = new List<string>( new string[] { "1", "2", "3", "4", "5" }); List<int> lstInt = lstStr.ConvertAll((s) => int.Parse(s)); foreach(int n in lstInt) { Console.WriteLine(n); } } } }
代码中,使用int.Parse()方法将lstStr列表中的所有元素转换为int类型,并返回由新类型元素组成的lstInt列表对象,最后,使用foreach语句访问lstInt对象的元素,会分行显示元素1、2、3、4、5。
字典是一种“键/值”结构的集合类型,在.NET Framework中可以使用Dictionary<K,V>泛型类处理。在字典中,每个元素都由“键”和“值”组成,其中,“键”是元素的名称,在一个字典中不能重复,也不能为null;“值”则是对应的元素数据。下面的代码演示了字典的基本操作。
using System; using System.Collections.Generic; namespace csfx_demo { class Program { static void Main(string[] args) { Dictionary<string, string> d = new Dictionary<string, string>(); d.Add("earth", "地球"); d["mars"] = "火"; d["mars"] = "火星"; Console.WriteLine(d["earth"]); // 地球 foreach (string k in d.Keys) { Console.WriteLine("{0} : {1}", k, d[k]); } } } }
代码执行结果如下图所示。
代码中,首先定义了Dictionary<string,string>对象,其中,第一个string为“键”的类型,第二个string是“值”的类型,也就是说,字典元素的名称和数据都是字符串类型。
接下来,使用Add()方法添加字典元素,参数一指定元素名称,参数二指定元素数据。使用索引形式指定元素数据时,当“键”不存在时会添加元素,当“键”存在时会修改元素的数据。
第一个输出中,通过“键”读取了“earth”对应的数据,显示为“地球”。需要注意的是,使用索引形式读取元素数据时,如果“键”不存在则会产生KeyNotFoundException异常。
最后,使用foreach语句结构依次访问“键”,并通过"键 : 值"格式显示元素。
下面是Dictionary泛型类的常用成员。
下面的代码演示了字典的一些操作。
using System; using System.Collections.Generic; namespace csfx_demo { class Program { static void Main(string[] args) { Dictionary<string, string> d = new Dictionary<string, string> { { "earth", "地球"}, { "mars", "火星" }, { "jupiter", "木星" } }; Console.WriteLine(d.ContainsKey("mars")); // True Console.WriteLine(d.ContainsValue("木星")); // True Console.WriteLine(d.ContainsKey("saturn")); // False Console.WriteLine(d.ContainsValue("土星")); // False // string key = "earth"; string value; if (d.TryGetValue("earth", out value)) { Console.WriteLine("{0} : {1}", key, value); } else { Console.WriteLine("{0}键不存在", key); } } } }
代码中,首先定义了字典对象d,并直接通过字典结构指定元素名称和数据,格式为{{key1,value1},{key2,value2},{key3,value3},...}。
接下来,测试了不同的“键”和“值”是否存在。最后,通过TryGetValue()方法读取元素的数据。代码执行结果如下图所示,这里,可以修改变量key的值来观察执行结果。