需求:定义一个DateTime列表
一般方式:
List<DateTime> dt = new List<DateTime>();
上面的做法,由于用到<>符号,但当用到太多的<>符号时,阅读代码会感到很麻烦,不直观。可采用以下实现
较差的方式:
定义类:class DateTimeList:List<DateTime>{}
DateTimeList dt = new DateTimeList();
缺点:
执行 typeof(List<DateTime>) == (typeof(DateTimeList)) 此操作为false。 说明这是两个不同的类。因此当一个方法的参数为List<DateTime>时,可以传入DateTimeList对象;反过来则会出错。这做法会让开发者糊涂。
较好的方式:
利用源代码文件的顶部使用传统的using命令
using DateTimeList = System.Collections.Generic.List<System.DateTime>.
此时 typeof(List<DateTime>) == (typeof(DateTimeList)) 此操作为true。因此,可以解决上面“较差的方式”出现的缺点。