Adding a fucntion ref to delegate

R

Raj

How can I add a function reference to an existing delegate programmatically?
And also, how to add/remove??

Thank you

Regards
Raj
 
P

Peter Duniho

How can I add a function reference to an existing delegate
programmatically?

What are you asking? In C#, a "function reference" is essentially a
delegate itself. And you can combine delegates into new delegates with
the "+" operator or the Delegate.Combine() method.
And also, how to add/remove??

The "+" operator or Combine() method adds, the "-" operator or Remove()
method removes.

Pete
 
R

Raj

You can use + operator to add function reference only in sequence.

In case, if I need to add a function reference in between and
programmatically, is that still possible?
 
P

Peter Duniho

You can use + operator to add function reference only in sequence.

In case, if I need to add a function reference in between and
programmatically, is that still possible?

IMHO, if you feel a need for your delegates to be combined and thus
invoked in a particular order, a multicast delegate is probably the wrong
solution. It is probably better to implement your own ordered list of
delegates that manages that explicitly.

That said, it certainly can be done with only a delegate as the data
structure, if you are willing to write some extra code. The first step
would be to have some way of representing exactly where in the invocation
sequence you want the newly added delegate to appear. Then you would have
to enumerate the delegate's invocation list, adding each one in sequence
to a new delegate instance (or adding it to a plain List<Delegate>, which
you then later convert to an array that is passed to the
Delegate.Combine() overload that takes an array of delegate instances),
inserting the new delegate at the appropriate point.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

delegate instance 7
delegate signature 7
multicast delegate 3
ref parameter 15
Unregistering a Delegate 3
?? A Lambda Equivalent of this Delegate ?? 2
Why we should use delegate? 2
Questions About Delegates 6

Top