Functional Programming on .NET - The Best of Both Worlds - Isaac Abraham - NDC Oslo 2024

Learn how functional programming in .NET combines the best of both worlds, exploring F#'s advantages over C# for FP while maintaining access to the full .NET ecosystem.

Key takeaways
  • Functional programming fundamentally comes down to two core concepts: expressions (everything returns a value) and immutability (data cannot be changed after creation)

  • C# is primarily an object-oriented language that has added functional features over time, but will always have mutable state at its heart and isn’t optimized for functional programming

  • Key functional programming patterns include:

    • Pure functions instead of stateful objects
    • Immutable data structures
    • Expression-based programming over statements
    • Separation of data and behavior
    • Function composition
  • While C# has functional features like lambdas, records, and pattern matching, these are primarily syntactic sugar and don’t make the language truly functional

  • F# provides a more natural functional programming experience on .NET because it was designed as a functional-first language, while still allowing object-oriented programming when needed

  • Benefits of functional programming include:

    • Easier testing due to pure functions
    • Fewer bugs from immutability
    • Simpler composition of functions
    • Better concurrency with no shared mutable state
    • More maintainable code through separation of concerns
  • When doing functional programming in C#, focus on:

    • Using static methods over instance methods
    • Immutable types and data structures
    • Expression-based code where possible
    • Keeping mutable state at the edges of the application
  • Rather than trying to force functional programming in C#, consider F# for a better functional programming experience while still leveraging the .NET ecosystem

  • The functional approach works particularly well for cloud computing, data processing pipelines, and concurrent programming scenarios

  • You don’t need to learn advanced concepts like monads or category theory to benefit from functional programming - focus on the fundamentals of expressions and immutability