C
C#4w ago
Faker

✅ Enums in C#

Hello guys, I'm learning about what an "enum" is, I often saw this word several times without really understanding what it is or how it works. So from what I've read, an enum is a distinct value type which represents a set of constants. We declare one as follows:
enum DaysOfWeeks
{
...
}
enum DaysOfWeeks
{
...
}
Enums are mostly used where we need to represent "options". It is more user-friendly because it's easier to read if (color == Colors.Red) than if (color == 1). This is what I understood so far, is there anything else to add please
39 Replies
Thinker
Thinker4w ago
That's pretty much it, yep An enum is just a set of constants with names associated with them
Faker
FakerOP4w ago
yep noted. One thing I didn't understand though, consider the following:
DaysOfWeek day = DaysOfWeek.Friday;
string dayString = day.ToString(); // "Friday"
DaysOfWeek day = DaysOfWeek.Friday;
string dayString = day.ToString(); // "Friday"
Assume DaysOfWeek is an enum, why do we need to use the .ToString() method here? When we say day = DaysOfWeek.Friday, "Friday" isn't assigned to day? what if we try to console.write day ?
Cattywampus
Cattywampus4w ago
while this is not entirely wrong :
a distinct value type which represents a set of constants.
I prefer this :
a set of named constants of the underlying integral numeric type
source : official docs
MODiX
MODiX4w ago
Thinker
REPL Result: Success
enum DaysOfWeek
{
Monday, Tuesday, Wensday, Thursday, Friday, Saturday, Sunday
}

DaysOfWeek day = DaysOfWeek.Friday;
Console.WriteLine(day);
enum DaysOfWeek
{
Monday, Tuesday, Wensday, Thursday, Friday, Saturday, Sunday
}

DaysOfWeek day = DaysOfWeek.Friday;
Console.WriteLine(day);
Console Output
Friday
Friday
Compile: 435.643ms | Execution: 25.585ms | React with ❌ to remove this embed.
Faker
FakerOP4w ago
by the way the underlying type can be changed to byte for example but it should always be an integral type ?
Thinker
Thinker4w ago
yeah, because floats are kinda weird An enum has to be any integer type, i.e. bytes, shorts, ints, longs, and their signed/unsigned variants
Cattywampus
Cattywampus4w ago
^
Faker
FakerOP4w ago
oh but it works fine? then we don't have to use the ToString method ?
Thinker
Thinker4w ago
Well, the trick is that Console.WriteLine actually just calls ToString on whatever you give it
Faker
FakerOP4w ago
ahhhh true remember that
Thinker
Thinker4w ago
So no we do need ToString to turn the enum into its name
Faker
FakerOP4w ago
yeppp I see, make sense now, thanks !!
Thinker
Thinker4w ago
Oh, also, there's another variant of enums you might wanna know about
Faker
FakerOP4w ago
oh ? which one
Thinker
Thinker4w ago
Sometimes you might see something like this being done
Options options = Options.RequireWifi | Options.PrintErrors | Options.NoHeader;
Options options = Options.RequireWifi | Options.PrintErrors | Options.NoHeader;
(I just made these names up, they don't mean anything real here) This is called a flags enum It's pretty much just a normal enum, except you can specify multiple variants in the same value. In this case options consists of RequireWifi, PrintErrors, and NoHeader.
Faker
FakerOP4w ago
ahhh I see interesting, like these are "fallback choices" for the same value? say RequireWifi is 1, PrintErrors also has a value of 1?
Thinker
Thinker4w ago
Not really, these are all separate values one moment
MODiX
MODiX4w ago
Thinker
REPL Result: Success
[Flags]
enum Options
{ // decimal binary
RequireWifi = 1, // 001
PrintErrors = 2, // 010
NoHeader = 4, // 100
}

Options options = Options.RequireWifi | Options.NoHeader;

Console.WriteLine(options);
[Flags]
enum Options
{ // decimal binary
RequireWifi = 1, // 001
PrintErrors = 2, // 010
NoHeader = 4, // 100
}

Options options = Options.RequireWifi | Options.NoHeader;

Console.WriteLine(options);
Console Output
RequireWifi, NoHeader
RequireWifi, NoHeader
Compile: 468.207ms | Execution: 27.024ms | React with ❌ to remove this embed.
Thinker
Thinker4w ago
Similarly to how normal enums are just constant numbers with names, flag enums are also just numbers with names, but the numbers are usually powers of 2, which means we can use the bitwise-or operator on them to represent a combination of multiple values.
Sehra
Sehra4w ago
you can also let the compiler do the math part and assign the values to 1 << 0, 1 << 1, 1 << 2
Cattywampus
Cattywampus4w ago
^ i prefer that, much much better to read
Thinker
Thinker4w ago
(yeah, just didn't wanna be confusing with syntax)
Faker
FakerOP4w ago
yep I see, by the way what is the [Flags] syntax? to show to the compiler that we are going to use a flag enum? I notice the [...] syntax another place also, don't remember when or for which purpose but mostly likely it was for unit testing
Thinker
Thinker4w ago
[Flags] is an attribute, which just attaches additional metadata to the type/method
Faker
FakerOP4w ago
everything within [] are just meta data?
Thinker
Thinker4w ago
yeah In this case, [Flags] specifies that when we turn it into a string, it should be written out as RequireWifi, NoHeader, instead of 5.
Faker
FakerOP4w ago
yeah I see
Thinker
Thinker4w ago
When you see something like [Fact] or [Test] on a method, it specifies that the method is a unit test
Faker
FakerOP4w ago
yep noted, thanks, really appreciate, I now have an overview of enums, flag enums and also the attribute thing meta data, will just refresh a bit from what I've learnt, if I have questions will come back but should be ok for now
Thinker
Thinker4w ago
no problem :meow_owo:
Faker
FakerOP4w ago
by the way, what does this syntax do?
Sehra
Sehra4w ago
left shift, takes a 1 and shifts it over 0, 1, or 2 bits
Faker
FakerOP4w ago
ah I see, I need to refresh a bit about bitwise operators like left shift and right shift, I forgot how it works internally 😭
Thinker
Thinker4w ago
decimal: 1
binary: 0001

1 << 1:
decimal: 2
binary: 0010

1 << 2:
decimal: 4
binary: 0100

1 << 3:
decimal: 8
binary: 1000
decimal: 1
binary: 0001

1 << 1:
decimal: 2
binary: 0010

1 << 2:
decimal: 4
binary: 0100

1 << 3:
decimal: 8
binary: 1000
Sehra
Sehra4w ago
so instead of remembering 1,2,4,8,16,32 sequence, you can count 1,2,3,4,5,6
Faker
FakerOP4w ago
by the way bitwise operators are important? I mean if they are here they should have some importance but in general
Cattywampus
Cattywampus4w ago
in enum flags case, definitely yes
Thinker
Thinker4w ago
you probably won't need most bitwise operators much in your day-to-day code, but sometimes they're useful Also yeah | for flag enums is very common
Faker
FakerOP4w ago
ahh I see yep noted, thanks !

Did you find this page helpful?