Delegate Nedir?
C# programlama dilinde tanımlanan ve metot olarak adlandırılan işlevlerin bellek adresini tutmak için
kullanılan yapıya delegate veya temsilci denir. C# delegate yapısı C/C++ dillerindeki function pointer
veya fonksiyon göstericileri yapısına benzemektedir.
Metot adı ve parametresi metot imzası olarak adlandırılır.
Bellekte bulunan metotlara erişebilmek için metot imzasının bilinmesi gerekir. Bu metot imzası C#
içerisinde yer alan delegate ile tanımlanır.
Metotların sabit bir tanımı yoktur. Aşağıdaki iki metot birbirinden farklıdır:
void Topla(int sayi1, double sayi2);
void Topla(double sayi1, int sayi2);
Metot adı ve parametresi metot imzası olarak adlandırılır.
Delegate Kullanımı
Genel tanımı aşağıdaki gibidir:
delegate <Metot_Dönüş_Değeri> <Temsilci_Adı> (<Metot_Parametresi>);
Yukarıda tanımlanan ilk metoda erişmek için aşağıdaki gibi bir delegate tanımı yapılmalıdır:
delegate void Temsilci (int sayi1,int sayi2);
Oluşturulan bu temsilci sayesinde bu imzaya sahip metotlara erişilebilir. Metotlara erişimi aşağıdaki
gibidir:
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace @delegate | |
{ | |
class Program | |
{ | |
static void Sum(int number1, double number2) | |
{ | |
Console.WriteLine("Number1: {0} + Number2: {1} = result {2}", number1, number2, number1 + number2); | |
} | |
delegate void sumDelegate(int x, double y); | |
static void Main(string[] args) | |
{ | |
sumDelegate sum = Sum; // sumDelegate sum = new sumDelegate(Topla); | |
sum(3, 5); | |
Console.ReadLine(); | |
} | |
} | |
} |
Func Delegate
Sıfır veya daha fazla input alır ve bir tane output verir.
public delegate TResult Func < in T,out TResult>(T arg);
Örnek:
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace @delegate | |
{ | |
class Program | |
{ | |
static int Sum(int num1, int num2) | |
{ | |
return num1 + num2; | |
} | |
static void Main(string[] args) | |
{ | |
Func<int, int, int> add = Sum; | |
int result = add(3, 5); | |
Console.WriteLine(result); | |
Console.ReadLine(); | |
} | |
} | |
} |
Action Delegate
Bir değer döndürmez, bunun dışından func delegate ile aynıdır.
public delegate void Print (int val);
Örnek:
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace @delegate | |
{ | |
class Program | |
{ | |
static void ConsolePrint(int num) | |
{ | |
Console.WriteLine(num); | |
} | |
static void Main(string[] args) | |
{ | |
Action<int> printCns = ConsolePrint; | |
printCns(5); | |
Console.ReadLine(); | |
} | |
} | |
} |
Func ve Action Delegate Avantajları
- Tanımlamak hızlı ve kolaydır.
- Uygulama boyunca uyumlu tip.
- Kodu kısadır.
Kaynak:
Ders notları
https://docs.microsoft.com/tr-tr/dotnet/api/system.action-1?view=net-6.0
https://www.yusufsezer.com.tr/csharp-delegate/
https://docs.microsoft.com/tr-tr/dotnet/api/system.func-2?view=net-6.0