Tuesday, July 18, 2017

Delegates in C#- 2


1.
 namespace DelegateEx
{
  public delegate int myDelegate(int x, int y);
  public delegate string mydelString(string fname, string lname);

    class Program
    {
       public static void Main(string[] args)
        {
            myDelegate del = new myDelegate(Math.Add);
            del += Math.Subtract;
            int result =  del(10,6);

            del += Math.Multiply;
            int r = del(3, 4);
            //myDelegate m = Math.Add;
            //int res = m.Invoke(2, 3);
           // int res = del.Invoke(6, 7);

            Console.WriteLine(result);
            Console.WriteLine(r);


            mydelString d1 = null;
            d1 += Math.GetFullName;// static methods can only be accessed from other static methods, and here Main and GetFullName are both static.
      string res = d1("test", "name"); // d1.Invoke("test", "name");
            Console.WriteLine(res);

            Console.ReadLine();
        }

      
    }

    public class Math {
        public static int Add(int a, int b)
        {
            return a + b;
        }
        public static int Multiply(int a, int b)
        {
            return a*b;
        }

        public static int Subtract(int a, int b)
        {
            return a - b;
        }

     public static string GetFullName(string fname, string lname)
        {
            return "The fullName is " + fname + " " + lname;
        }
    }
}
----------------------------------------------------------------------------------------
2.

public partial class multicast : System.Web.UI.Page
{

    public delegate void ProgressReporter(int percentComplete);

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // create a delegate, assign methods to it, then invoke it.
    // This is time consuming method...so we need to know its progress.
    // so we give a delegate parameter and ask it to fire it.. to let the subscribers know about progress.
    public void HardWork(ProgressReporter pr) 

    {
        for (int i = 0; i < 10; i++)
        {
            pr(i * 10); // invoking the delegate
            System.Threading.Thread.Sleep(100);
        }
    
    }


    public void WriteProgressToConsole(int percent)
    {
        Response.Write("WriteProgressToConsole value: " + percent);

    }
    public void WriteProgressToFile(int percent)
    {
        Response.Write("WriteProgressToFile value: " + percent);
      

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        ProgressReporter p = WriteProgressToConsole; // assigning methods to delegate
        p += WriteProgressToFile;

        HardWork(p);
     
    }
     
}
-------------------------------------------------------------------------
 3.


public partial class delegate1 : System.Web.UI.Page
{

    public delegate int Transform(int x);

    public delegate int TransformArray(int x);

    public delegate bool delIsEven(int x);

    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    //public int Square(int a)
    //{
    //    return a * a;
    //}

    // or the above method can also be declared like this below.
    int Square(int a) => a * a;
    int SquareArrayElement(int a) => a * a;
    bool MethodIsEven(int a) => (a % 2 == 0) ? true : false;

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Transform t = new Transform(Square); // or like below
        Transform t = Square;
        //int result= t(6); 
        // or like this below
        int result = t.Invoke(6);

        Response.Write("The delegate return value is: " + result + "</br>");

       
        TransformArray delarray = SquareArrayElement; // assigning methods to Delegate.
        delIsEven dIsEven = MethodIsEven;

        int[] myArray = { 2, 3, 4, 5, 6, 7, 8 };

        int[] finalArray = MethodToSquareArrayElement(myArray, delarray); // passing delegate as parameter

        for (int i = 0; i < finalArray.Length; i++)
        {
            string flag = "";
            if (dIsEven(finalArray[i]))
            {
                flag = "It is Even";
            }
            else
            {
                flag = "It is Odd";
            }

            Response.Write(finalArray[i] + " " + flag + "</br>");
        }

        //for (int i = 0; i < myArray.Length; i++)
        //{

        //    // int result1 = SquareArrayElement(myArray[i]);
        //    int result1 = delarray.Invoke(myArray[i]);
        //    Response.Write(result1 + "</br>");
        //}
    }

    public int[] MethodToSquareArrayElement(int[] arr, TransformArray del) // passing delegate as parameter.
    {
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = del(arr[i]); //
            // above line we can also write as i.e we can also directly pass the method name, but we preferred to pass delegate ref. above.
            //  arr[i] = SquareArrayElement(arr[i]);
            // ie. A Delegate variable can be assigned to a method at run time. This is useful for writing plugin methods.

        }

        return arr;
    }
}



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More