迭代集合中的对象并调用特定对象上的方法

时间:2014-05-20 15:00:00

标签: c# collections

我在列表中有一组对象。

基类为car,派生类为CompactTruckSUV

在我的基类上,我有一个名为Drive()的方法。

假设我的列表中有10辆车,而#3车是我想要运行Drive()功能的卡车。如何从列表中选择该特定对象并在其上运行该方法?

2 个答案:

答案 0 :(得分:1)

List<Car> carList;
// fill List with values
carList[2].Drive();

答案 1 :(得分:1)

var cars = new List<Car>();
// Add cars....
car[2].Drive();