When is useful to use delegate type in a web application
I use C# and Asp.Net Web Forms.
I read about the way Asp.Net deal with events for the GUI Objects using its "Delegate Model".
I'm concerting if would make sense use "Custom Delegate Type" in other context a part of event handling.
My questions:
In a real world scenario when could be useful to use Delegate Types apart from event handling? Example: for validation fields in a class?
In which kind of architecture Custom Delegate Type best feet?
Pro an开发者_开发知识库d Cons of Delegate Type?
Thanks for your time on this!
If you use LINQ to Objects at all, you're using delegates. (If you're using EF or something similar, then you're using expression trees of delegate types.)
If you use MVC, you're probably using expression trees too (
@Html.For(x => ...)
)If you ever start threads or tasks, you'll be using delegates.
Basically they're great for representing single operations in an easily-expressible way. They can be used as an alternative to single-method interfaces, and work well for providing hooks for custom behaviour instead of using inheritance.
Since .NET 3.5 with its Action
and Func
delegate families, I rarely create custom delegate types - only when it makes sense to do so on readability grounds, basically.
精彩评论