This question already has answers here:
Total number of items defined in an enum
(10 answers)
Closed 8 years ago.
I need to know the number of enums that have been declared.
So far I am using the following which works but I wonder if there is a better way?
enum MyEnum
{
foo = 1,
bar = 2
}
int noOfEnums = Enum.GetNames(typeof(MyEnum)).Count();
noOfEnums will be 2;
You may try to use:
enum MyEnum
{
foo = 1,
bar = 2
}
var noOfEnums = Enum.GetNames(typeof(MyEnum)).Length;
The length property of this array equals the number of items defined in the enum
Also check Count Items in a C# Enum
Related
This question already has answers here:
How do I cast int to enum in C#?
(32 answers)
Convert String to Type in C# [duplicate]
(4 answers)
Enum String Name from Value
(14 answers)
Get the name of Enum value
(3 answers)
Get enum name when value is known
(7 answers)
Closed 5 years ago.
From another layer in an application I can get back the name of the enum type and an integer value. How can I use this data to get the string representation of that value for that enum.
For example, if I have an enum:
public enum Cheese
{
Edam = 1,
Cheddar = 3,
Brie = 201,
RedLeicester = 1001
}
and the data I have available is
string dataType = "Cheese";
int dataValue = 3;
How can I get the result:
"Cheddar"
In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assembly where enum is defined and all your enums are defined in the same namespace you know at compile time, you can do it like that:
string dataType = "Cheese";
int dataValue = 3;
var enumType = Type.GetType("Namespaces.Of.Your.Enums." + dataType);
var name = Enum.GetName(enumType, dataValue);
If your enum is in another assembly than this code is running at - you will need to provide assembly qualified name of your enum type (again with namespace).
Type t = Type.GetType(dataType)
string value = Enum.GetName(t, dataValue)
should do the trick (uncompiled, on subway)
enum.Parse() would be your friend here.
Like so:
int cheeseType = 1;
(Cheese)Enum.Parse(typeof(Cheese), cheeseType.ToString());
This question already has answers here:
How to compare enum and int values?
(7 answers)
What's the simplest way to compare an enum to a integer value return from a DB
(5 answers)
C# Enum - How to Compare Value
(5 answers)
Closed 5 years ago.
I have a simple enum with a few values
Enum Status{
Available,
Taken,
Sold//and many more
}
I want to check whether the status is somewhere in between the possible values(assuming the values are written in such an order that the lowest are the beginning of a process, and the last ones are the final steps of the process)
Something like
Status s1;//Some value
if(s1<5 && s1>3)
//The value is one of the enum values in those range
//(for example the job has already been accepted, but has still not been
//shipped)
Is it possible?
Thanks.
You can assign integer values to your enum and then check.
enum Status
{
Available = 1,
Taken = 2,
Sold = 3
//and many more
}
Status s1; // any value
if ((int)s1 <= 5 && (int)s1 >= 3)
This question already has answers here:
Finding an enum value by its Description Attribute [duplicate]
(3 answers)
Get enum from enum attribute
(7 answers)
Closed 8 years ago.
I have an enum width descriptions:
public enum PostTypes {
[Description("Business")]
Business = 1 << 0,
[Description("Design"]
Design = 1 << 1,
[Description("Marketing")]
Marketing = 1 << 2,
} // PostTypes
I would like to get an Enum based on Description, so:
1 - If I have String="Business" I would get PostTypes.Business.
2 - If I have String[] = new String[] { "Business", "Design" } I would have PostTypes.Business | PostTypes.Design.
UPDATE
I am using the following:
Enum.GetValues(typeof(PostTypes)).Cast<PostTypes>().Where(v => new String[] { "Business", "Design" }.Contains(v.GetDescription())).Aggregate((a, b) => a | b);
This is working even for Flag enums and string arrays.
Could someone, please, help me in creating an extension where I pass the enum type and use a func for setting the rule for search, either the Description attribute or any other.
How can I do this?
Thank You,
Miguel
This question already has answers here:
How do I check if more than one enum flag is set?
(5 answers)
Closed 2 years ago.
So I have a flags Enum
public Enum test
{
test1 = 1,
test2 = 2,
test3 = 4,
etc.
}
How can I test that one bit, and only one bit is set?
I've 100% done this before but my mind is not working this am!
To check that only a single bit is set in a number, the number must (by definition) be a power of two. As such, you can use the following to test:
int intVal = ((int)myEnumFlags);
bool singleBitIsSet = intVal != 0 && (intVal & (intVal-1)) == 0;
My favorite reference for this kind of thing:
http://aggregate.org/MAGIC
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enums returning int value
I want to numerate "Stack", "Overflow" and "Stackoverflow". Therefore I use enumerate.
enum Status : int
{
Stack = 1,
Overflow = 2,
StackOverflow = 3
}
But when I want to get value of Stack, I get "Stack".
int status = 0;
status = Status.Stack; //I want to assign 1 to status but Status.Stack returns "Stack"
How can I do it?
Just cast to int:
status = (int) Status.Stack;