B
B
BadCats2016-04-05 14:20:13
C++ / C#
BadCats, 2016-04-05 14:20:13

Enumerations. GetValue method in a loop?

Hello everyone, I have 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 enum:
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 the elements of the EnumType enumeration, which are presented in the form of a table like any other array, and the GetValue method, by the i-th number, pulls out data about the element of the array of constants written to the array variable.
So, this array is created as one-dimensional or as two-dimensional - because the enumeration constant consists of a name and a value?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BernadetteWolowitz, 2016-04-05
@BadCats

The array is created as one-dimensional. It doesn't matter if it contains: int or EnumType or BigObjectWithManyProperties.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question