Generate consecutive sentence of int values accessed from different threads I need to generate a sequence of int values: { 0, 1, 2, ... }, but there's a twist: I need to access them from different threads.

So I wrote this code:

class Counter
{
    private static Counter instance = new Counter();
    private static int i = 0;

    public static Counter Instance
    {
        get
        {
            return instance;
        }
    }

    public int Next()
    {
        lock (this)
        {
            return i++;
        }
    }

    public void Reset()
    {
        lock (this)
        {
            i = 0;
        }
    }

}


May this be implemented in a simpler way?
private static int i = 0;


Are you sure that i should be static? If it's static there isn't too much sense of the Counter instance = new Counter() instance and the Next and the Reset methods also could be static.

Anyway, I'd use a longer variable name, like nextValue for better readability.

Furthermore, consider the drawbacks of the Singleton pattern:


  This pattern makes unit testing far more difficult, 
  as it introduces global state into an application.


More on Wikipedia.