Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decreasing value in C# or VB.NET enum

Tags:

c#

enums

vb.net

More for interest than actual need... is it possible to have an automatically decreasing enum in C# or VB.NET?

public enum testEnum
{
    this = -1,
    that,
    other,
}

So that that = -2 and other = -3.

I'm pretty sure the only way to do it is to specifically assign "that" and "other", but I wondered if there was an automatic way of doing it.

Edit

To be clear, I'm simply talking about the automatic assignment of the value, not the actual value of the enum decreasing.

like image 770
freefaller Avatar asked Mar 26 '26 08:03

freefaller


1 Answers

No, it is not possible.

You must declare the values if you wish to do so, or reverse the declaration:

public enum testEnum
{
    other = -3,
    that,
    @this
}
like image 183
Oded Avatar answered Mar 27 '26 21:03

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!