SlideShare a Scribd company logo
Functional programming in
C#
Tadeusz Balcer
DISCLAIMER
DISCLAIMER
I’M NOT AN EXPERT IN THIS
TOPIC!
DISCLAIMER
I’M NOT AN EXPERT IN THIS
TOPIC!
But I will try to show you what I have learnt 
Agenda
1. Introduction
a) What is functional programming?
b) Separation between functions and arguments (data)
c) Benefits of Functional Programming
2. Functional programming in C#
a) Data Objects
b) Honest functions
c) Tuple and ValueTuple
d) Option<T>
e) LINQ
What is Functional Programming?
What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
• Functional programming is programming with mathematical
functions - used functions should always provide the same results for the
same input.
What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
• Functional programming is programming with mathematical
functions - used functions should always provide the same results for the
same input.
• At very high level, it's a programming style that emphasizes functions
while avoiding state mutation.
What is Functional Programming?
• Functional programming is a paradigm which concentrates on computing
results rather than on performing actions.
• Functional programming is programming with mathematical
functions - used functions should always provide the same results for the
same input
• At very high level, it's a programming style that emphasizes functions
while avoiding state mutation.
• Functional programming is a style that treads computation as the
evaluation of mathematical functions and avoids changing-state and
mutable data
Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
• In functional programming, logic is encoded in functions and data is
captured with data objects, which are the input and output of
functions
Separation between functions and arguments
(data)
• In Object Oriented Programming, state (data) and methods
(functions) are bounded in objects
• In functional programming, logic is encoded in functions and data is
captured with data objects, which are the input and output of
functions
• Math functions are not class method. Math functions does not leave
any mark of it's exsistsence in outside world, while class methods may
change the state of an object.
Functions
Methods
Functional programming in practice
Aggregation of functions
Benefits of Functional Programming
• Tamed Side Effects
• Simple concurrency (no shared variables)
• Straightforward unit testing
• Different point of view for looking at problems
Benefits of Functional Programming
Benefits of Functional Programming
Functional programming in
C#
Functional Programming in C#
Making code more
functional
Functional Programming in C#
Use more specyfic
arguments and results
Data Objects instead of Primitive Types
Why use more specyfic data types?
• Primitive types are often not specific enough - for example, if you take
age, int can be -1000 as well as 1000.
Why use more specyfic data types?
• Primitive types are often not specific enough - for example, if you take
age, int can be -1000 as well as 1000.
• You’ll have to write additional unit tests for the cases in which
validation fails.
Why use more specyfic data types?
• Primitive types are often not specific enough - for example, if you take
age, int can be -1000 as well as 1000.
• You’ll have to write additional unit tests for the cases in which
validation fails.
• There are a few other areas of the application where an age is
expected, so you’re probably going to need the same validation in
those places. This will cause duplication.
Data Objects
Functional Programming in C#
Functional Programming in C#
Data Objects – interaction with primitive
types
Data Objects – interaction with primitive
types
Data Objects – implicit/explicit operators
Data Objects – implicite/explicit operators
Honest functions
Honest functions
• Honest function - it always honors signature. There is no other
possible outcome than defined (also no error throw). It just maps
each element of domain to an element of the codomain. Function is
honest if ts behavior can be predicted by its signature. No throwing
exceptions, and no null return values.
Honest functions
• Honest function - it always honors signature. There is no other
possible outcome than defined (also no error throw). It just maps
each element of domain to an element of the codomain. Function is
honest if ts behavior can be predicted by its signature. No throwing
exceptions, and no null return values.
• Dishonest function - give ma an int, and I may return a Risk.
Honest functions
Dishonest functions
Honest functions
Honest functions
Honest functions
Honest functions
DISHONEST SIGNATURE HONEST SIGNATURE
Composing values
Composing values with tuples
Tuple
TupleValue
Avoiding nulls
Functional Programming in C#
Avoiding null bomb – Option<T>
Avoiding null bomb – Option<T>
Functional Programming in C#
Avoiding null bomb – Option<T>
Avoiding null bomb – Option<T>
LINQ
Functional Programming in C#
Functional Programming in C#
LINQ
Functional Programming in C#
Functional Programming in C#
Functional Programming in C#
Summary
• Data Objects instead of Primitive Types
• Honest functions to have output for every possible input
• ValueTuple to aggregate Data Objects
• Option<T> to avoid nulls
• LINQ instead of foreach
Bibliography
• https://github.com/
louthy/language-
ext/wiki
Bibliography
• Functional Programming in C#: How to
write better C# code (2017)
Bibliography (Pluralsight)
• Functional Programming with C# (Dave Fencher) (2015)
• Applying Functional Principles in C# (Vladimir Khorikov)
(2016)
• Making Your C# Code More Functional (Zoran Horvat)
(2018)
Bibliography (Presentations)
• Railway oriented programming: Error handling in functional languages
by Scott Wlaschin (link) (2014)
• Functional Programming in C# (link) (2018)
Questions?
Thank you!

