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.
var instance = new Instance { OnEvent1 += ... }
? If so, the answer is that you'll be able to do it in C# 6... – Defective