20

Given the C# enum:

public enum stuffEnum: int
{
    New = 0,
    Old = 1,
    Fresh = 2
}

How do I loop through it in a way that I can copy both the key and its value in a single loop? Something like:

foreach(var item in stuffEnum)
{
    NewObject thing = new NewObject{
       Name = item.Key,
       Number = item.Value
    }
}

So you would end up with 3 objects, with their Name properties set to "New", "Old", and "Fresh", and the Number properties set to 0, 1 and 2.

How do I do this?

yesman
  • 5,531
  • 9
  • 39
  • 87
  • http://stackoverflow.com/questions/972307/can-you-loop-through-all-enum-values – Ghyath Serhal Nov 12 '14 at 12:12
  • Those show an enum with single elements (or just keys). My enum has key/value pairs, and is therefore different. – yesman Nov 12 '14 at 12:12
  • 3
    @BasR. No it is not different, you simply explicitly define the values. casting your enum value to `int` will give you the value. – Rotem Nov 12 '14 at 12:14
  • @BasR. Which makes your goal to have an object with an enum value and the same value cast to int rather redundant. – Rotem Nov 12 '14 at 12:16

3 Answers3

22

The Enum class has the Methods you're looking for.

foreach(int i in Enum.GetValues(typeof(stuff)))
{
    String name = Enum.GetName(typeof(stuff), i);
    NewObject thing = new NewObject
    {
        Name = name,
        Number = i
    };
}
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
14

You could use LINQ (as almost always):

List<NewObject> stuff = Enum.GetValues(typeof(stuffEnum)).Cast<stuffEnum>()
    .Select(s => new NewObject { Name = s.ToString(), Number = (int) s })
    .ToList();
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • 1
    Compared to the other answers your answer is supposed to be (a) the first one, (b) easy to read, (c) fast to execute, (d) exercise in using the largest number of different keywords one has to learn? My current bet is (d) but perhaps I'm wrong and there is an unspoken (e) I did not get? – xmojmr Nov 12 '14 at 13:08
  • 2
    @xmojmr: I'm going to guess some combination of (b) easy to read and (e) uses LINQ. Plenty of people find LINQ easy to read. Further, it does have a conciseness advantage. Anyone familiar with LINQ should be familiar with all of the extension methods Tim uses, which is all he really uses that other answers do not use (well, that and lambdas). – Brian Nov 12 '14 at 20:14
  • @Brian I was missing the (e)verybody should worship LINQ. This code is missing the `foreach` so it is the longest and generates the most of MSIL instructions (I have debugged into what .NET Framework LINQ does a few times) does not add space efficiency nor time efficiency and is the most dense (no whitespaces) so slows down the reading. I was wondering if there is some hidden benefit of using this style other than it is possible. But I get your point, it is a matter of taste and my comment was practically pointless and OP's question was answered – xmojmr Nov 12 '14 at 21:52
  • 3
    Personally, I would probably have put `.Cast` on its own line, and, if the `.Select` had more parameters, put each parameter on its own line. Having mentally modeled LINQ as a pipeline of transformations, I find that scanning LINQ code written in such a manner to be faster than reading the equivalent non-LINQ code, especially in the case of longer sequences of transformations. I normally outright ignore any indirection-based performance penalties unless it's a legitimate bottleneck. I've never had this happen in production code, but have experienced it when writing recursive puzzle solvers. – Brian Nov 13 '14 at 02:29
5
foreach (var enumValue in Enum.GetValues(typeof(StuffEnum)))
{
    Console.WriteLine("Name: {0}, Value: {1}", enumValue, (int)enumValue);
}

results in

Name: New, Value: 0
Name: Old, Value: 1
Name: Fresh, Value: 2
Bruno
  • 1,033
  • 11
  • 14