#1,152 – The Action Delegate Type

The .NET Framework includes the Action predefined delegate type that represents the signature of a method that accepts zero or more parameters and does not have a return value (i.e. returns void).

Overloads of Action are :

  • delegate void Action()
  • delegate void Action(T p1)
  • delegate void Action(T1 p1, T2 p2)
  • etc…

You might define your own delegate type:

        public delegate void Merger<T1,T2>(T1 thing1, T2 thing2);

        static void MergeIntAndString<T1,T2>(Merger<T1,T2> myMerger, T1 n, T2 s)
        {
            myMerger(n, s);
        }

        public static void ConcatIntString(int n, string s)
        {
            Console.WriteLine("{0}{1}", n, s);
        }

        static void Main(string[] args)
        {
            MergeIntAndString(ConcatIntString, 42, "Adams");
        }

However, you could instead use the Action delegate type, avoiding the definition of your own custom delegate type.

        static void MergeIntAndString<T1,T2>(Action<T1,T2> myMerger, T1 n, T2 s)
        {
            myMerger(n, s);
        }

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,152 – The Action Delegate Type

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: