Func<> delegesi, Preciadte<T> delegesi gibi linq işlemlemlerinde kullanılmaktadır. Aslında Pricate<T>, Func<T,bool> ile aynı yapıya sahiptir. Bir T nesnesi gider bool tipinde bir değer geri döner. Framework de bir çok yapısı bulunmaktadır. En çok kullanılanlar aşağıdadır.
Public delegate TResult Func<TResult>() // parametre yok, dönüş değeri Tresult
Public delegate TResult Func<T, TResult>(T t) // parametre T, bir adet, dönüş değeri Tresult
Public delegate TResult Func<T1, T2, TResult>(T1 t1, T2 t2) // parametre T, 2 adet, dönüş değeri Tresult
Public delegate TResult Func<T1, T2, T3, TResult>(T1 t1, T2 t2, T3 t3) // parametre T, 4adet, dönüş değeri Tresult
Public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 t1, T2 t2, T3 t3,T4 t4) // parametre T, 4 adet, dönüş değeri Tresult
Parametre yok ise şöyle bir örnek yapabiliriz.
static void Main(string[] args)
{
//delegeat tanımla ve başlat
Func<string> funcDelegate = First;
string mesaj = funcDelegate();
Console.WriteLine(mesaj);
}
private static string First()
{
return "merhaba dünya!!";
}
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int> { 6, 9, 3, 6, 7, 33, 56 };
Predicate<int> more = new Predicate<int>(sonuc);
Func<int, bool> more2 = new Func<int, bool>(sonuc);
int sayi= list.FirstOrDefault(more2);
Console.WriteLine(sayi);
}
static bool sonuc(int sayilar)
{
return sayilar > 10;
}
}
Örneğimizi çoğaltılım artıkk, 2 parametr e bir geriş dönüş olsun. Float olan dönüş tipidir.
public class Program{
static void Main(string[] args)
{
Func<int, int, float> deleggem = Ekle;
double sayi = deleggem(2, 4);
Console.WriteLine(sayi);
}
private static float Ekle(int birinci, int ikinci) {
return birinci * ikinci;
}
}
Aynı örneği isimsiz bir method şeklinde yazdım. Bir method bir defa lazım olacak ise, bu istenilen yerde yazılabilir.
public class Program{
static void Main(string[] args){
Func<int, int, float> deleggem = delegate(int birinci, int ikinci) {
return birinci * ikinci;
};
double sayi = deleggem(2, 4);
Console.WriteLine(sayi);
}
}
Şimdide aynı örneği lambada ifadeleri ile yazalım.
public class Program{
static void Main(string[] args){
Func<int, int, float> delegem=(x,y)=>x*y;
double sayi = delegem(2, 4);
Console.WriteLine(sayi);
}
}
Son bir örnek daha verelimm.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication18
{
public class Person
{
public int ProductId { get; set; }
public string Name { get; set; }
public string SurName { get; set; }
}
public static class ProductFactory
{
public static Person CreateProduct(int id=0) {
return CreateProduct(id, "No Name", "No Surname");
}
public static Person CreateProduct(int id, string ad) {
return CreateProduct(id, ad, "Soy isimsiz");
}
public static Person CreateProduct(int id, string name, string surname) {
return new Person {
ProductId = id,
Name = name,
SurName = surname,
};
}
}
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person> {
ProductFactory.CreateProduct(1),
ProductFactory.CreateProduct(2,"Kemal"),
ProductFactory.CreateProduct(3,"Mustafa", "Kemal"),
ProductFactory.CreateProduct(4,"Kemal", "Ballı"),
ProductFactory.CreateProduct()
};
List<Person> newList = list.Where(x => x.Name == "Kemal").ToList(); ;
newList.ForEach(x => Console.WriteLine("{0}: {1} {2}", x.ProductId, x.Name,x.SurName));
}
}
}