✅ 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:
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 please39 Replies
That's pretty much it, yep
An enum is just a set of constants with names associated with them
yep noted. One thing I didn't understand though, consider the following:
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 ?
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 typesource : official docs
Thinker
REPL Result: Success
Console Output
Compile: 435.643ms | Execution: 25.585ms | React with ❌ to remove this embed.
by the way the underlying type can be changed to byte for example but it should always be an integral type ?
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
^
oh but it works fine? then we don't have to use the ToString method ?
Well, the trick is that
Console.WriteLine
actually just calls ToString
on whatever you give itahhhh
true
remember that
So no we do need
ToString
to turn the enum into its nameyeppp I see, make sense now, thanks !!
Oh, also, there's another variant of enums you might wanna know about
oh ? which one
Sometimes you might see something like this being done
(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
.ahhh I see
interesting, like these are "fallback choices" for the same value?
say RequireWifi is 1, PrintErrors also has a value of 1?
Not really, these are all separate values
one moment
Thinker
REPL Result: Success
Console Output
Compile: 468.207ms | Execution: 27.024ms | React with ❌ to remove this embed.
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.
you can also let the compiler do the math part and assign the values to
1 << 0
, 1 << 1
, 1 << 2
^ i prefer that, much much better to read
(yeah, just didn't wanna be confusing with syntax)
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[Flags]
is an attribute, which just attaches additional metadata to the type/methodeverything within
[]
are just meta data?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
.yeah I see
When you see something like
[Fact]
or [Test]
on a method, it specifies that the method is a unit testyep 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
no problem :meow_owo:
by the way, what does this syntax do?
left shift, takes a 1 and shifts it over 0, 1, or 2 bits
ah I see, I need to refresh a bit about bitwise operators like left shift and right shift, I forgot how it works internally 😭
so instead of remembering 1,2,4,8,16,32 sequence, you can count 1,2,3,4,5,6
by the way bitwise operators are important? I mean if they are here they should have some importance but in general
in enum flags case, definitely yes
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 commonahh I see
yep noted, thanks !