DEV Community

Cover image for Key Features of Functional Programming in C#
waelhabbal
waelhabbal

Posted on • Edited on

Key Features of Functional Programming in C#

What is Functional Programming?

Functional programming is a programming paradigm that emphasizes the use of pure functions, immutability, and the avoidance of changing state. It's a way of writing code that focuses on what the code should do, rather than how it should do it.

Key Features of Functional Programming in C#:

  1. Immutability: In functional programming, data is immutable, meaning it cannot be changed once it's created. This makes it easier to reason about the code and reduces the risk of bugs.

Example:

// Immutable class
public class Person
{
    private readonly string _name;
    private readonly int _age;

    public Person(string name, int age)
    {
        _name = name;
        _age = age;
    }

    public string Name { get { return _name; } }
    public int Age { get { return _age; } }
}
Enter fullscreen mode Exit fullscreen mode
  1. Pure Functions: Pure functions are functions that always return the same output given the same inputs and have no side effects.

Example:

// Pure function
public int CalculateTotal(int[] numbers)
{
    return numbers.Sum();
}
Enter fullscreen mode Exit fullscreen mode
  1. Lambda Expressions: Lambda expressions are small, anonymous functions that can be defined inline within a method or expression.

Example:

// Lambda expression
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Sort((x, y) => x.CompareTo(y));
Enter fullscreen mode Exit fullscreen mode
  1. Extension Methods: Extension methods are methods that can be added to existing types without modifying their definition.

Example:

// Extension method
public static class StringExtensions
{
    public static string ToUpper(this string str)
    {
        return str.ToUpper();
    }
}

string myString = "hello";
string upperCaseString = myString.ToUpper();
Enter fullscreen mode Exit fullscreen mode
  1. Expression Trees: Expression trees are a way to represent code as data, allowing you to manipulate and analyze code at runtime.

Example:

// Expression tree
Expression<Func<int, int>> expression = x => x * x;

// Compile the expression tree
Delegate compiled = expression.Compile();

int result = compiled.Compile().Invoke(5); // returns 25
Enter fullscreen mode Exit fullscreen mode
  1. Pattern Matching: Pattern matching is a way to match an object against a set of patterns and execute different blocks of code based on the result.

Example:

// Pattern matching
string input = "hello";

if (input switch
{
    "hello" => Console.WriteLine("Hello!"),
    "goodbye" => Console.WriteLine("Goodbye!"),
    _ => Console.WriteLine("Invalid input")
})
{
    Console.WriteLine("Pattern matched!");
}
Enter fullscreen mode Exit fullscreen mode

How to Use Functional Programming in C#:

  1. Use Immutable Data Structures: Use immutable data structures instead of mutable ones to reduce bugs and improve performance.

Example:

// Immutable data structure
public class BankAccount
{
    private readonly decimal _balance;

    public BankAccount(decimal balance)
    {
        _balance = balance;
    }

    public void Deposit(decimal amount)
    {
       return new BankAccount(_balance + amount);
    }

    public void Withdraw(decimal amount)
    {
       return new BankAccount(_balance - amount);
    }

    public decimal GetBalance()
    {
        return _balance;
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Compose Functions: Compose small, pure functions together to create more complex functionality.

Example:

// Composed function
Func<int, int> doubleAndAddOne = x => x * 2 + 1;
Func<int, int> addThree = x => x + 3;

int result = doubleAndAddOne.Compose(addThree).Invoke(5); // returns 13
Enter fullscreen mode Exit fullscreen mode
  1. Use Lambda Expressions: Use lambda expressions to create small, anonymous functions that can be used inline.

Example:

// Lambda expression
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Sort((x, y) => x.CompareTo(y));
Enter fullscreen mode Exit fullscreen mode
  1. Use Extension Methods: Use extension methods to add functionality to existing types without modifying their definition.

Example:

// Extension method
public static class StringExtensions
{
    public static string ToUpper(this string str)
    {
        return str.ToUpper();
    }
}

string myString = "hello";
string upperCaseString = myString.ToUpper();
Enter fullscreen mode Exit fullscreen mode
  1. Use Expression Trees: Use expression trees to manipulate and analyze code at runtime.

Example:

// Expression tree
Expression<Func<int, int>> expression = x => x * x;

// Compile the expression tree
Delegate compiled = expression.Compile();

int result = compiled.Compile().Invoke(5); // returns 25
Enter fullscreen mode Exit fullscreen mode
  1. Use Pattern Matching: Use pattern matching to match an object against a set of patterns and execute different blocks of code based on the result.

Example:

// Pattern matching
string input = "hello";

if (input switch
{
    "hello" => Console.WriteLine("Hello!"),
    "goodbye" => Console.WriteLine("Goodbye!"),
    _ => Console.WriteLine("Invalid input")
})
{
    Console.WriteLine("Pattern matched!");
}
Enter fullscreen mode Exit fullscreen mode

Functional programming in C# is a paradigm that emphasizes immutability, pure functions, and avoiding changing state. This approach can help reduce bugs, improve performance, and make code more maintainable and scalable. By applying functional programming principles and techniques, you can write more robust and efficient C# code.

Top comments (2)

Collapse
 
richard_keene_f6ecb7a747f profile image
Richard Keene

The example for immutable class,
// Immutable data structure
public class BankAccount

Is not correct.

That is a highly mutable class where the balance can be changed, and will not even compile.
A correct class would be where for example Deposit would read like:
public BankAccount Deposit(decimal amount)
{
return new BankAccount(_balance + amount);
}

Now you know the balance can never change. You create a new copy when you add. This makes the class very thread safe.

Similar code is for strings, like where
string s = "Hello";
string x = s + " World";

There the string held by s never changes after being created. Strings in C# are always readonly.

Collapse
 
waelhabbal profile image
waelhabbal

Hi Richard Keene,

Thank you so much for taking the time to read my post and for your insightful feedback! I really appreciate your suggestions regarding the immutable data structure.

I agree that the example for the immutable class is not correct, and I will make sure to update my post accordingly to reflect this improvement. It's always great to learn from the community, and your input is very helpful.

If you have any more thoughts or if there's anything else you'd like to discuss, Iā€™d love to hear more. Thanks again for your help!

Best,
Wael Habbal