Search code examples
c#.netdesign-patternsfinite-automatafsm

Does C# include finite state machines?


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?


Solution

  • 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.