C# ; AutoResetEvent ; simple code with comments
\nusing System.Threading;\n\npublic class ZeroEvenOdd\n{\n    private int n;\n\n    // AutoResetEvent works like a door in the elevator; one passed and door clo
```\nusing System.Threading;\n\npublic class ZeroEvenOdd\n{\n    private int n;\n\n    // AutoResetEvent works like a door in the elevator; one passed and door closed\n    // ManualResetEvent works like a door in the show: door opens and all pass\n\n    // Put _printZero into signaled state, allowing to execute it with `WaitOne`\n    private AutoResetEvent _printZero = new(true);\n    private AutoResetEvent _printEven = new(false);\n    private AutoResetEvent _printOdd  = new(false);\n\n    public ZeroEvenOdd(int n)\n    {\n        this.n = n;\n    }\n\n    // printNumber(x) outputs "x", where x is an integer.\n    public void Zero(Action<int> printNumber)\n    {\n        bool allowPrintOdd = true;\n\n        for (int i = 0; i < n; i++)\n        {\n            _printZero.WaitOne();\n\n            printNumber(0);\n\n            // Allow to print odd\n            if (allowPrintOdd)\n            {\n                _printOdd.Set();\n            }\n            // Allow to print even\n            else \n            { \n                _printEven.Set();\n            }\n\n            allowPrintOdd = !allowPrintOdd;\n        }\n    }\n\n    public void Even(Action<int> printNumber)\n    {\n        for (int i = 2; i <= n; i+=2)\n        {\n            _printEven.WaitOne();\n\n            printNumber(i);\n\n            // Allow to print zero number\n            _printZero.Set();\n        }\n\n    }\n\n    public void Odd(Action<int> printNumber)\n    {\n        for (int i = 1; i <= n; i+=2)\n        {\n            _printOdd.WaitOne();\n\n            printNumber(i);\n\n            // Allow to print zero number\n            _printZero.Set();\n        }\n    }\n}\n\n```
[]