Total Number of Items Defined in an Enum

Total number of items defined in an enum

You can use the static method Enum.GetNames which returns an array representing the names of all the items in the enum. The length property of this array equals the number of items defined in the enum

var myEnumMemberCount = Enum.GetNames(typeof(MyEnum)).Length;

Find total number of items defined in an enum C#

Because your namespace is Enum as well. Its confusing the compiler. Try this:

namespace Enum
{
class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(test.FruitCount);
}
}

public class Test
{
enum Fruits { Apple, Orange, Peach }
public int FruitCount
{
get
{
return System.Enum.GetNames(typeof(Fruits)).Length;
}
}
}
}

I basically fully-qualified Enum with System.Enum.GetNames

Number of elements in an enum

I don't believe there is. But what would you do with such a number if they are not sequential, and you don't already have a list of them somewhere? And if they are sequential but start at a different number, you could always do:

enum blah {
FIRST = 128,
SECOND,
THIRD,
END
};
const int blah_count = END - FIRST;

How to get number of possible items of an Enum?

Yes you can use the Enum.values() method to get an array of Enum values then use the length property.

public class Main {
enum WORKDAYS { Monday, Tuesday, Wednesday, Thursday, Friday; }

public static void main(String[] args) {
System.out.println(WORKDAYS.values().length);
// prints 5
}
}

http://ideone.com/zMB6pG

Getting total number of enum items

An enum is a plain-old-C type, therefore it provides no dynamic runtime information.

One alternative is to use the last element of an enum to indicate the count:

typedef enum {
Red,
Green,
Blue,
numColors
} Color;

(How) can I count the items in an enum?

There's not really a good way to do this, usually you see an extra item in the enum, i.e.

enum foobar {foo, bar, baz, quz, FOOBAR_NR_ITEMS};

So then you can do:

int fuz[FOOBAR_NR_ITEMS];

Still not very nice though.

But of course you do realize that just the number of items in an enum is not safe, given e.g.

enum foobar {foo, bar = 5, baz, quz = 20};

the number of items would be 4, but the integer values of the enum values would be way out of the array index range. Using enum values for array indexing is not safe, you should consider other options.

edit: as requested, made the special entry stick out more.

Is it possible to determine the number of elements of a c++ enum class?

Not directly, but you could use the following trick:

enum class Example { A, B, C, D, E, Count };

Then the cardinality is available as static_cast<int>(Example::Count).

Of course, this only works nicely if you let values of the enum be automatically assigned, starting from 0. If that's not the case, you can manually assign the correct cardinality to Count, which is really no different from having to maintain a separate constant anyway:

enum class Example { A = 1, B = 2, C = 4, D = 8, E = 16, Count = 5 };

The one disadvantage is that the compiler will allow you to use Example::Count as an argument for an enum value -- so be careful if you use this! (I personally find this not to be a problem in practice, though.)

Is there a dedicated way to get the number of items in a python `Enum`?

Yes. Enums have several extra abilities that normal classes do not:

class Example(Enum):
this = 1
that = 2
dupe = 1
those = 3

print(len(Example)) # duplicates are not counted
# 3

print(list(Example))
# [<Example.this: 1>, <Example.that: 2>, <Example.those: 3>]

print(Example['this'])
# Example.this

print(Example['dupe'])
# Example.this

print(Example(1))
# Example.this

How can I get the number of enums as a constant?

It's not possible to get the number of enums as a const. Enum.GetNames uses reflection to get these things, and that's inherently a runtime operation. Therefore, it can't be known at compile time, which is a requirement for being const.



Related Topics



Leave a reply



Submit