C
C#2mo ago
mack

✅ enumeration help

can you call on the “index” of an entry in an enumeration? like say theres 3 options, red, blue, green, and i have an array with 1, 2, 3. if a random int is used to select between the array, can i also use that same random int to call upon the corresponding enumeration? i.e. red being called at the same time/in a similar way as 1, blue and 2, etc sorry if the question doesnt make sense, i can rephrase
11 Replies
cap5lut
cap5lut2mo ago
if mean with enumeration something like
enum Color
{
Red,
Blue,
Green
}
enum Color
{
Red,
Blue,
Green
}
that is possible. under the hood these are just numbers as well. the above enum could also be written as
enum Color : int // the underlying number type
{
Red = 0,
Blue = 1,
Green = 2
}
enum Color : int // the underlying number type
{
Red = 0,
Blue = 1,
Green = 2
}
the values are implicitly added. and then u can cast between the enum and int, Color color = (Color)1; would be the same as Color color = Color.Blue; and vice versa int num = (int)Color.Green; would be the same as int num = 2; so actually you would not even need the array here at all
mack
mackOP2mo ago
oh lovely, thats so cool! thanks so much so for instance what if i just wanted to debug log the randomized color to string
cap5lut
cap5lut2mo ago
u would have it as Color value already then, its ToString() method will do the right thing, and stuff like Console.WriteLine(); automatically calls that method
MODiX
MODiX2mo ago
cap5lut
REPL Result: Success
enum Color { Red, Blue, Gree }
Color color = (Color)Random.Shared.Next(0, 3);
Console.WriteLine(color);
enum Color { Red, Blue, Gree }
Color color = (Color)Random.Shared.Next(0, 3);
Console.WriteLine(color);
Console Output
Blue
Blue
Compile: 386.570ms | Execution: 21.133ms | React with ❌ to remove this embed.
cap5lut
cap5lut2mo ago
hey @mack, there is one thing about the numbers and enum stuff i forgot to mention. its entirely possible to get "undefined" enums. eg for enum Color { Red, Blue, Gree } where the values are respectively 0, 1 and 2 u can still do stuff like Color color = (Color)255;, tho its not a defined enum. to check if it is a valid/defined enum, there is the Enum.IsDefined() method.
Enum.IsDefined Method (System)
Returns a Boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.
MODiX
MODiX2mo ago
cap5lut
REPL Result: Success
enum Color { Red, Blue, Green }
Color color1 = (Color)1;
Color color2 = (Color)255;
Console.WriteLine(Enum.IsDefined(color1));
Console.WriteLine(Enum.IsDefined(color2));
enum Color { Red, Blue, Green }
Color color1 = (Color)1;
Color color2 = (Color)255;
Console.WriteLine(Enum.IsDefined(color1));
Console.WriteLine(Enum.IsDefined(color2));
Console Output
True
False
True
False
Compile: 480.216ms | Execution: 25.329ms | React with ❌ to remove this embed.
mack
mackOP2mo ago
thanks for this extra information :) youre very helpful
cap5lut
cap5lut2mo ago
if your questions are answered, please $close the thread, btw 🙂
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
mack
mackOP2mo ago
$close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server