C# Functional Programming
Tags: csharp, functional, dotnet
Published: 2019-07-16 17:56:03

Let’s start with a question:

> Is it worth it?

Like almost everything in software development, It depends… I really like the functional programming paradigm, then, probably there’s some bias here. But let’s trying to be reasonable, right?

The first major point is: why to use C#? — If you want to use functional programming there’s a huge probability that **F#** fits better than C# in a .NET environment.

There’s a little problem here, a lot of .NET developers have no idea what is F#, I genuinely think that this is a huge mistake, but it’s topic for another article. So, let’s keep it at C# and this bring us to the next question:

> Do you like System.Linq?

The answer probably will be: **_YES!_**

Because System.Linq is awesome and guess what, it works with one of the core functional concepts: _High Order Functions_. There’s also a lot more things related to System.Linq, like extension methods, IEnumerables and so on.

Backing to the original question, well, C# contains some native features related to functional paradigm, so, it’s fair to say, that you can do great things by using it.

Let’s code a simple example, we want to iterate an `IEnumerable` to show each one of its elements:

{% gist https://gist.github.com/gabrielschade/cb2221c4fcd91a38709bd68d33c974e0 %}

Our core feature was implemented successfully, but is that the best code that we can create? — I don’t think so.

Let’s remove the **hard coded values** and make it a parameter, also, we can use `IEnumerable` interface with generics, it makes a lot more sense:

{% gist https://gist.github.com/gabrielschade/9f7be725ca456d5189cc1158300d9dbe %}

The `static` modifier it’s just for make it callable by `Main` method directly. This new implementation is better than the previous one, but there’s still some hard code here, can you see it?

It’s called **hard coded behavior**, as developer I have to say that it’s almost so toxic as **hard coded values**.

> But, what it exactly means?

Well, when we think about code, specially object oriented code, variables are understood as values or attributes and methods as behaviors. So, **hard coded behavior** occurs when you call a method **hard coded**, like, `Console.WriteLine` in our code.

Okay, but is it possible to pass a function as parameter? — **Sure**, just like `System.Linq` do.

As I said before, there’s a lot of functional programming support in C# and one of these is the reference type called `delegate`. It’s used to encapsulate a function/method, it’s like a variable that can work with functions instead of values.

> Delegates are a topic apart, so if you don’t know anything about it, just check [this link](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/delegate) out.

Fortunately, there’s two generic delegates built-in C#. They are `Action` and `Func`, both of them can work with generic types.

The only difference between them are the fact that `Action` is used to encapsulate methods with no return (`void` methods), for all other type of methods the `Func` delegate is used.

Let’s change our code a little bit, just replace the hard coded call to a generic one:

{% gist https://gist.github.com/gabrielschade/2875afc712d8d6e1e663d082da2fabd5 %}

**Congratulations**! You just did your first _High Order Function_!

This method is a lot more generic than the previous one, now we need to change just the `Iterate` call if we want a different operation.

For instance, if we want to write the value squared instead of the original one:

{% gist https://gist.github.com/gabrielschade/b675c5f7b248f910ad3532191118de41 %}

As you may notice, `delegates` work with named and anonymous functions, in the above code, we changed the `Iterate` call by passing a lambda expression as parameter.

The icing on the cake is make it an extension method, so, it’ll works like a Linq method.

> If you don’t know what an extension method is, check [this link](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods).

In order to do that, we need to create a new `static` class and use the `this` keyword before the first parameter:

{% gist https://gist.github.com/gabrielschade/82d4f792ffe37a7367fb0718d35f2822 %}

Now we can use it as an `IEnumerable` method:

{% gist https://gist.github.com/gabrielschade/5b4572bd3fb75e699f856f48526c3195 %}

This is a very simple application of functional programming using C#, but the main point is: knowing functional programming will extends your toolbox, so you probably gonna be a better developer with this skills.

It is more than another language, it is a lot more related to the way of thinking and solving problems than the programming tools you will use.

> And please, you **shouldn’t** try to use your functional programming skills in each single piece of code you develop.

Use it to improve your code and your solutions, not to make them more complex.

And remember:

### With great power comes great responsibility

See you in the next post!