I need to pass a instance (which will be created in this very moment) of a certain type to a method. This type offers several events which I'd like to subscribe to too, so my code looks like this:
var instance = new Instance();
instance.OnEvent1 += (sender, args) => {
DoThis();
DoThat();
}
instance.OnEvent2 += (sender, args) => DoThisToo();
instance.OnEvent3...
MyMethod(instance);
Now, is it possible to add the handlers during initialization? So I can write something like this:
MyMethod((MyType)instance => {
instance.OnEvent1 += (sender, args) => {
DoThis();
DoThat();
}
instance.OnEvent2...
});
This is, of course only desired because of cosmetic reasons. I like my code small & readable.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…