Does C# include finite state machines?
Solution 1
Yes, C# has iterator blocks which are compiler-generated state machines.
If you wish to implement you own state machine you can create custom implementations of the IEnumerable<T>
and IEnumerator<T>
interfaces.
Both of these approaches highlight the .NET framework's implementation of the iterator pattern.
Solution 2
.NET 4 Update 1 now supports it in the following class: System.Activities.Statements.StateMachine
Here is a tutorial on how to use it. Here's a hands on lab.
Solution 3
Check out Stateless -> http://code.google.com/p/stateless/. Its a lightweight alternative to the heavier WWF.
Here's a couple of articles by the author of the tool:
State Machines in Domain Models
Parameterised Triggers and Re-entrant States in Stateless
Solution 4
I maintain an open-source project which implements (among other things) a generic finite state machine for .NET. It is built on top of QuickGraph, so you get many graph-analysis algorithms for free.
See this page for more information about the project, and specifically "Jolt.Automata : Finite State Machines" for more information about the feature.
Solution 5
The things that come near to FSMs are workflows in .NET 3.5, however, also workflows are not exactly FSMs.
The power of using FSMs is that you can create them explicitly in your code, having less chance of creating bugs. Besides, of course some systems are FSMs by nature, so it is more natural to code them like so.
Maciek
Updated on August 26, 2020Comments
-
Maciek almost 4 years
I've recently read about the
boost::statechart
library (finite state machines) and I loved the concept.Does C# have a similar mechanism ? Or can it be implemented using a specific design pattern?