---
title: Generate the next sequence value
summary: Generate and manage an in-memory monotonic sequence counter.
status: draft
owner: saif-docgen
last_reviewed: 2026-09-15
library: SAIF.Framework.Common
reference_type: package
operation_id: OP-028
trigger: Caller needs a monotonically increasing in-memory counter (e.g. record numbering in a generated file)*
---

# Generate the next sequence value

Generate and manage an in-memory monotonic sequence counter, triggered when caller needs a monotonically increasing in-memory counter (e.g. record numbering in a generated file)*.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-028 |
| **Trigger** | Caller needs a monotonically increasing in-memory counter (e.g. record numbering in a generated file)* |
| **Inputs** | Optional `startValue` (default 0) |
| **Outputs** | `int` / `long` |
| **Primary components** | ``SequenceGenerator<T>` / `Sequence` / `LongSequence` — `NextValue`, `SetValue`, `Reset`` |
| **Entry point** | `Common/SAIF.Common/Classes/SequenceGenerator.cs` |

## Steps

1. The protected base constructor stores `startValue` in `CurrentValue`. `Sequence` additionally calls `SetValue(startValue)` (a redundant re-assignment); `LongSequence` does not. ([BR-065](../business-rules.md#br-065-a-sequence-generator-starts-at-the-caller-supplied-start-value%2C-defaulting-to-0%2C-and-may-be-repositioned-at-any-time-via-setvalue))
2. Reading `NextValue` increments `CurrentValue` by 1 first and returns the incremented value — with a start value of 0 the first value handed out is 1. ([BR-066](../business-rules.md#br-066-nextvalue-is-pre-increment))
3. `SetValue(T)` repositions the counter to an arbitrary value. ([BR-065](../business-rules.md#br-065-a-sequence-generator-starts-at-the-caller-supplied-start-value%2C-defaulting-to-0%2C-and-may-be-repositioned-at-any-time-via-setvalue))
4. `Reset()` sets `CurrentValue` back to literal `0` in both concrete types — not to the original start value. ([BR-067](<../business-rules.md#br-067-reset()-returns-the-counter-to-literal-zero%2C-not-to-the-original-start-value>))

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start(["Caller creates or uses<br>a sequence generator"]) --> S1["Constructor stores<br>startValue in CurrentValue"]
	S1 --> C1{"Concrete type<br>is Sequence?"}
	C1 -->|Yes| S1a["Also call<br>SetValue(startValue)"]
	C1 -->|No| S1b["LongSequence skips<br>redundant call"]
	S1a --> S2["Caller reads NextValue"]
	S1b --> S2
	S2 --> S3["Increment CurrentValue<br>by 1, then return it"]
	S3 --> C2{"Caller calls<br>SetValue(T)?"}
	C2 -->|Yes| S4["Reposition CurrentValue<br>to arbitrary value"]
	C2 -->|No| C3{"Caller calls<br>Reset()?"}
	C3 -->|Yes| S5["Set CurrentValue back<br>to literal 0"]
	C3 -->|No| End([Counter value returned])
	S4 --> End
	S5 --> End
```

## Failure and edge-case behavior

- No synchronization — the increment in step 2 is not thread-safe.* No overflow guard; `int`/`long` overflow wraps silently in the default unchecked context.*

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts.

## Sources
- `Common/SAIF.Common/Classes/SequenceGenerator.cs`
- `Common/SAIF.Common/Classes/Sequence.cs`
- `Common/SAIF.Common/Classes/LongSequence.cs`
