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: 
It's useful to remember that state machines are an abstraction, and you don't need particular tools to create one, however tools can be useful.

You can for example realise a state machine with functions:

```
void Hunt(IList<Gull> gulls)
{
    if (gulls.Empty())
       return;

    var target = gulls.First();
    TargetAcquired(target, gulls);
}

void TargetAcquired(Gull target, IList<Gull> gulls)
{
    var balloon = new WaterBalloon(weightKg: 20);

    this.Cannon.Fire(balloon);

    if (balloon.Hit)
    {
       TargetHit(target, gulls);
    }
    else
       TargetMissed(target, gulls);
}

void TargetHit(Gull target, IList<Gull> gulls)
{
    Console.WriteLine("Suck on it {0}!", target.Name);
    Hunt(gulls);
}

void TargetMissed(Gull target, IList<Gull> gulls)
{
    Console.WriteLine("I'll get ya!");
    TargetAcquired(target, gulls);
}

```

This machine would hunt for gulls and try to hit them with water balloons. If it misses it will try firing one until it hits (could do with some realistic expectations ;)), otherwise it will gloat in the console. It continues to hunt until it's out of gulls to harass.

Each function corresponds to each state; the start and end (or *accept*) states are not shown. There are probably more states in there than modelled by the functions though. For example after firing the balloon the machine is really in another state than it was before it, but I decided this distinction was impractical to make.

A common way is to use classes to represent states, and then connect them in different ways.
There are 2 popular state machine packages in NuGet.

[Appccelerate.StateMachine](https://github.com/appccelerate/statemachine) (13.6K downloads + 3.82K of legacy version (bbv.Common.StateMachine))

[StateMachineToolkit](https://github.com/OmerMor/StateMachineToolkit) (1.56K downloads)

The Appccelerate lib has [good documentation](http://www.appccelerate.com/statemachine.html), but it does not support .NET 4, so I chose StateMachineToolkit for my project.