V0ldek V0ldek - 6 months ago 23
C# Question

Adding functionality to already existing function (C#, C++)

Is there a way in C# to extend a function like following ?

void foo()
// function body, instructions


and then somewhere else in the code

foo += (x) => { // some functionality }

Which in practice would add the lambda functionality to be fired at the end of a

  • What would be the name and the internal details of such mechanism ?

  • Is something of this nature possible in C++?


Your example won't compile with the message :

error CS1656: Cannot assign to 'foo' because it is a 'method group'

It's not possible to do stuff like that. What you saw was probably operator += for events. Here's an example with a lambda.

Regarding C++

It's not possible to add a call to the end of a different function (the same as for C#). But you can implement events and overload operator += for your events to accept pointers to functions.