for training purposes I am following along a Tutorial written for Java, which I successfully \'translated\' into C# so far, however, I am facing now a problem for which I don\'t
There are no anonymous classes in C#, but you have two ways of achieving the same result:
The first approach is self-explanatory, but the code would be quite a bit longer. The named classes should be OK, because they are private to the implementation of your publicly visible class.
The second approach could look as follows:
public class LevelUpOption {
private String name;
public String name() { return name; }
public LevelUpOption(String name, Action<Creature> invoke){
this.name = name;
this.invoke = invoke;
}
public readonly Action<Creature> invoke;
}
Now you can initialize your array like this:
private static LevelUpOption[] options = new [] {
new LevelUpOption("Increased hit points", c => c.gainMaxHp() ),
new LevelUpOption("Increased attack value", c => c.gainAttackValue()),
...
};
Since invoke
is a delegate, the syntax of calling it is the same:
options[i].invoke(myCreature);