More Related Content

PDF
Ch. 16 Database Case Study: XML/XSLT
PPTX
RAJAT PROJECT.pptx
PDF
Functional Programming - Worth the Effort
PDF
Functional Programming in Ruby
PPTX
Different paradigms for problem solving.pptx
PPTX
Principled And Clean Coding
PPT
Basics of cpp
PDF
Functional Swift
Ch. 16 Database Case Study: XML/XSLT
RAJAT PROJECT.pptx
Functional Programming - Worth the Effort
Functional Programming in Ruby
Different paradigms for problem solving.pptx
Principled And Clean Coding
Basics of cpp
Functional Swift

Similar to Functional Programming in C# (20)

PPTX
Functional Programming.pptx
PPTX
XII Computer Science- Chapter 1-Function
PDF
Intro to Functional Programming @ Scala Montreal
PDF
Functional Go
PDF
Functional solid
PPTX
Improve Code Readability and Understanding Facilitate Code Reusability Across...
PPTX
Math with .NET for you and Azure
PDF
A Theory of Functional Programming LambdUp
PDF
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
PDF
Python functional programming
PPTX
a brief explanation on the topic of Imperative Programming Paradigm.pptx
PDF
C++ Programming with examples for B.Tech
PDF
Software Design And Analysis Ii Lecture Notes Cuny Csci235 Itebooks
PPTX
[OOP - Lec 02] Why do we need OOP
PDF
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
PPT
Basic terminologies & asymptotic notations
PDF
C++ [ principles of object oriented programming ]
PDF
OOP UNIT 1_removed ppt explaining object.pdf
PPTX
Lecture 1.pptx
Functional Programming.pptx
XII Computer Science- Chapter 1-Function
Intro to Functional Programming @ Scala Montreal
Functional Go
Functional solid
Improve Code Readability and Understanding Facilitate Code Reusability Across...
Math with .NET for you and Azure
A Theory of Functional Programming LambdUp
Kaush, Vitali - Title: Exploring the Power of V5 Sensors in Robotics: Enhanci...
Python functional programming
a brief explanation on the topic of Imperative Programming Paradigm.pptx
C++ Programming with examples for B.Tech
Software Design And Analysis Ii Lecture Notes Cuny Csci235 Itebooks
[OOP - Lec 02] Why do we need OOP
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Basic terminologies & asymptotic notations
C++ [ principles of object oriented programming ]
OOP UNIT 1_removed ppt explaining object.pdf
Lecture 1.pptx
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
top salesforce developer skills in 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
System and Network Administraation Chapter 3
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
CHAPTER 2 - PM Management and IT Context
medical staffing services at VALiNTRY
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Nekopoi APK 2025 free lastest update
How to Migrate SBCGlobal Email to Yahoo Easily
top salesforce developer skills in 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Design an Analysis of Algorithms I-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Softaken Excel to vCard Converter Software.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Designing Intelligence for the Shop Floor.pdf
System and Network Administraation Chapter 3
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Operating system designcfffgfgggggggvggggggggg
Digital Systems & Binary Numbers (comprehensive )
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
2025 Textile ERP Trends: SAP, Odoo & Oracle
CHAPTER 2 - PM Management and IT Context
Ad

