concept mutable state in category c#

appears as: mutable state, mutable state
Real-World Functional Programming with Examples in F# and C#

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

Haskell appeared in 1990 and has been popular in the academic community. In this section you’ve seen that in functional languages, you work with immutable data structures and use immutable values rather than mutable variables. This isn’t strictly true in F# because you can still declare mutable values. This nonstrict approach is particularly useful for .NET interoperability. Most of the .NET libraries rely on mutable state, as it was designed for imperative object-oriented languages such as C# and VB.NET.

As we’ve explained, the command pattern often works with mutable state, encapsulated in something like the Receiver class of our example. Listing 8.6 shows an example of this, creating a more flexible income test for our financial application. The goal is to allow the test to be configured later without updating the collection of tests.

Listing 8.6. Income test using the command pattern (C#)

We start by creating a class that carries the mutable state and corresponds to the Receiver component from the command design pattern ❶. The state is a recommended minimal income and the class has a mutable property for modifying it. The next method implements the test itself and compares whether the income of the given client is larger than the current minimal value stored in the test.