Wednesday, June 12, 2013

(Part 2) What are delegates in c#?

Hello everyone, today we are going to proceed from our Previous tutorial, in this there was a introduction for delegates. Now today we are going to discuss multicast delegates.  Following we will do in our today's tutorial
  1. Introduction to multicast delegates
  2. Limitations of multicast delegates
  3. Syntax of multicast delegates
  4.   Implementing multicast delegates
  5. Brief example of multicast delegates
1. Introduction to multicast delegates
     A special feature of delegates is that a single delegate can encapsulate more than one method of matching signature. These kind of delegates are called ‘Multicast Delegates’. Internally, multicast delegates are sub-type of System.MulticastDelegate, which itself is a sub class of System.Delegate

2.  Limitations of multicast delegates
      While using the multicast delegates, the return type of the delegates must be the ‘void’. The reason for this limitation is that a multicast delegate may have multiple methods in its invocation list. Since single delegate (or method) can only return a single value, a multicast delegate type must have the void as the return type.

3.  Syntax of multicast delegates
     The syntax of the multicast delegate is same as the simple delegate, with the limitation which we have discussed earlier:

Delegate void testMulticastDelegate(list of parameters);

Delegate: the keyword that will ensure us that this is the delegate.
Void: the return type of the delegate.
testMulticastDelegate: the name of the delegate.
list of parameters: the matching parameters same like of function.
We will proceed with the following example throughout this tutorial.

Delegate void testMulticastDelegate(int a,int b);

 4. Implementing multicast delegates


     As we have discussed the syntax of our multicast delegate and now we will proceed with the implementation of the multicast delegate or the way referring to more than one function using the same delegate.
Don’t forget to use ‘+=’ operator to refer more than one function into multicast delegate. As we are doing in the following:
testMulticastDelegate arithMethod;
arithMethod  =new testMulticastDelegate(add);
arithMethod  +=new testMulticastDelegate(sub);
arithMethod  +=new testMulticastDelegate(mul);
Don’t forget to use ‘-=’ operator to remove a function from delegate. As we are doing in the following:
testMulticastDelegate arithMethod;
arithMethod  =new testMulticastDelegate(add);
arithMethod  +=new testMulticastDelegate(sub);
arithMethod  -=new testMulticastDelegate(mul);


5. Brief example of multicast delegates
     Open Visual studio and follow the following steps:
File->New->Project->Installed Templates->C#->Windows->Console Application
And paste the following code.




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

namespace ConsoleApplication1
{
    //declaring delegate
    public delegate void testMulticastDelegate(int p, int q);
    class Program
    {
        // function for adding two numbers
        static void add(int a, int b )
        {
            Console.WriteLine("{0} + {1} = {2}",a,b,a+b);
        }

        // function for comparing two numbers
        static void max(int a, int b)
        {
            if (a > b)
            {
                Console.WriteLine("{0} is greater than {1}", a, b);
            }
            else
            {
                Console.WriteLine("{0} is greater than {1}", b, a);
            }
        }
       
        // function for multiplying two number
        static void mul(int a, int b)
        {
            Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
        }
        static void Main(string[] args)
        {
            int num1, num2, num3;
            testMulticastDelegate arithMethod;
           
            // referring more than one function to a delegate
            arithMethod = new testMulticastDelegate(add);
            arithMethod += new testMulticastDelegate(max);
            arithMethod += new testMulticastDelegate(mul);

            Console.Write("Enter first Number:\t");
            num1 = Convert.ToInt16(Console.ReadLine());

            Console.Write("Enter seconed Number:\t");
            num2 = Convert.ToInt16(Console.ReadLine());

            arithMethod(num1,num2);

            Console.Write("Enter 1 for removing multiplication function and 2 for sum:\t");
            num3 = Convert.ToInt16(Console.ReadLine());

            switch(num3)
            {
                case 1:
                    Console.WriteLine("multiplication() method has been removed");
                  
                    // Eliminating a function reference
                    arithMethod -= new testMulticastDelegate(mul);
                    arithMethod(num1, num2);
                    break;
                case 2:
                    Console.WriteLine("sum() method has been removed");

                  
                     // Eliminating a function reference
                    arithMethod -= new testMulticastDelegate(add);
                    arithMethod(num1, num2);
                    break;

            }
            Console.ReadLine();
        }
    }
}








This will out put in the following way.


T
In the end of today's tutorial we have understand the data encapsulation in OOP.
That's all from today.
Thank You! ! !

 


Monday, June 10, 2013

(Part 1) What are delegates in c#?

Hello everyone, today we are going to discuss the delegates using c#,we will discuss the syntax, and in the end we will take a look at example to add two numbers using delegates in c#. In the Prerequisites we must have  Visual studio (any version).

What are the delegates in c# (dotnet)?
Delegates are similar to object references, but are used to reference methods instead of objects. The type of a delegate is the type or signature of the method rather than the class. Hence following are the three properties of delegates:
  1. The type or signature of the method that the delegate can point to
  2. The delegate reference which can be used to reference a method
  3. The actual method referenced by the delegate
What are the advantages of delegates?
At Very first advantage of the delegate is that it encapsulate our method so the user of our application is don't know about the inner processing of the application. The other advantages are in the following:
  • Effective use of delegate improves the performance of application
  • Used to call a method asynchronously
Declaring a delegate:
Declaration of a delegate is in the following:

delegate datatype delegate_name(list of parameters);
delegate: keyword
datatype: type of the delegate which must be same as the type of function to whom the delegate is refering.
list of parameters: all the parameters same as the parameters of the function to whom the delegate is refering.

Example for syntax:
   delegate int myDelegate(int a, int b);
Once we have defined a delegate type, we can set it to reference actual methods with matching signatures. A delegate reference can be declared just like an object reference. For example, a reference of type myDelegate (defined above) can be declared as:
myDelegate arithDelegate;

Now, the delegate reference arithDelegate can reference any method whose signature is identical to the signature of myDelegate.

The delegate reference can be made to reference any method with a matching signature by passing its name as the parameter of delegate:
myDelegate arithDelegate = new myDelegate(function_name);
 For calling our or passing the parameters to our actual function we can do some following stuff.
int r = arithDelegate(list of parameters);

Now, we have done each and every thing for the delegate. So, let's take an example to add two numbers as in the following:

Example: adding two numbers
using System;

namespace cSharpDotNet
{
 
    class Program
    {
        //Declaring our delegate
        delegate int arithMethod(int a, int b);
       
        //Function definition to whom delegate will reference
        private static int add(int a, int b)
        {
            return a + b;
        }
       
        static void Main(string[] args)
        {
           //initializing object of our delegate and also referring to the function
            arithMethod obj = new arithMethod(add);

            //Passing parameters to the delegate
            int num = obj(4,5);
            Console.WriteLine("The result of arithmetic operation '+' on 3 and 4 is :{0}", num);
            Console.ReadLine();
           
        }
    }
}
after building solution and debugging the above code this will show the following output on to the black screen.
 That's all from today.

Thank you! ! !