خواهش می کنم یه نفر بگه وقتی delegate هست چه نیازی به event داریم؟من هر چی میخونم بیشتر قاطی میکنم.
Printable View
خواهش می کنم یه نفر بگه وقتی delegate هست چه نیازی به event داریم؟من هر چی میخونم بیشتر قاطی میکنم.
نقل قول:
Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own delegated method.
This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. For example, a sort algorithm could be passed a reference to the method that compares two objects. Separating the comparison code allows the algorithm to be written in a more general way.
سعی کنین که اول MSDN رو بخونین....نقل قول:
Delegates have the following properties:
- Delegates are similar to C++ function pointers, but are type safe.
- Delegates allow methods to be passed as parameters.
- Delegates can be used to define callback methods.
- Delegates can be chained together; for example, multiple methods can be called on a single event.
- Methods don't need to match the delegate signature exactly. For more information, see [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ]
- C# version 2.0 introduces the concept of [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] , which permit code blocks to be passed as parameters in place of a separately defined method.
بنام خدا.
سلام.
دوست عزیز و بزرگوار.
مثال delegate ها و event ها مثل مثال class ها و توابع درونشون هست.
شما یک کلاس رو هربار که new کنید مجدداً میتونید از توابع داخل اون استفاده کنید.یعنی توابع داخل کلاس سرجاشون هستند و تکون نمیخورن.
شما یک delegate رو در نظر بگیر:
این delegate مانند یک کلاس فقط یکبار تعریف میشه.اما Event هایی که از اون استفاده میکنند میتونن خیلی زیاد باشن:کد:delegate void ProgressStatus(string status);
شما یک delegate رو یکبار تعریف میکنید (مانند کلاس) و تمام event هایی که خروجی هاشون شبیه هم هستکد:event ProgressStatus Start;
event ProgressStatus Complete;
رو از همون delegate استفاده میکنید.مانند مثالی که در بالا زدم.یک delegate برای چند event.
اگر شما نیاز به رویداد یا event ای داری که ورودی و خروجیش فرق میکنه نیاز هست که delegate جدیدی برای اون بسازی:
موفق و سربلند باشید در پناه حق.کد:delegate void ProgressStatus(string status);
event ProgressStatus Start;
event ProgressStatus Complete;
delegate string ProgressPercent(double value);
event ProgressPercent ValueChanged;