Question: **Update:**

Again thanks for the examples, they have been very helpful and with the following, I don't mean
to take anything away from them.

Aren't the currently given examples, as far as I understand them & state-machines, only half of what we usually understand by a state-machine?  

In the sense that the examples do change state but that's only represented by changing the value of a variable (and allowing different value- changes in different states), while usually, a state machine should also change its behavior, and behavior not (only) in the sense of allowing different value changes for a variable depending on the state, but in the sense of allowing different methods to be executed for different states.

Or do I have a misconception of state machines and their common use?

---

**Original question:**

I found this discussion about [state machines & iterator blocks in c#](https://stackoverflow.com/questions/1406986/does-c-include-finite-state-machines) and tools to create state machines and whatnot for C#, so I found a lot of abstract stuff but as a noob, all of this is a little confusing.

So it would be great if someone could provide a C# source code-example that realizes a simple state machine with perhaps 3,4 states, just to get the gist of it.

Answer: 
I'm posting another answer here as this is state machines from a different perspective; very visual.

My original answer is classic imperative code. I think its quite visual as code goes because of the array which makes visualizing the state machine simple. The downside is you have to write all this. [Remos](https://stackoverflow.com/users/448580/remo-gloor)'s answer alleviates the effort of writing the boiler-plate code but is far less visual. There is the third alternative; really drawing the state machine.

If you are using .NET and can target version 4 of the run time then you have the option of using **workflow's state machine activities**. These in essence let you draw the state machine (much as in [Juliet](https://stackoverflow.com/users/40516/juliet)'s diagram) and have the WF run-time execute it for you.

See the MSDN article [Building State Machines with Windows Workflow Foundation](https://learn.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/state-machine-workflows) for more details, and [this CodePlex site](http://wf.codeplex.com/releases/view/43586) for the latest version.

That's the option I would always prefer when targeting .NET because its easy to see, change and explain to non programmers; pictures are worth a thousand words as they say!
You can use my solution, this is the most convenient way. It’s also free.

Create state machine in **three** steps :

**1.** Create scheme in [node editor](https://github.com/SimpleStateMachine/SimpleStateMachineNodeEditor) and load it in your project using [library](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary)

```
StateMachine stateMachine = new StateMachine("scheme.xml");

```

**2.** Describe your app logic on events⚡

```
stateMachine.GetState("State1").OnExit(Action1);
stateMachine.GetState("State2").OnEntry(Action2);
stateMachine.GetTransition("Transition1").OnInvoke(Action3);
stateMachine.OnChangeState(Action4);

```

**3.** Run the state machine

```
stateMachine.Start();

```

Links:

Node editor: <https://github.com/SimpleStateMachine/SimpleStateMachineNodeEditor>

Library: <https://github.com/SimpleStateMachine/SimpleStateMachineLibrary>