How to Create an Inline Function in C#

Muhammad Maisam Abbas Feb 16, 2024
  1. Create Inline Functions With Lambda Expressions in C#
  2. Create Inline Functions With Lambda Statements in C#
  3. Create Inline Functions With Local Functions in C#
How to Create an Inline Function in C#

In this tutorial, we will discuss methods to create an inline function in C#.

Create Inline Functions With Lambda Expressions in C#

In programming languages like C and C++, an inline function is declared with the inline keyword. The code inside an inline function gets substituted with the function call by the compile. Hence, making the code inline. Unfortunately, there is no built-in keyword to declare an inline function in C#. We can create an inline function with lambda expressions in C#. Lambda expressions are used to create anonymous functions in C#. The => keyword is used to write lambda expressions.

The following code example shows us how to create an inline function with lambda expressions in C#.

using System;

namespace inline_function {
  class Program {
    static void Main(string[] args) {
      Func<int, int, int> add = (x, y) => x + y;
      Console.WriteLine(add(1, 2));
    }
  }
}

Output:

3

We created an inline function that returns the sum of 1 and 2 with lambda expressions in C#. We created the add function that returns the sum of two integer variable using the => keyword. The Func<int, int, int> specifies the data types of parameters, and the last int in the Func<int, int, int> specifies the return type of the anonymous function. We can also use Action<T1, T2> keyword to specify anonymous functions if we do not want to return anything. The following code example shows us how we can use the Action<T1, T2> keyword to create an inline function that does not return any value in C#.

using System;

namespace inline_function {
  class Program {
    static void Main(string[] args) {
      Action<int, int> sum = (x, y) => Console.WriteLine(x + y);
      sum(5, 6);
    }
  }
}

Output:

11

We created an inline function that returns the sum of 5 and 6 with lambda expressions in C#. We created the sum() function that returns the sum of two integer variable using the => keyword. The Action<int, int> specifies the data types of parameters of the anonymous function. We can use lambda expressions only for a single line of code.

Create Inline Functions With Lambda Statements in C#

If we have more than one line of code in our code, we have to use lambda statements. Lambda statements are also used to declare anonymous functions which can be used as inline functions. Declaring anonymous functions in lambda statements are similar to lambda expressions, with the only difference being that the multi-line statements are enclosed in {};. The following code example shows us how to create an inline function with lambda statements in C#.

using System;

namespace inline_function {
  class Program {
    static void Main(string[] args) {
      Action<int, int> sum = (x, y) => {
        int s = x + y;
        Console.WriteLine(s);
      };
      sum(6, 7);
    }
  }
}

Output:

13

We created an inline function that displays the sum of 6 and 7 with lambda statements in C#. We can write multiple lines of code inside the sum() function. Just like lambda expressions, there are two types of lambda statements, the Func<T,T-return> which returns a value, and the Action<T>, which does not return a value.

Create Inline Functions With Local Functions in C#

Local functions are the functions contained inside another function in C#. A local function can only be accessed by the function enclosing it. Local functions are available in 7.0 and higher versions of C#. Local functions can be used to provide the functionality of inline functions in C#. The following code example shows us how to create an inline function with local functions in C#.

using System;

namespace inline_function {
  class Program {
    static void Main(string[] args) {
      void sum(int a, int b) {
        Console.WriteLine(a + b);
      }
      sum(7, 11);
    }
  }
}

Output:

18

We created an inline function that returns the sum of 7 and 11 with local functions in C#. Declaring a local function is the same as declaring a normal conventional function. It is just declared without an access specifier and inside another function.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp Function