From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Why expressions are better for functional programs

Why expressions are better for functional programs - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Why expressions are better for functional programs

One fundamental difference between functional and imperative styles is that imperative code relies on statements, functional code relies on expressions. Since C# supports both styles, we should minimize the use of statements when possible. Focus on writing your code to favor expressions over statements. Languages like C# use statements such as the if statement and the for statement to perform logic tests and execute actions. With statements, the focus is on describing how tasks should be done, using statements to change the program state through loops, conditionals, and variable assignments. This approach emphasizes the steps involved with each statement, altering the state as the program progresses. While this is effective, it contrasts with functional programming, where the focus shifts to expressions. In contrast, functional programming emphasizes what to compute using expressions that produce values. In many cases, this value is the result of a calculation, but that is only a…

Contents