Functional Programming in C#

  • 3. DISCLAIMER I’M NOT AN EXPERT IN THIS TOPIC!
  • 4. DISCLAIMER I’M NOT AN EXPERT IN THIS TOPIC! But I will try to show you what I have learnt 
  • 5. Agenda 1. Introduction a) What is functional programming? b) Separation between functions and arguments (data) c) Benefits of Functional Programming 2. Functional programming in C# a) Data Objects b) Honest functions c) Tuple and ValueTuple d) Option<T> e) LINQ
  • 6. What is Functional Programming?
  • 7. What is Functional Programming? • Functional programming is a paradigm which concentrates on computing results rather than on performing actions.
  • 8. What is Functional Programming? • Functional programming is a paradigm which concentrates on computing results rather than on performing actions. • Functional programming is programming with mathematical functions - used functions should always provide the same results for the same input.
  • 9. What is Functional Programming? • Functional programming is a paradigm which concentrates on computing results rather than on performing actions. • Functional programming is programming with mathematical functions - used functions should always provide the same results for the same input. • At very high level, it's a programming style that emphasizes functions while avoiding state mutation.
  • 10. What is Functional Programming? • Functional programming is a paradigm which concentrates on computing results rather than on performing actions. • Functional programming is programming with mathematical functions - used functions should always provide the same results for the same input • At very high level, it's a programming style that emphasizes functions while avoiding state mutation. • Functional programming is a style that treads computation as the evaluation of mathematical functions and avoids changing-state and mutable data
  • 11. Separation between functions and arguments (data) • In Object Oriented Programming, state (data) and methods (functions) are bounded in objects
  • 12. Separation between functions and arguments (data) • In Object Oriented Programming, state (data) and methods (functions) are bounded in objects • In functional programming, logic is encoded in functions and data is captured with data objects, which are the input and output of functions
  • 13. Separation between functions and arguments (data) • In Object Oriented Programming, state (data) and methods (functions) are bounded in objects • In functional programming, logic is encoded in functions and data is captured with data objects, which are the input and output of functions • Math functions are not class method. Math functions does not leave any mark of it's exsistsence in outside world, while class methods may change the state of an object.
  • 18. Benefits of Functional Programming • Tamed Side Effects • Simple concurrency (no shared variables) • Straightforward unit testing • Different point of view for looking at problems
  • 19. Benefits of Functional Programming
  • 20. Benefits of Functional Programming
  • 25. Use more specyfic arguments and results Data Objects instead of Primitive Types
  • 26. Why use more specyfic data types? • Primitive types are often not specific enough - for example, if you take age, int can be -1000 as well as 1000.
  • 27. Why use more specyfic data types? • Primitive types are often not specific enough - for example, if you take age, int can be -1000 as well as 1000. • You’ll have to write additional unit tests for the cases in which validation fails.
  • 28. Why use more specyfic data types? • Primitive types are often not specific enough - for example, if you take age, int can be -1000 as well as 1000. • You’ll have to write additional unit tests for the cases in which validation fails. • There are a few other areas of the application where an age is expected, so you’re probably going to need the same validation in those places. This will cause duplication.
  • 32. Data Objects – interaction with primitive types
  • 33. Data Objects – interaction with primitive types
  • 34. Data Objects – implicit/explicit operators
  • 35. Data Objects – implicite/explicit operators
  • 37. Honest functions • Honest function - it always honors signature. There is no other possible outcome than defined (also no error throw). It just maps each element of domain to an element of the codomain. Function is honest if ts behavior can be predicted by its signature. No throwing exceptions, and no null return values.
  • 38. Honest functions • Honest function - it always honors signature. There is no other possible outcome than defined (also no error throw). It just maps each element of domain to an element of the codomain. Function is honest if ts behavior can be predicted by its signature. No throwing exceptions, and no null return values. • Dishonest function - give ma an int, and I may return a Risk.
  • 47. Tuple
  • 51. Avoiding null bomb – Option<T>
  • 52. Avoiding null bomb – Option<T>
  • 54. Avoiding null bomb – Option<T>
  • 55. Avoiding null bomb – Option<T>
  • 56. LINQ
  • 59. LINQ
  • 63. Summary • Data Objects instead of Primitive Types • Honest functions to have output for every possible input • ValueTuple to aggregate Data Objects • Option<T> to avoid nulls • LINQ instead of foreach
  • 65. Bibliography • Functional Programming in C#: How to write better C# code (2017)
  • 66. Bibliography (Pluralsight) • Functional Programming with C# (Dave Fencher) (2015) • Applying Functional Principles in C# (Vladimir Khorikov) (2016) • Making Your C# Code More Functional (Zoran Horvat) (2018)
  • 67. Bibliography (Presentations) • Railway oriented programming: Error handling in functional languages by Scott Wlaschin (link) (2014) • Functional Programming in C# (link) (2018)