17 Kasım 2016 Perşembe

delegate Anahtar Kelimesi - Metodun İmzasını Belirtir

Giriş
Açıklaması şöyle
A delegate is a type that represents a method with a specific signature and return type.
C++ ile karşılaştırması şöyle
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.
Hazır Delegate Sınıfları
Action DelegateFunction Delegate hazır sınıflardan.

Tarihçesi
Bence tarihçesini anlamak önemli diye not almak istedim. Burada delegate tanımının neden şöyle olmadığı anlatılıyor. İki seçenek var var bu type safety adına tercih edilmemiş.
delegate myDelegate = new delegate (int) -> void
Açıklaması şöyle
C# is a historically grown language. It is frequently useful to pass around references to methods, especially when writing event-driven code in GUIs. To address this use case, C# 1.0 provided delegates. Delegates were very good, especially compared with alternative languages like Java (which had no comparable mechanism at all at the time).

A delegate must have a specific signature so that calls and assignments to the delegate variable can be type-checked. There are two ways to achieve this:

- Either, there is some type syntax that represents a delegate type with some signature. This syntax will have to be used wherever the delegate type is referenced.
- Or, there is a way to declare a type name that represents a signature.

C# 1.0 settled on the second approach, which is advantageous if the delegate type is referenced in multiple places.

The C language used the first approach. Very roughly, the equivalent to your first example would be written in C as:
void (*myMethodHolder)(int) = NULL;

Though C made it also possible to declare an alias:
typedef void (*MyDelegate)(int);
MyDelegate myMethodHolder = NULL;

In C, this second solution is usually preferred since the function type syntax is pretty confusing, especially when denoting functions that return functions or an array of functions.

With C# 2.0, generic type names were introduced that represent common delegates. In a way, this also lets you use the first approach, albeit with less crazy syntax than C. Your example could be written as:

Action<int> myMethodHolder;
Where the delegate returns a value, a Func<...> should be used instead of an Action<...>.

In my opinion, using the Action/Func names are vastly preferable to declaring delegate types yourself. Of course, it is still possible to provide custom names when desired with a type alias like using MyDelegate = Action<int>.

Tanımlama
Örnek
Şöyle yaparız.
delegate void MyCallback();
Aynı şeyi Java'da yapmak için arayüz tanımlamak gerekir.
@FuntionalInterface
public interface Runnable {
    void run();
}
Örnek
Şöyle yaparız
delegate void MyDelegate(int num);
MyDelegate myMethodHolder;





Hiç yorum yok:

Yorum Gönder