[...]
On my Forms I have several combo boxes and get filled when they get
focus. Is it proper to use delegate in this situation? Will it improve
performance? In what situations to use delegate?
You'd use a delegate any time that you want to be able to write
general-purpose code that calls some method that cannot or should not be
specified except at run-time. As compared to specifying the method to be
called explicitly where the call is made, using a delegate won't improve
performance and in fact probably will reduce performance by a very tiny
amount (it's unlikely to be by an amount you'd be able to measure in a
real-world application).
As far as what's proper to use in the situation you describe, that's hard
to say. You didn't describe the situation in much detail, nor even
exactly how you're using the delegate. If you're storing the delegate
explicitly, then at the very least it's possible your code would work a
little better from a design standpoint if you used an event instead:
instead of assigning a delegate to some variable, you'd add it to the
event as a subscriber. But this is more a matter of writing the code so
that the design conforms more to the standard mechanisms used in
..NET...it's not something that would affect performance or even the basic
functionality.
Another alternative would be to define an interface that requires the
method you're going to call. Then the class that implements the method
would just be declared as implementing that interface. Rather than
storing a delegate in this case, you'd store the reference to an instance
of that class and call the interface method directly. Obviously, this
would only work if the method isn't static.
And of course, a third alternative is to just make the code that calls the
method dependent on the class that declares it. In this case, you would
simply store the reference to an instance of that class and call the
method as needed. IMHO, this would be fine if your code is such that you
will always be calling this particular class. The main reason for
abstracting things away from something like this is to make the code more
general-purpose, but that's not always a design requirement.
Given your question, there's no good way for anyone to tell you with
certainty what the "best" approach would be. But hopefully based on your
own knowledge of your code and the above, you can figure that out yourself.
Pete