Counter
Energy.Base.Counter is a thread-safe counter that supports minimum and maximum bounds, overflow detection, and loop mode.
Features
Thread-safe increment and decrement.
Configurable minimum and maximum values.
Overflow flag.
Loop mode that wraps the value back to the minimum or maximum on overflow.
Direct value assignment and absolute value access.
Example
// Counter from 0 to 9, looping back to 0
Energy.Base.Counter counter = new Energy.Base.Counter(0, 9)
{
Loop = true,
};
for (int i = 0; i < 15; i++)
{
Console.WriteLine(counter.Increment());
}
// Output: 1, 2, ... 9, 0, 1, 2, 3, 4, 5
Properties
| Property | Description |
|---|---|
Value |
Current counter value. |
Minimum |
Minimum value. |
Maximum |
Maximum value. |
Overflow |
True if the last operation caused an overflow. |
Loop |
Wrap around on overflow. |
Absolute |
Current value as an unsigned long. |
Methods
| Method | Description |
|---|---|
Increment() |
Increment by one. |
Increment(long by) |
Increment by the specified value. |
Decrement() |
Decrement by one. |
Decrement(long by) |
Decrement by the specified value. |
Reset() |
Reset to the minimum value. |
Reset(long value) |
Reset to the specified value. |
ToString() |
Return the current value as a string. |