【发布时间】:2012-07-10 21:22:44
【问题描述】:
假设我有以下课程:
class Master {
int x;
int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
class Sub1:Master {
int z;
public int Z { get { return z; } set { z = value; } }
}
class Sub2:Master {
int w;
public int W { get { return w; } set { w = value; } }
}
class Sub3:Master {
int t;
public int T { get { return t; } set { t = value; } }
}
然后我定义了三个不同的数组,每个Sub*类型一个:
List<Sub1> array1;
List<Sub2> array2;
List<Sub3> array3;
最后,我需要一种以统一方式访问所有实例的方法。想法是使用一个新数组List<T>[] array4 = new[] {array1, array2, array3}; 并使用int 作为索引,因此我不必为属性X 和Y 编写三倍的常用操作。
但是,我不能这样做,因为三个数组的类型不同。我可以使用什么?
【问题讨论】:
-
你试过 List
[] masterList = new List []{array1, array2, array3};
标签: c# .net arrays generic-list