DEV Community

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

Posted 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)
    {
        _balance += amount;
    }

    public void Withdraw(decimal amount)
    {
        _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 (0)