0

Say you have a byte value equal to its max value, so byte aByte = 255;. When you increment a value past its max value, it usually wraps around to its minimum value, so if I were to write Console.WriteLine(++aByte), this would print 0 to the console because it can't exceed its max value.

I would like to have this same effect when it comes to enumerations I create. So let's say this is the code:

using System;

namespace CSharp_Test_003
{
    class Program
    {
        static void Main()
        {
            Console.Title = "Enumerations";

            Season currentSeason = Season.Spring;
            Console.Write($"{currentSeason++} ");
            Console.Write($"{currentSeason++} ");
            Console.Write($"{currentSeason++} ");
            Console.Write($"{currentSeason++} ");
            Console.Write($"{currentSeason++} ");
        }

        private enum Season { Spring = 0, Summer, Autumn, Winter }
    }

Currently this prints out:

Spring Summer Autumn Winter 4

But I would really like it to say "Spring" where there is a "4". If there's a practical way to make this possible I would really love to know.

3
  • There are a lot of "dirty" solutions, but I think the cleanest way would be to write a small class that's doing what you want. Commented May 13, 2022 at 16:55
  • The problem is that the underlying type is an int and not restricted to the values you specify Commented May 13, 2022 at 16:59
  • @JoelCoehoorn byte is unsigned, byte.MinValue is 0. sbyte is signed its MinValue is -128 Commented May 13, 2022 at 17:04

1 Answer 1

2

You can write your own custom enum that works in a similar way

static void Main()
{
    Console.Title = "Enumerations";

    Season currentSeason = Season.Spring;
    Console.Write($"{currentSeason++} ");
    Console.Write($"{currentSeason++} ");
    Console.Write($"{currentSeason++} ");
    Console.Write($"{currentSeason++} ");
    Console.Write($"{currentSeason++} ");
}

//private enum Season { Spring = 0, Summer, Autumn, Winter }

public record Season
{
    public int Id { get; }
    public string Name { get; }
    private static readonly List<Season> _seasons = new();

    public static IReadOnlyList<Season> All => _seasons.OrderBy(x => x.Id).ToList();

    private Season(int id, string name)
    {
        Id = id;
        Name = name;
        _seasons.Add(this);
    }

    public static Season Spring { get; } = new Season(0, nameof(Spring));
    public static Season Summer { get; } = new Season(1, nameof(Summer));
    public static Season Winter { get; } = new Season(3, nameof(Winter));
    public static Season Autumn { get; } = new Season(2, nameof(Autumn));

    public override string ToString() => this.Name;

    public static implicit operator string(Season season) => season.ToString();

    public static Season operator ++(Season season)
    {
        if (season.Id == All.Count - 1)
            return All[0];

        return All[season.Id + 1];
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

You'd want the constructor to be private with a design like this.
@HansKesting the constructor adds to the list. That said, populating the list that way results in the order of the list depending on the order that the constructors of the fields run in, which seems to be a rather fragile way of defining the code, I'd prefer to populate the list via a static constructor instead to avoid that.
Spring, Summer, Autumn, Winter variables need to be declared as readonly
@Servy populating the list from a static constructor makes it more error prone, if someone adds a new value and forgets to also add it to the constructor things might break so I tend to do it this way. If order is important I would rather sort the List values via Linq.
@XavierJ changed them to properties as they are public, should not have been fields.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.