开发者

what is Delegate and when to use it? [closed]

开发者 https://www.devze.com 2023-03-06 11:59 出处:网络
It's diff开发者_如何转开发icult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
It's diff开发者_如何转开发icult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

i have searched on the Google but could not find exactly what the delegate is and when to use delegate? can any one explain me with an example?


You can get it cleared here :

What : http://csharpindepth.com/Articles/Chapter2/Events.aspx

when : http://msdn.microsoft.com/en-us/magazine/cc301810.aspx


A delegate is kind of like a callback, but it allows you to register more than one callback action.

Maybe an example is easier: you have an OnClick delegate. By default, nothing happens. But you can register 1, or 2, or N callbacks which will ALL receive the OnClick event.


Delegate is a concept related to Delegation Design Pattern. Let me point you to the wiki link so you can take over the study from there.

http://en.wikipedia.org/wiki/Delegation_pattern


In simplest terms, a delegate is just like a function assigned to a variable (I'm not sure if that's really what happens under the hood but it helps to think of it like that). If you have programmed in C++, its like the address of a function or the return value of GetProcAddress() but with more functionality and typesafety.

You can call the delegate like regular function, pass it as a function argument (like a regular variable) and it can also be null. Google it again (or visit the links of other posters), there are lots of materials about delegates.

0

精彩评论

暂无评论...
验证码 换一张
取 消