Call backs using delegates c sharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication20
{
      class Program
    {
        static void Main(string[] args)
        {
            MyClass m = new MyClass();
            m.run(callback);
        }
        static void callback(int i)
        {
            Console.WriteLine(i);
        }

    }

    class MyClass
    {
        public delegate void show(int i);
        public void run(show s)
        {
            for(int i =0; i<10 i="" p="">            {
                s(i);
            }
        }


    }
}


Here, we are creating a delegate to show the integer, we know delegate is nothing but a reference to a method. I have created a callback method in Myclass and passing this method as reference to to run method, So now we are saying to complier show is nothing but callback method in our class, so it invokes back the callback class as it is static. this is simple call back technique using delegates
Call backs using delegates c sharp Call backs using delegates c sharp Reviewed by Share less to Learn More on 4:30 PM Rating: 5

No comments