How to get next or previous enum value in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
C# enums are named constants, but they do not include built-in navigation methods such as "next" or "previous." If you want that behavior, you usually read the enum values into an ordered array, find the current position, and then decide whether you want wraparound or strict boundary behavior.
Start With the Enum Values
The standard building block is Enum.GetValues, which returns the declared values of the enum.
Once you have that array, moving to the next or previous enum value becomes an indexing problem.
Next Value With Wraparound
A common choice is circular navigation, where moving forward from the last enum value returns the first one.
This works well for state cycles, rotating UI modes, or menu selection logic.
Previous Value With Wraparound
The same idea works in reverse.
The extra + values.Length prevents a negative index before the modulo operation.
Boundary-Safe Version Without Wraparound
Sometimes wraparound is the wrong behavior. In that case, return the current value or throw an exception when the caller is already at the boundary.
This is often better for workflows where the enum represents a true linear progression.
Be Careful With Custom Numeric Values
Enums can declare custom underlying values.
Navigation should usually follow declaration order or the array returned by Enum.GetValues, not assume that current + 1 is valid. Numeric arithmetic on enum values is rarely the right answer unless the enum was explicitly designed to be sequential.
Flags Enums Are a Different Problem
If the enum uses [Flags], next and previous usually stop making sense because combined values such as Read | Write are not part of a simple linear list.
For flags enums, treat the problem as bit manipulation rather than next/previous navigation.
A Reusable Extension Method
If this pattern appears often in your codebase, an extension method keeps call sites cleaner.
Then you can call:
Common Pitfalls
The biggest pitfall is assuming enum values are always consecutive integers. C# allows gaps and custom numeric assignments, so value + 1 is not a reliable navigation strategy.
Another issue is forgetting to decide whether wraparound is correct. Circular navigation is convenient, but it can hide bugs if the business rule is actually linear.
Developers also apply the same logic to [Flags] enums even though those represent combinable bit fields rather than an ordered sequence.
Finally, avoid recomputing the value array in extremely hot code paths if performance matters. For normal application logic it is fine, but reusable caches can help in tight loops.
Summary
- Use
Enum.GetValues<T>()to obtain the enum's ordered values. - Find the current index, then compute the next or previous position explicitly.
- Decide whether wraparound or boundary-safe behavior is correct for the use case.
- Do not rely on numeric
+ 1unless the enum was intentionally designed for that. - Treat
[Flags]enums as a separate bitmask problem, not as a next-value sequence.
Related reading
- How to get only filenames within a directory using c?
- How to get the current user in ASP.NET MVC
- How to get the index of an element in an IEnumerable?
- How to get the latest offset from the Kafka topic in Confluent kafka C# library?
- How to get the type of T from a member of a generic class or method
- How to get topics list from Kafka using C#
- how to get value from appsettings.json
- How to get Xml as string from XDocument?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.