14 Ekim 2023 Cumartesi

C#'DA DELEGATE (TEMSİLCİ)

 



namespace CSExamples

{

    class Program

    {

        static void Main(string[] args)

        {



            // Delegate (Temsilciler)


            Hesap hesap = Topla;


            int sonuc = hesap(5, 4);


            Console.WriteLine(sonuc);




            Console.ReadLine();

            

        }


        delegate int Hesap(int x, int y);


        static int Topla(int a, int b)

        {

            return a + b;

        }


        static int Cikar(int a, int b)

        {

            return a - b;

        }


        static int Carp(int a, int b)

        {

            return a * b;

        }


        static int Bol(int a, int b)

        {

            if (a > 0 && b > 0)

                {

                return a / b;

            }

            return 0;

        }

    }

}





namespace CSExamples

{

    class Program

    {

        static void Main(string[] args)

        {



            // Delegate (Temsilciler)


            Hesap hesap = Topla;


            int sonuc = hesap(5, 4);


            hesap = Carp;


            Console.WriteLine("Çarp : " + hesap(5,4));





            Console.WriteLine(sonuc);




            Console.ReadLine();

            

        }


        delegate int Hesap(int x, int y);


        static int Topla(int a, int b)

        {

            return a + b;

        }


        static int Cikar(int a, int b)

        {

            return a - b;

        }


        static int Carp(int a, int b)

        {

            return a * b;

        }


        static int Bol(int a, int b)

        {

            if (a > 0 && b > 0)

                {

                return a / b;

            }

            return 0;

        }

    }

}





namespace CSExamples

{

    class Program

    {

        static void Main(string[] args)

        {



            // Delegate (Temsilciler)


            Hesap hesap = new Hesap(Topla);

            Console.WriteLine("Topla : " + hesap(5,4));




            Console.ReadLine();

            

        }


        delegate int Hesap(int x, int y);


        static int Topla(int a, int b)

        {

            return a + b;

        }


        static int Cikar(int a, int b)

        {

            return a - b;

        }


        static int Carp(int a, int b)

        {

            return a * b;

        }


        static int Bol(int a, int b)

        {

            if (a > 0 && b > 0)

                {

                return a / b;

            }

            return 0;

        }

    }

}






namespace CSExamples

{

    class Program

    {

        static void Main(string[] args)

        {



            // Delegate (Temsilciler)


            Hesap hesap = new Hesap(Topla);

            Console.WriteLine("Topla : " + hesap(5,4));

            hesap = new Hesap(Cikar);

            Console.WriteLine("Çıkar : " + hesap(5, 4));


            Console.ReadLine();

            

        }


        delegate int Hesap(int x, int y);


        static int Topla(int a, int b)

        {

            return a + b;

        }


        static int Cikar(int a, int b)

        {

            return a - b;

        }


        static int Carp(int a, int b)

        {

            return a * b;

        }


        static int Bol(int a, int b)

        {

            if (a > 0 && b > 0)

                {

                return a / b;

            }

            return 0;

        }

    }

}






namespace CSExamples

{

    class Program

    {

        static void Main(string[] args)

        {



            // Delegate (Temsilciler)


            Hesap hesap = new Hesap(Topla);

            int sonuc = hesap.Invoke(4, 4);


            Console.WriteLine(sonuc);


            Console.ReadLine();

            

        }


        delegate int Hesap(int x, int y);


        static int Topla(int a, int b)

        {

            return a + b;

        }


        static int Cikar(int a, int b)

        {

            return a - b;

        }


        static int Carp(int a, int b)

        {

            return a * b;

        }


        static int Bol(int a, int b)

        {

            if (a > 0 && b > 0)

                {

                return a / b;

            }

            return 0;

        }

    }

}



GENERIC DELEGATE (JENERİK TEMSİLCİ)



namespace CSExamples

{

    class Program

    {


        delegate void MyGenericDelegate<T>(T args);




        static void Main(string[] args)

