How to Convert Enum to List

Computer programming languages have many different ways of storing data. Most data is stored according to a type, which helps define its range of values. For example, a digit type can hold the values zero through nine. An "enum" is a special type that is determined by the user. An "enum" can store the days of the week, for example. You can use a language like C# to transform an "enum" into a list with a little bit of code.

Things You'll Need

  • Computer with Visual Studio 2010 installed
Show More

Instructions

    • 1

      Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File/New/Project" and click "Visual C#/Console Application." A new Console Application project is created, and source code file appears in the main editor window. The source code has a blank "main" method.

    • 2

      Declare an "enum" by writing the following statement in the line directly above the "main" method declaration:

      enum days {Sun, Mon, Tue, Wed, Thu, Fri, Sat};

    • 3

      Write the rest of the statements between the curly brackets of the "main" method.

    • 4

      Transform the "enum" into a list by using the following statement:

      var array = Enum.GetValues(typeof(days)).Cast<days>().ToArray();

    • 5

      Print out the value of the very first element of the list (element 0) to the output console. You can do this with a single statement like this:

      Console.Out.WriteLine(array.ElementAt(0));

    • 6

      Execute the program by pressing the "F5" key. The program output looks like this:

      sun

Related Searches:

References

Comments

Related Ads

Featured