Hello everyone, there is the following example:

class Program { static void Main() { // Enum.GetValues() - возвращает экземпляр System.Array, при этом каждому элементу массива // будет соответствовать член указанного перечисления. // Помещаем в массив элементы перечисления. Array array = Enum.GetValues(typeof(EnumType)); // Получаем информацию о количестве элементов в массиве. Console.WriteLine("Это перечисление содержит {0} членов \n", array.Length); // Вывод на экран всех элементов перечисления for (int i = 0; i < array.Length; i++) { Console.WriteLine("Имя константы: {0}, значение {0:D}", array.GetValue(i)); } // Delay. Console.ReadKey(); } } 

Here is the listing itself:

  enum EnumType { Zero, // = 0 One = 1, one = One, Two = 2, Three, // = 3 Four, // = 4 Five = 5, //Six, Seven, Eight = 8, Nine, Ten = 10, Infinite = 255 } 

I have a question about the cycle

 // Вывод на экран всех элементов перечисления for (int i = 0; i < array.Length; i++) { Console.WriteLine("Имя константы: {0}, значение {0:D}", array.GetValue(i)); } 

The arrray variable of the Array type contains all elements of the EnumType enumeration, which are presented in the form of a table like any other array, and the GetValue method, using the i-th number, pulls data about the element of the constant array written to the variable array.

So, this array is created as one-dimensional or two-dimensional - because the enumeration constant consists of a name and a value?

  • you take only the values, GetValues - where can the second dimension come from? - Grundy

1 answer 1

There is only one type of value in an enumeration - the actual members of the enumeration (what you called the name). Therefore, when you get a similar array, it’s naturally a one-dimensional array that contains only enumeration values.

Another thing is that each member of the enumeration can be explicitly reduced to a number. But this does not affect the resulting array and, moreover, is in no way connected with the index i in the given cycle.