Advanced C#: Functional Programming Patterns

Advanced C#: Functional Programming Patterns

3h 11mAdvanced2024-12-02

Authors

Walt Ritscher

Walt Ritscher

Programmer, UI Designer, Staff Instructor at LinkedIn Learning

Learn-it Labs

Learn-it Labs

Course details

In this course for advanced developers, Walt Ritscher guides you through the fundamentals and advanced concepts of functional programming in C#. Learn how to write pure functions and refactor impure ones to minimize side effects. Discover how to leverage higher-order functions, immutable types, and functional function concepts for better code reliability and maintainability. Understand and implement patterns like monads, currying, and function composition. Use LINQ for filtering, transforming, and mapping data efficiently. Learn to create and use the Either type for error handling and handle randomness in a functional manner. This course is perfect for software developers, coding enthusiasts, and anyone interested in enhancing their programming skills with functional techniques. By the end of this course, you will be able to write clean, reliable, and efficient code in C# while adopting a functional programming mindset.

Skills covered

C#Programming FoundationsAdvancedProgramming LanguagesMicrosoftSoftware Development

Concepts

Introduction

  • Functional programming patterns
  • What you should know
  • How to access the sample code on GitHub

Overview

  • What is functional programming
  • Core principles of functional programming
  • Don't be making side effects
  • Monads, currying and other terms
  • The functional parts of C#

Favor Pure Functions

  • Function categories
  • Write pure functions for better code
  • Example of an impure function
  • Refactor impure function to pure
  • Reduce code side effects
  • Refactor side effect example
  • Don't mutate input arguments
  • Use an immutable argument
  • What about impure class properties

Side Effects and I O

  • Work with files
  • Calculate the total in a pure function
  • Filter with LINQ Where

Functional Functions

  • Functional function concepts
  • Use Func T for first class functions
  • Higher-order - LINQ
  • Higher-order - filter example
  • Higher-order - transform example
  • Return function from function call

Immutable Types

  • Principles of immutable types
  • Simple immutable example
  • More principles
  • Read-only properties
  • Instance method in immutable type
  • Use helper methods to create new instance
  • Use factory method to create instance

Choose Expressions

  • Why expressions are better for functional programs
  • Some examples of C# expressions
  • Rewrite IF statement with expression
  • Rewrite SWITCH statement with expression
  • Rewrite FOREACH with expression
  • IsPrime function as an expression

Function Composition and Pipelining

  • Learn the composition patterns
  • Compose functions
  • Pipelining with extension methods
  • Pipelining with generic methods
  • Pipelining with IEnumerable

Work with Lists

  • Understand Map and other concepts
  • Map with LINQ SELECT
  • Map to a different type
  • Filter with LINQ WHERE
  • Flatten with SelectMany
  • Join with SelectMany
  • Fold with SUM and AGGREGATE

Randomness

  • Randomness in functional programming
  • A functional Random class
  • Specify a smaller range of random numbers
  • Extension methods for various types
  • Create a random list
  • Randomize the order of a list

Other Functional Types

  • Replace VOID with the UNIT class
  • Replace NULL with OPTION
  • Explore the Either type for error handling

Continuing Your Journey

  • Next steps
80,000 Toman