When would I use a delegate in asp.net?

2020-07-06 07:03发布

I'm always looking for a way to use all the tools I can and to stretch myself just beyond where I am at. But as much as I have read about delegates, I can never find a place to use them (like Interfaces, Generics, and a lot of stuff, but I digress.) I was hoping someone could show me when and how they used a delegate in web programming for asp.net c#(2.0 and above).

Thank you and if this wrong for Stack Overflow, please just let me know.

8条回答
疯言疯语
2楼-- · 2020-07-06 07:35

There isn't anything special to asp.net related to delegates (besides considerations when using async stuff, which is a whole different question), so I will point you to other questions instead:

Delegate Usage : Business Applications

Where do I use delegates?

查看更多
劳资没心,怎么记你
3楼-- · 2020-07-06 07:48

Well, whenever you handle an event, you're using a delegate.

查看更多
forever°为你锁心
4楼-- · 2020-07-06 07:52

Another example would be to publish events for user controls.

Eg.

// In your user control
public delegate void evtSomething(SomeData oYourData);
public event evtSomething OnSomething;

// In the page using your user control
ucYourUserControl.OnSomething += ucYourUserControl_OnSomething;

// Then implement the function
protected void ucYourUserControl_OnSelect(SomeData oYourData)
{
   ...
}
查看更多
姐就是有狂的资本
5楼-- · 2020-07-06 07:53

Recently i used the delegates for "delegating" the checking of the permissions.

public Func CheckPermission;

This way, the CheckPermission function can be shared by various controls or classes, say it in a static class or a utilities class, and still be managed centralized, avoiding also Interface explossion; just a thought

查看更多
We Are One
6楼-- · 2020-07-06 07:55

bdukes is right about events. But you're not limited to just using delegates with events.

Study the classic Observer Pattern for more examples on using delegates. Some text on the pattern points toward an event model, but from a raw learning perspective, you don't have to use events.

One thing to remember: A delegate is just another type that can be used & passed around similar to your primitive types such as an "int". And just like "int", a delegate has it's own special characteristics that you can act on in your coding when you consume the delegate type.

To get a really great handle on the subject and on some of it's more advanced and detailed aspects, get Joe Duffy's book, .NET Framework 2.0.

查看更多
\"骚年 ilove
7楼-- · 2020-07-06 07:55

To answer your second question first, I think this is a great question for StackOverflow!

On the first, one example would be sorting. The Sort() method on List takes a delegate to do the sorting, as does the Find() method. I'm not a huge fan of sorting in the database, so I like to use Sort() on my result sets. After all, the order of a list is much more of a UI issue (typically) than a business rule issue.

Edit: I've added my reasons for sorting outside the DB to the appropriate question here.

Edit: The comparison function used in the sort routine is a delegate. Therefore, if you sort a List using the .Sort(Comparison(T)) method the Comparison(T) method you pass to the sort function is a delegate. See the .Sort(Comparison(T)) documentation.

查看更多
登录 后发表回答