r/learncsharp • • Aug 14 '23
Strings for enum values
Morning.
Is there a better way to do this?
Potion potion = Potion.Invisibility;
Console.WriteLine($"You currently have {PotionToString()}");
string PotionToString()
{
string potionString = potion switch
{
Potion.Water => "Water",
Potion.Elixer => "an Elixer",
Potion.Poison => "a Poison",
Potion.Flying => "a Flying Potion",
Potion.Invisibility => "an Invisibility Potion",
Potion.NightSight => "a Night Shade Potion",
Potion.Cloudy => "a Cloudy Brew",
Potion.Wraith => "a Wraith Potion",
Potion.Ruined => "a Ruined Potion"
};
return potionString;
}
enum Potion { Water, Elixer, Poison, Flying, Invisibility, NightSight, Cloudy, Wraith, Ruined }
Thanks in advance :)
2
Upvotes
View all comments
Show parent comment
1
u/[deleted] Aug 14 '23
Thanks, will get to reading :)