This is part of a school assignment and I came up with the following solution to leave out the max value in a Enumeration:static void Opdracht3(){    List<int> values = new List<int>();    foreach (int value in Enum.GetValues(typeof(Opleidingen)))    {        values.Add(value);    }    foreach (int value in values)    {        if (value != values.Max())        {            Console.WriteLine(Naam: {0}, Waarde: {1}, Enum.GetName(typeof(Opleidingen), value), value);        }    }}How could I improve this piece of (working) code?
Leaving out the max value in an Enumeration
c#;optimization;enum
Actually, most of the leg work is done for us in the framework.  Enum.GetValues returns the list sorted ascending already, via the underlying implementation of Type.GetEnumData.With that in mind, you can use LINQ for the rest, retrieving all but the last value:static void Opdracht3(){    var values = Enum.GetValues(typeof(Opleidingen)).Cast<Opleidingen>().ToList();    var valuesExceptMax = values.Take(values.Count - 1);    foreach (var value in valuesExceptMax)    {        Console.WriteLine(Naam: {0}, Waarde: {1}, value, (int) value);    }}Since we retrieve the enum values themselves rather than casting to int up front, we can eliminate the Enum.GetName calls.  Passing an enum value into a string format will use the name for us (see: Enum.ToString()).In case you are hesitant to assume sorting up-front, it's pretty trivial to toss a call to .OrderBy(x => x) just before the .ToList() in our first LINQ statement.  However, if you're going to order the list, it would be simpler to sort descending and skip the first (max) value, so we can drop the .ToList():static void Opdracht3(){    var values = Enum.GetValues(typeof(Opleidingen)).Cast<Opleidingen>().OrderByDescending(x => x);    var valuesExceptMax = values.Skip(1);    foreach (var value in valuesExceptMax)    {        Console.WriteLine(Naam: {0}, Waarde: {1}, value, (int) value);    }}