# Differences between the Action and Func Delegates in C#

<p>I had always wanted to learn and use the Action and Func delegates in C#. Coincidentally, I wrote on this topic lately, and I am glad to share my learnings here.</p>
<p>In this article, I will discuss the differences between the Action and Func delegate types in C#. I will begin with a definition of delegates in C#, and then proceed to explore the differences between the Action and the Func delegate type.</p>
<p>So, let's jump in.</p>
<h2><a id="H2"></a>What Is a Delegate?</h2>
<p>A <code>delegate</code> in C# is a pointer to a method. It is a reference type that a method can be assigned to. We can use a delegate to pass a method as a parameter to another method.</p>
<h2><a id="H2"></a>Action and Func Delegates</h2>
<p>Generic delegates were introduced to make the definition of delegates terser and reduce the need to create custom delegates. The <code>Action</code> and <code>Func&lt;TResult&gt;</code> delegates are two sets of generic delegates that were introduced in .NET 3.5. We use these delegate types to encapsulate a method, without having to explicitly define a custom delegate.</p>
<h2><a id="H2"></a>What is the difference between Action and Func Delegates</h2>
<p>We use the <code>Action</code> delegate to reference a method whose signature matches that defined by the delegate. Since the <code>Action</code> delegate doesn't return a value, the return type of the method it references must be <code>void</code>.</p>
<p>While the <code>Func&lt;TResult&gt;</code> delegate is used to encapsulate a method whose signature is the same as that defined by the delegate but returns a value.</p>
<h2><a id="H2"></a>Action Delegate</h2>
<p>To understand how the Action delegate works, let's create a method, <code>Print</code> in the <code>Calculator</code>  class that prints a string on the console:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public void Print()
{
    Console.WriteLine("The answer is 200");
}</pre>
<p>Next, let's create an <code>Action</code> delegate that references this method:</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="generic">Action printDelegate = calc.Print;</code></p>
<p>Finally, we can call the delegate and the method it references in the <code>Main</code> method of the <code>Program</code> class:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">Console.Write("Method:");
calc.Print();
Console.Write("Delegate:")
printDelegate();</pre>
<p>At this point, we can start the app and view the result on the console:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">Method:The answer is 200
Delegate:The answer is 200</pre>
<p>As we can see, we can call a method directly, or use a delegate to do the same. Hence, we can call a delegate and pass parameters if required to execute a method the delegate encapsulates.</p>
<h3><a id="H3"></a>Using Action&lt;T&gt;</h3>
<p><code>Action&lt;T&gt;</code> is the generic variant of <code>Action</code>. We use it to encapsulate a method that has only one parameter and returns void.</p>
<p>To understand how the <code>Action&lt;T&gt;</code> delegate works, let's define a method <code>PrintResult</code> in the <code>Calculator</code> class. This method takes a single integer parameter and displays it on the Console:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public void PrintResult(int result)
{
    Console.WriteLine($"The result is {result}")
}</pre>
<p>We can now define an <code>Action&lt;T&gt;</code> delegate that references the <code>PrintResult</code> method:</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="generic">Action&lt;int&gt; actionDelegate = calc.PrintResult;
</code></p>
<p>Now, we can use this delegate to execute the method <code>PrintResult</code>. When we call the delegate, we need to pass in the required parameter.</p>
<p>Moreso, we can also use the <code>Action&lt;T&gt;</code> overloads to reference methods that have up to 16 parameters:</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="generic">Action&lt;Type1, Type2, Type3, ..., Type16&gt; actionDelegate = SomeMethod;
</code></p>
<h2><a id="H2"></a>Func&lt;TResult&gt; Delegate</h2>
<p>We use the <code>Func&lt;TResult&gt;</code> delegate type to reference a method that matches the defined signature, that is, it has no parameter but has a return value.</p>
<p>To illustrate, let's define a method <code>Result</code> in the <code>Calculator</code> class that returns an integer:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public int Result()
{
    var result = Sum(5, 5);
    return result;
}
</pre>
<p>Next, we instantiate a <code>Func&lt;TResult&gt;</code> delegate that points to the <code>Result</code> :</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="generic">Func&lt;int&gt; resultDelegate = calc.Result;</code></p>
<p>Now, whenever we invoke this delegate, it executes the method <code>Result</code>. As we can see, when we use delegates, we are provided with increased flexibility and functionality.</p>
<h3><a id="H3"></a>Using Func&lt;T, TResult&gt;</h3>
<p>We use the Func&lt;T, TResult&gt; delegate type and its overloads to encapsulate methods that have 1- 16 parameters, and a return value:</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="generic">Func&lt;Type1, Type2, Type3, ..., TResult&gt; funcDelegate = SomeMethod2;</code></p>
<p>It is important to note that the last type defined is that of the return value of the method referenced.</p>
<p>As an illustration,  let's define a method <code>Exponent</code> in the <code>Calculator</code> class, that takes in an integer input <code>number</code>, and returns the square of the integer:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public int Exponent(int number)
{
   var result = number * number;

   return result;
}</pre>
<p>Next, we create <code>Func&lt;TResult&gt;</code> delegate that encapsulates the method <code>Exponent</code>:</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="generic">Func&lt;int, int&gt; exponentDelegate = calc.Exponent;       </code></p>
<p>Finally, we can now call this delegate whenever we want to execute the method, and we will get the same result.</p>
<h2><a id="H2"></a>Conclusion</h2>
<p>We have learned about the Action and Func delegates in this article. We explored the differences between them, and how we can use them in practice.</p>
