none
delegate and action RRS feed

  • Question

  • i want to know about the difference between delegate and action ?

    give some example please.

     

    and if there is some difference then mention . 

     


    maifs

    Wednesday, October 19, 2011 6:17 PM

Answers

  • An Action is a delegate.  It is a delegate type defined within the framework.

    Action(T) Delegate (System) 

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, October 19, 2011 10:05 PM
    Moderator
  • Hi Mehmood,

    Just like Rudy said Action is a delegate type defined within the framework and another one is Func<TResult> .

    The different between Action and Func is that Func return a value but Action does not.

    You can use Action like this:

    class Program

    {

        static void Main(string[] args)

        {

            Action<string> act = new Action<string>(HelloSomebody);

            act("Bob");

        }

     

        static void HelloSomebody(string name)

        {

            Console.WriteLine("Hello {0}!", name);

        }

    }

     

    It is same if you use delegate like this:

    class Program

    {

        public delegate void sampleDelegate(string s);

        static void Main(string[] args)

        {

            sampleDelegate sd = new sampleDelegate(HelloSomebody);

            sd("Bob");

        }

     

        static void HelloSomebody(string name)

        {

            Console.WriteLine("Hello {0}!", name);

        }

    }

     

    Here is the link of delegates:

    http://msdn.microsoft.com/en-us/library/ms173171(v=VS.80).aspx

    Hope this helps.

    Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 20, 2011 9:23 AM
    Moderator

All replies

  • An Action is a delegate.  It is a delegate type defined within the framework.

    Action(T) Delegate (System) 

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, October 19, 2011 10:05 PM
    Moderator
  • Hi Mehmood,

    Just like Rudy said Action is a delegate type defined within the framework and another one is Func<TResult> .

    The different between Action and Func is that Func return a value but Action does not.

    You can use Action like this:

    class Program

    {

        static void Main(string[] args)

        {

            Action<string> act = new Action<string>(HelloSomebody);

            act("Bob");

        }

     

        static void HelloSomebody(string name)

        {

            Console.WriteLine("Hello {0}!", name);

        }

    }

     

    It is same if you use delegate like this:

    class Program

    {

        public delegate void sampleDelegate(string s);

        static void Main(string[] args)

        {

            sampleDelegate sd = new sampleDelegate(HelloSomebody);

            sd("Bob");

        }

     

        static void HelloSomebody(string name)

        {

            Console.WriteLine("Hello {0}!", name);

        }

    }

     

    Here is the link of delegates:

    http://msdn.microsoft.com/en-us/library/ms173171(v=VS.80).aspx

    Hope this helps.

    Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 20, 2011 9:23 AM
    Moderator