Нужно по массив делегатов должен пройтись по каждому студенту.
public delegate void DelegateAction();
struct Student {
int _Energy;
double _AverageMark;
string _Name, _Surname;
public string Name {
get => _Name;
set => _Name = value;
}
public string Surname {
get => _Surname;
set => _Surname = value;
}
public int Energy {
get => _Energy;
set => _Energy = value;
}
public double AverageMark {
get => _AverageMark;
set => _AverageMark = value;
}
public Student(string Name, string Surname, int Energy, double AverageMark) : this() {
this.Name = Name;
this.Surname = Surname;
this.Energy = Energy;
this.AverageMark = AverageMark;
}
public void Wash() { }
public void Feed() { ++this.Energy; }
public void PutSleep() { ++this.Energy; }
public void Grant() {
if (this.AverageMark > 4.5)
Console.WriteLine("{0}, you've just got a grant", this.Name + " " + this.Surname);
}
}
class Program {
static void Main() {
Student[] university = new Student[]{
new Student("Kyle", "Kyzma", 90, 4.5),
new Student("Kelly", "Oubre", 100, 4.7),
new Student("Andrew", "Wiggins", 95, 3.2),
};
//DelegateAction[] studentAction = new DelegateAction[] {
// university[0].Wash,
// university[0].Feed,
// university[0].PutSleep,
// university[0].Grant,
// university[1].Wash,
// university[1].Feed,
// university[1].PutSleep,
// university[1].Grant,
// university[2].Wash,
// university[2].Feed,
// university[2].PutSleep,
// university[2].Grant,
//};
DelegateAction[] studentAction = new DelegateAction[4];
for (int counterDelegates = 0; counterDelegates < studentAction.Length; counterDelegates++) {
for (int counterInstances = 0; counterInstances < university.Length; counterInstances++) {
studentAction[counterDelegates] = new DelegateAction(university[counterInstances].Feed).Invoke;
studentAction[counterDelegates] = new DelegateAction(university[counterInstances].Wash).Invoke;
studentAction[counterDelegates] = new DelegateAction(university[counterInstances].Grant).Invoke;
studentAction[counterDelegates] = new DelegateAction(university[counterInstances].PutSleep).Invoke;
}
}
}
}
Подскажите, насколько это правильно? Меня терзают сомнения по этому поводу. Не нравится цикл. Делегаты не вызываются(invoke). Возможно ли, вообще как-то сократить в цикле вызова методов?