Sayfalar

Delegates etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Delegates etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

29 Ağustos 2019 Perşembe

Fun


Değer tip dönen metotları çalıştırmak üzere tasarlanmış bir mimaridir. Gönderilen kod bloğunu yada metotu çalıştırır.

class Program
{
 
    static void Main(string[] args)
    {
 
        //Parametre alıp int döenen Fun metot
        Func<intintint> callSum = Sum;     
        Console.WriteLine(callSum(13));
 
 
        // Parametresiz int değer dönen bir Fun metot
        Func<int> getRandomnumber = () => new Random().Next(1100);
 
        Console.WriteLine(getRandomnumber());
 
 
        Console.ReadLine();
 
    }
 
 
    public static int Sum(int num1, int num2)
    {
 
        Console.WriteLine( num1 + num2);
        return num1 + num2;
    }
 
 
}

Action


Void metotları çalıştırmak üzere tasarlanmış bir mimaridir. Gönderilen kod bloğunu yada metotu çalıştırır.

MyAction(() =>
{
    SayHello();  //Sonuç "Hello"
});
 
 
 void MyAction(Action action)
{
    try
    {
        action.Invoke();
    }
    catch (Exception ex)
    {
 
        Console.WriteLine(ex.Message);
    }
}
 
 
 void SayHello()
{
    Console.WriteLine("Hello");
}

Delegates



Void olan ve parametre almayan metotları iletmek için kullanılacak delege tanımlaması:
Bir senaryo çerçevesinde tasvir edelim;
Osmanlı, İngiliz Kralına sadece mesaj iletip mesaj alacak bir elçi yollamak istediğinde

Delegate HQ adlı merkezden
public delegate void DelegateHQ();
Bir elçi tayin edilir.
DelegateHQ ottomanDelegate;
Bu elçiye iki adet görev verilir.(+=)
ottomanDelegate = ottomanSultan.SendMessage;
ottomanDelegate += ottomanSultan.GetFeedBack;
Elçi kralın huzurunda bu görevleri yerine getirir.
ottomanDelegate();
Eğer yolda bir değişiklik olursa ör. Mesajı göndermekten vazgeçilirse bu görev iptal edilir.( - = )
ottomanDelegate -= ottomanSultan.SendMessage;
Void olan ve parametre alan metotları iletmek için kullanılacak delege tanımlaması:
public delegate void DelegateHQ2(string text);
DelegateHQ2 ottomanDelegateWithFerman;
ottomanDelegateWithFerman = ottomanSultan.SendFerman;
ottomanDelegateWithFerman("ferman içeriği"); 
Değer dönen ve parametre alan metotları iletmek için kullanılacak delege tanımlaması:
public delegate int DelegateHQ3(int number1, int number2);
DelegateHQ3 ottomanDelegateWithNum;
ottomanDelegateWithNum = ottomanSultan.ReceiveGold;
ottomanDelegateWithNum(24);

 Tüm kodlama:

namespace Delegates
{
    class Program
    {
        public delegate void DelegateHQ();
 
        public delegate void DelegateHQ2(string text);
 
        public delegate int DelegateHQ3(int number1, int number2);
 
        static void Main(string[] args)
        {
            OttomanSultan ottomanSultan = new OttomanSultan();
 
            DelegateHQ ottomanDelegate;
            ottomanDelegate = ottomanSultan.SendMessage;
            ottomanDelegate += ottomanSultan.GetFeedBack;
            ottomanDelegate -= ottomanSultan.SendMessage;
            ottomanDelegate();
 
            DelegateHQ2 ottomanDelegateWithFerman;
            ottomanDelegateWithFerman = ottomanSultan.SendFerman;
            ottomanDelegateWithFerman("ferman içeriği");
 
            DelegateHQ3 ottomanDelegateWithNum;
            ottomanDelegateWithNum = ottomanSultan.ReceiveGold;
            ottomanDelegateWithNum(24);
 
 
            Console.ReadLine();
        }
 
        public class OttomanSultan
        {
            public void SendMessage()
            {
                Console.WriteLine("Message received by England King!");
            }
 
            public void GetFeedBack()
            {
                Console.WriteLine("Feed Back taken by ottomanDelegate!");
            }
 
            public void SendFerman( string ferman)
            {
                Console.WriteLine("{0} received by England King!",ferman);
            }
 
            public int ReceiveGold(int num1, int num2)
            {
                
                Console.WriteLine("{0} gold taken ", num1+num2);
                return num1 + num2;
            }
 
 
        }
    }
}





MVC LOADING TOO SLOW ISSUE