為了免code看不懂,可以用enum去指定array的index
ex:
cars[0] = .....
-> cars[(int)carNames.MrsOliver] = ....
好處是在遙遠的另一邊不用去猜0號車到底是MrsOliver還是MrsMarry
範例:
Schedule[] schedules = new int[Enum.GetNames(typeof(WeekDay)).Length];
enum WeekDay
{
sun = 0;
mon = 1;
tue = 2;
...
}
schedules[(int)weekDay.sun] = ....
當然也可以建立個key是enum type的 dictionary
Dictionary<WeekDay, Schedule> schedules = new Dictionary<WeekDay, Schedule>();
就可以直接存取
schedules[weekDay.sun] = ....
沒有留言:
張貼留言