        {



            // Delegate (Temsilciler)




            MyGenericDelegate<string> stringDelegate = new MyGenericDelegate<string>(StringTarget);

            stringDelegate("Talo");





            Console.ReadLine();



        

        }


        static void StringTarget(string arg)

        {

            Console.WriteLine("Arg metnini büyüt: " + arg.ToUpper());

        }


        static void IntTarget(int arg)

        {

            Console.WriteLine("++arg : " + ++arg);

        }


    }

}




namespace CSExamples

{

    class Program

    {


        delegate void MyGenericDelegate<T>(T args);




        static void Main(string[] args)

        {



            // Delegate (Temsilciler)




            MyGenericDelegate<int> intDelegate = new MyGenericDelegate<int>(IntTarget);

            intDelegate(6);




            Console.ReadLine();



        

        }


        static void StringTarget(string arg)

        {

            Console.WriteLine("Arg metnini büyüt: " + arg.ToUpper());

        }


        static void IntTarget(int arg)

        {

            Console.WriteLine("++arg : " + ++arg);

        }


    }

}


DELEGATE & EVENT İLİŞKİSİ

Öncelikle bir form uygulaması açıyoruz ve içine load olarak mesaj yüklüyoruz. Form açılırken gelen mesaj dışında ayrıca form alanına çift tıkladığımızda gelmesi için bir mesaj daha yüklüyoruz. Kodlar aşağıdadır.





namespace WindowsFormsApp1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();


            this.Load += Form1_Load;

            this.DoubleClick += Form1_DoubleClick;

        }

        

        private void Form1_DoubleClick(object sender, EventArgs e)

        {

            MessageBox.Show("Merhaba!");

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            MessageBox.Show("Merhaba!");

        }

    }

}







namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        delegate int Del(int x);

        delegate void Yaz(string metin);
        public Form1()
        {
            InitializeComponent();

            Button btn = new Button();
            btn.Text = "Tıkla";
            btn.Name = "btnTikla";
            btn.Width = 200;
            btn.Height = 50;
            btn.Left = btn.Width + 5;
            btn.Click += delegate (object sender, EventArgs e)
            {
                string mesaj = (sender as Button).Text;
                MessageBox.Show(mesaj);
            };
            this.Controls.Add(btn);
        }
        
        
    }
}


ACTION

Geriye değer döndürmeyen void metotlar için kullanılır.




namespace CSExamples
{
    class Program
    {

        delegate void MyGenericDelegate<T>(T args);



        static void Main(string[] args)
        {


            // Action

            Action<int, string> action = new Action<int, string>(KullaniciBilgi);
            action(3, "TALO");
            



            Console.ReadLine();


        
        }

        static void KullaniciBilgi(int kullaniciId, string adSoyad)
        {
            Console.WriteLine("Kullanıcı Id : " + kullaniciId);
            Console.WriteLine("Ad Soyad : " + adSoyad);
        }


        static double GetUserAgeAtTime(DateTime birthDate)
        {
            return (DateTime.Now - birthDate).TotalDays;

        }




    }
}

FUNCTION

Geriye değer dönebilen action versiyonudur.




namespace CSExamples
{
    class Program
    {

        delegate void MyGenericDelegate<T>(T args);



        static void Main(string[] args)
        {


            
            // Function

            Func<DateTime, double> func = new Func<DateTime, double>(GetUserAgeAtTime);
            double dateDiff = func(new DateTime(1991, 2, 6));
            Console.WriteLine(dateDiff + " gündür yaşıyorsunuz.");


            Console.ReadLine();


        
        }

        static void KullaniciBilgi(int kullaniciId, string adSoyad)
        {
            Console.WriteLine("Kullanıcı Id : " + kullaniciId);
            Console.WriteLine("Ad Soyad : " + adSoyad);
        }


        static double GetUserAgeAtTime(DateTime birthDate)
        {
            return (DateTime.Now - birthDate).TotalDays;

        }




    }
}
Paylaş:

0 Yorumlar:

Yorum Gönder

İSTATİSTİK

KİTAP ÖNERİSİ