Pages

Saturday, January 29, 2011

28/1 Indexers , Delegates:- -



DATE 28/1/11 Friday

Indexers – These are near similar to Properties that are used for providing access to an array outside of the class. Once an Indexer is defined on a class the object of that class will work like an array providing access to the values of array i.e. defined inside it.
- Add a class IndexerDemo.cs and write the following:
using System;
namespace oopsProject
{
    public class IndexerDemo
    {
        //Private array declaretion
        string[] arr;
        public IndexerDemo(int size)
        {
        // initializing array with a size
            arr = new string[size];
        // Assigning the default value to array
            for (int i = 0; i < size; i++)
                arr[i] = "empty";
        }
        // defining an indexer to access the array out of the class

        public string this[int index]
        {
            get { return arr[index]; }
            set { arr[index] = value; }
        }
    }
}



- Add a class TestIndexer.cs and write the following:
using System;
namespace oopsProject
{
    class TestIndexer
    {
       
        static void Main()
        {
            IndexerDemo obj = new IndexerDemo(6);
            for (int i = 0; i < 6; i++) ;
            Console.Write(obj[i] +" ");
            Console.WriteLine();
            obj[1] = "red"; obj[3] = "blue"; obj[5] = "green";
            for (int i = 0; i < 6; i++) ;
            Console.Write( obj[i] + " ");
            Console.ReadLine();
       
        }

        public static int i { get; set; }
    }
}



Delegates:-  
1.      These are similar to function pointers, which are avilabel in our traditional languages like C and C++ which provides an option to invoke a function with them, which will be faster in execution.
2.       These function pointers are provided under .net languages in a form of Delegates which can be referred as pointer to the method.
3.      Invocation of a method can be done in Two different ways –
                                I.            We can invoke a method directly using object of the class.
                              II.            We can invoke a method without object of a class using a Delegate, which is faster in execution when compared with first process.
Note – A Delegate is also a type (Reference type).
Using Delegates: - To use a delegate adopt blow process.
Step 1:
Delegate Declearetion: - Here we need to declare a delegate for invoking the method where IO parameters for the Delegate should matched with IO parameters of Method it has to call.
Syntax –
[<modifiers>] delegate <void |type> <name> [<param def’s>]


 Eg:
public void Add (int x, int y)
{
Console.WriteLine(x +y);
}
public delegate void AddDel( int x, int y);



Eg 2:
public string SayHello (string name)
{
return “Hello “ + name;
}
public delegate string SayDel(string name);


Step 2:
Creating Object of Delegate: - as a delegate is also a type, to consume it we need to create object of it. While creating the object we need to pass the method as a parameter to its constructor, which we want to call using the delegate.
AddDel ad = new AddDel(Add);
SayDel sd = new SayDel(SayHello);

Step 3:
Invoking the Delegate to Execute the Method: - Now we can call the delegate for any number of times so that the method gets executed, where each delegate uses a separate Stack to execute the method for any number of times.

ad (100,50) ; ad (1046,54) ; ad (237,265) ;
sd (“xxx”); sd (“yyy”); sd (“zzz”);

Note – A delegate can be declared either within a namespace or within a class also.
- Add a class DelClass.cs and write the following:
using System;
namespace oopsProject
{
    class DelClass
    {
        public string SayHello(String name)
        {
            return "Hello " + name;

        }
        public delegate string SayDel(string name);
        static void Main()
        {

            DelClass obj = new DelClass();
            SayDel sd = new SayDel(obj.SayHello);
            Console.WriteLine(sd("prabhu"));
            Console.WriteLine(sd("nitya"));
            Console.WriteLine(sd("choure"));
            Console.ReadLine();
        }
    }
}



Delegetes are of two types:-
1.       Uni-cast or Single-Cast delegates
2.      Multi- Cast delegates
- If a delegate is used for invoking only a single method, it is a single cast delegate.
- If we can use a delegate for invoking more than one method also i.e. a multi-cast delegate, but if we want to invoke multiple methods using a single delegate then all the method should have the same io parameters.
 - Add a class MultiDel.cs and write the following:
using System;
namespace oopsProject
{
    class MultiDel
    {
        public void Add(int x, int y)
        {
            Console.WriteLine("Add : " + (x + y));
        }
        public void sub(int x, int y)
        {
            Console.WriteLine("sub : " + (x - y));
        }
        public void mul(int x, int y)
        {
            Console.WriteLine("mul : " + (x * y));
        }
        public void div(int x, int y)
        {
            Console.WriteLine("div : " + (x / y));
        }
        static void Main()
        {
            MultiDel obj = new MultiDel();
            Math m = new Math(obj.Add);
            m += obj.sub; m += obj.mul; m += obj.div;
            m(100, 50);
            Console.WriteLine();
            m(1844, 5230);
            Console.WriteLine();
            m -= obj.mul;
            m(1023, 523);
            Console.WriteLine();
            Console.ReadLine();
        }
 
 
    }
}



Note – The advantage with multicast delegate is, a single delegate call will execute all the method that is bound to it at a time.

0 comments:

Post a Comment