views:

1862

answers:

6

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?

+2  A: 

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.

Andrew Hare
Wow that was quick, thanks for the response
Maciek
Iterators are by far no state machines. Some basic concepts of FSMs are states, transitions, transition guards, actions and hierarchical states. These are NOT explicit in the iterator blocks, so i do not agree that this is a implementation of FSM.
Henri
Iterator blocks are implemented as state machines, but that does not mean they are suited for building an arbitrary state machine. Erik Lippert makes this point here: http://stackoverflow.com/questions/1194853/implementing-a-state-machine-using-the-yield-keyword/1195205#1195205
Gabe Moothart
@Gabe - That is correct which is why I pointed out the `IEnumerable<T>` and `IEnumerator<T>` interfaces.
Andrew Hare
Also I didn't claim that iterator blocks should be used to implement a state machine, simply that they are an implementation of a state machine that the compiler generates for you.
Andrew Hare
The fact that Iterators use a FSM is irrelevant to the question at hand. The OP wants to create his own.
Adam Lassek
One of the coolest uses of iterators as FSM I have seen has been uses as [asynchronous iterators](http://www.google.com/search?q=asynchronous+iterators) for asynchronous programming - most popular of which is Jeffrey Richter's [AsyncEnumerator](http://msdn.microsoft.com/en-us/magazine/cc546608.aspx). Maybe this is language construct abuse, but it represents the easiest way I have seen by far to write asynchronous code for linear workflows in C#.
fostandy
+4  A: 

Workflow Foundation (.NET 3.0) has a state machine workflow. 4.0 doesn't have exactly the same thing currently, but you can definitely create a state machine workflow using 4.0.

Will
+2  A: 

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.

Henri
FSM stands for Flying Spaghetti Monster. I think you answered the wrong question.
Gabe Moothart
I'm pretty sure that he wasn't referring to the Flying Spagetti Monster and instead was referring to Finite State Machine.
Nathan Palmer
+4  A: 

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.

Steve Guidi
+1  A: 

Windows Workflow Foundation (WF) that is part of the base class library in 3.0 and 3.5 includes a state-machine workflow design to manage state machines for your applications.

They have completely rewritten workflow for the upcoming 4.0 release, and the new WF 4.0 classes do not natively support state-machines, but all of the 3.0/3.5 classes are still fully supported under 4.0.

Bytemaster