Delegates are useful, and here is why (sample program)

J

Jon Skeet [C# MVP]

raylopez99 said:
Jon Skeet--here is the code. I think the problem is much more basic:
I don't know how different forms exist in the namespace. See the
comment at "***" below. I assumed they shared the same classes, but
they don't.

I've just spotted something I hadn't seen before, which makes my sample
code somewhat less useful. You'd said that Form2 derived from Form1 -
but in your code, they both just derive from Form.
 
J

Jon Skeet [C# MVP]

Author said:
Thank you very much. I think this Find example is enlightening. Most
of the time, we see delegate examples that are related to event
handling or they are for delegate's sake, giving us a false impression
that delegates are for event handling only.

Not only that, but people also get confused as to what an event
actually is. It's just a pair of methods - add and remove - linked by
some metadata.
I think the delegate philosophy seems to be clear now: We use a
delegate

1. when we don't know or it is impossible for us to know how a
certain part of an application is to be implemented, or

2. we know of a bunch of different potential ways to approach it, but
instead of trying to implement all possibilities and cluttering our
code with switches or ifs and maybe additional argument into the
method signature, we give (hence delegate) the concrete task to the
consumer class.

Yup. And of course 2 is just a special case of 1, really.
 
A

Author

Not only that, but people also get confused as to what an event
actually is. It's just a pair of methods - add and remove - linked by
some metadata.




Yup. And of course 2 is just a special case of 1, really.

That's right, I thought about this, too. Actually thought about making
this point. As a matter a fact, event handling is also only a special
case of delegate usage.
 
J

Jon Skeet [C# MVP]

Author said:
That's right, I thought about this, too. Actually thought about making
this point. As a matter a fact, event handling is also only a special
case of delegate usage.

Absolutely. Open your mind to the possibilities of doing other things
:)

It sounds like you've got a *much* better handle on delegates now.
 

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

Top