مشاهده نسخه کامل
: کمـــــــــــــک در مورد event ها در #C
amin.mohamadpour
22-05-2012, 21:39
خواهش می کنم یه نفر بگه وقتی 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.
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 Covariance and Contravariance ([ برای مشاهده لینک ، لطفا با نام کاربری خود وارد شوید یا ثبت نام کنید ])
C# version 2.0 introduces the concept of Anonymous Methods ([ برای مشاهده لینک ، لطفا با نام کاربری خود وارد شوید یا ثبت نام کنید ]), which permit code blocks to be passed as parameters in place of a separately defined method.
سعی کنین که اول MSDN رو بخونین....
بنام خدا.
سلام.
دوست عزیز و بزرگوار.
مثال delegate ها و event ها مثل مثال class ها و توابع درونشون هست.
شما یک کلاس رو هربار که new کنید مجدداً میتونید از توابع داخل اون استفاده کنید.یعنی توابع داخل کلاس سرجاشون هستند و تکون نمیخورن.
شما یک delegate رو در نظر بگیر:
برای مشاهده محتوا ، لطفا وارد شوید یا ثبت نام کنید
این delegate مانند یک کلاس فقط یکبار تعریف میشه.اما Event هایی که از اون استفاده میکنند میتونن خیلی زیاد باشن:
برای مشاهده محتوا ، لطفا وارد شوید یا ثبت نام کنید
شما یک delegate رو یکبار تعریف میکنید (مانند کلاس) و تمام event هایی که خروجی هاشون شبیه هم هست
رو از همون delegate استفاده میکنید.مانند مثالی که در بالا زدم.یک delegate برای چند event.
اگر شما نیاز به رویداد یا event ای داری که ورودی و خروجیش فرق میکنه نیاز هست که delegate جدیدی برای اون بسازی:
برای مشاهده محتوا ، لطفا وارد شوید یا ثبت نام کنید
موفق و سربلند باشید در پناه حق.
vBulletin , Copyright ©2000-2025, Jelsoft Enterprises Ltd.