I saw a question the last time on Stackoverflow. The accepted answer was from Jon Skeet which was:
- Event handlers (for GUI and more)
- Starting threads
- Callbacks (e.g. for async APIs)
- LINQ and similar (List.Find etc)
- Anywhere else where I want to effectively apply “template” code with some specialized logic inside (where the delegate provides the specialization)
I was once asked “What is the use of a delegate?”. The main answer that I found was “to delay the call”. Most people see delegate as events most of the time. However, they can be put to much greater use. Here is an example that I’ll gladly share with you all:
public class StreamProxy<T> where T : Stream
To summarize what it does… it accept a delegate that returns a class that derives from “Stream” that will be used as a Constructor. It will return you a StreamProxy object that you can then use to read or write String out of that stream. What is interesting is that when it’s first created… nothing is done on the file. You are just giving instruction to the class on how to access it. When you then read/write from the file, the class knows how to manage a stream and make sure that no locks are left on the files.
Here is a sample usage of that class:
// Here I use a FileStream but it can also be a MemoryStream or anything that derives from Stream
That’s all folks! As long as you can give a Stream to this proxy, you won’t need to do any “using” in your code and everything will stay clean!
See you all next time!