I know this question have been asked a lot, but I need one answer for all what I asked in a very practical way and preferably with code examples.
When Should I use delegate methods in C#?
How to use it?
Why should I use it?
what does it has to do with Multi threading?
Any answer is appreciated.
A delegate is basically a method pointer. It has a reference to the method, and its object (unless it’s a static method, of course).
You use a delegate whenever you need to call a method but the code calling it doesn’t know which method it is. The most common reason is that the code calling the method was made before the method, e.g. a library method like
List<T>.Sort(comparison).Delegates doesn’t call the method on a different thread, it’s called on the same thread as the code using the delegate. When a method is called on a different thread it’s because the code using the delegate is already running on a different thread, or specifically starts a new thread for the method.