+ 2

Get each entry of Enum type using foreach (C#)

Consider the following code in C#: enum Alignment : sbyte { Left = -1, Center = 0, Right = 1 } static void Main() { Console.WriteLine("Enum Alignment"); Console.WriteLine("____________________"); foreach (var value in Enum.GetValues(typeof(Alignment))) { Console.WriteLine(

quot;{value} = {(sbyte)value}"); } Console.ReadKey(); } Does anyone can please explain why the output for this enum type is the following: Enum Alignment ____________________ Center = 0 Right = 1 Left = -1 and not as defined as, Left = -1 Center = 0 Right = 1 Is there any method that ensure that enum base values are print in ascending order?

1st Jan 2017, 3:34 PM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
1 Resposta
+ 1
This is a very interesting question which has sent me down a rabbit hole. You can follow me from: https://blogs.msdn.microsoft.com/haibo_luo/2006/07/10/member-order-returned-by-getfields-getmethods/
3rd Jan 2017, 10:32 AM
Kitta Dale
Kitta Dale - avatar