Showing posts with label delegates. Show all posts
Showing posts with label delegates. Show all posts

Sunday, August 17, 2014

Understanding anonymous methods in delegates

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

namespace ConsoleApps
{
class UnderstandingAnonymousMethod
{
public delegate int ArithmeticOperator(int a, int b); // Anonymous delegate with arguments

public delegate void PrintDelegate(); // Anonymous delegate without arguments

public delegate void NormalDelegateExample();

public delegate void GroupConversionDelegate();


public static void Main()
{


ArithmeticOperator addop = delegate(int n1, int n2)
{
  return n1 + n2;
};
Console.WriteLine(addop(10, 20));  // Prints 30




PrintDelegate pt = delegate
{
   Console.WriteLine("Anonymous delegate executed");
};
pt();  // Prints "Anonymous delegate executed"




NormalDelegateExample nm = new NormalDelegateExample(Print);
nm();      // Prints "Normal delegate example"

GroupConversionDelegate gd = Print2;
gd();       // Prints "Group Conversion delegate example"

}



        public static void Print()
        {
            Console.WriteLine("Normal delegate example");
        }
        public static void Print2()
        {
            Console.WriteLine("Group Conversion delegate example");
        }
    }
}


 delegate is a reference type variable that holds the reference to a method

Friday, August 1, 2014

Understanding Delegates (C# reference)

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

namespace Apps
{
    class DelegateWithInstanceMethod
    {
        private int count;
        public DelegateWithInstanceMethod(int count)
        {
            this.count = count;
        }
        public void Print(string message)
        {
            Console.WriteLine(message);
            Console.WriteLine(count);
        }
    }
    class UnderstandingDelegates
    {
        public delegate void PrintValue(string message);

        public static void Main()
        {
            //create delegate and make it point ot method
            PrintValue d = new PrintValue(Print);                    // Example 1

            //invoke method pointed by delegate
            d("Hello - Normal delegate call");
            d.Invoke("Hi");

            //Method group conversion
            PrintValue d2 = Print;                                   // Example 2

            //invoke method pointed by delegate
            d2("Nice - Method group conversion");
            d2.Invoke("Good");


            //Using delegate With Instance Method

            DelegateWithInstanceMethod s = new DelegateWithInstanceMethod(10);         

            //Method group conversion with instance method
            PrintValue d3 = s.Print;                                            // Example 3

            //invoke method pointed by delegate
            d3.Invoke("Invoked the delegate using the instance method");

        }
        public static void Print(string message)
        {
            Console.WriteLine(message);
        }
    }
}