(C# Language) Expression-bodied Members

If a function consists of only one statement or expression, then the braces and the return keyword could be redundant. Thus, we can simplify such functions by removing the braces and the return keyword. The result is that the overall look of the function becomes simplified, and it becomes easier to read. Functions, properties, operators, and indexers that use this syntax are called expression-bodied members.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

Simplifying a single-liner function

Suppose we have a function that adds two numbers and its usual syntax is as shown


int sum(int x, int y)
{
  return x + y;
}

The function body can be simplified by using this syntax -


int sum(int x, int y) => x + y;

The braces have been removed because the function body was of just a single expression. The return keyword too has been removed because the compiler knows that the return datatype is int, and that the expression evaluates or converts to the same data type i.e., an int.

Video Explanation (see it happen!)

Please watch the following youtube video:

Simplifying a Property

Suppose we have a class that has a property with getter, or setter or both of them consisting of a single statement.


internal class Program
{
    // properties also 
    int _k;

    // property with single statement 
    // getter and setter 
    public int K
    {
      get 
      {
        return _k; 
      }
      
      set
      {
        _k = value;
      }
    }
}

The property can be condensed with the same syntax as seen here.


internal class Program
{
    // properties also 
    int _k;

    // property with single statement 
    // getter and setter 
    public int K
    {
      get => _k;
      set => _k = value;
    }
}

Another Example

Suppose we have a long function called fx. And suppose that there is another function px that calls this function.


internal class Program
{
  int fx()
  {
    int j = 32;

    j *= 2;

    return j;
  }

  // can be condensed 
  int px() 
  {
    int m = fx();

    return m;
  }
}

If the function px can be condensed into a single statement, then it can be simplified as seen here.


internal class Program
{
  int fx()
  {
    int j = 32;

    j *= 2;

    return j;
  }

  // px can be condensed thus 
  int px() => fx();

}


Project we created

We have already created a project that you can obtain from the downloads attached to this video. We can have a look at it now. Open the solution explorer and locate the program.cs file. Double-click to open it!


using System;

namespace MyConsoleApp
{
  internal class Program
  {
    // --- EXAMPLE 1 --- // 
    
    int sum(int x, int y) => x + y;

    // --- EXAMPLE 2 --- // 
    
    // suppose backing variable 
    int _k;
    
    // property simplified this -  
    public int K
    {
      get => _k;
      set => _k = value;
    }
    
    // --- EXAMPLE 3 --- // 
    
    // suppose we have a 
    // lengthy function  
    int fx()
    {
      int j = sum(5, 8);

      j *= 2;

      return j;
    }

    // suppose fx called from another 
    // px in a statement.  
    // then px can be simplified thus 

    int px() => fx() + sum(3, 5);

    // main function 
    static void Main(string[] args)
    {
      var p = new Program();

      Console.WriteLine($"sum = {p.sum(4, 5)}");

    }
  }
}

The first example shows a simplified function.

The second example shows a simplified property.

After that we have a function fx. This function is called from another single-liner function called px. The syntax for px has been simplified using the expression-body syntax.

Exercises on Expression Bodied Members

  1. Create a console project with the class Program as usual. Add a private member array (call it mAllInt) of int to this class with about five int numbers. Add an index accessor public property (this [int i]) that has only a get member that returns the i-th item of mAllInt. Use the expression body syntax. Inside main create an object of Program and print the third item of the array.
  2. Create a console project of a class called Program. This class has two private string members - firstName and lastName. Add a constructor that takes two args to initialize the two string members using the expression body syntax. Also add a get property called Name that returns a concatenation of firstName and lastName. Inside main create an object of your class and print the Name property.
  3. For solutions -> Please join our Course on C# Language
COMMERCIAL USE OF THIS CONTENT IS NOT ALLOWED.

This Blog Post/Article "(C# Language) Expression-bodied Members" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.