Enum.GetValues() Return Type
You need to cast the result to the actual array type you want
(Response[])Enum.GetValues(typeof(Response))
as GetValues isn't strongly typed
EDIT: just re-read the answer. You need to explicitly cast each enum value to the underlying type, as GetValues returns an array of the actual enum type rather than the base type. Enum.GetUnderlyingType could help with this.
Confusion about return value of Enum.GetValues()
GetValue returns an enumeration value object. This object is of a class (Enum class) that will cast to a string with its name, or cast to an integer with its value. So when the string formatting expects a string, it gets that. When it asks for a decimal (Format specifier D), the enum value gives it the default underlying value which is Int32, which easily casts to a Decimal.
Enum.GetValues(typeof(....)) not returning the proper enum values
Because under the covers Enums are just ints - the second returns the values of Enum1
, but really those values are just 0
and 1
. When you cast those values to the type Enum2
these are still valid and correspond to the values "A" and "B".
Why does Enum.GetValues() return names when using var?
Enum.GetValues
is declared as returning Array
.
The array that it returns contains the actual values as ReportStatus
values.
Therefore, the var
keyword becomes object
, and the value
variable holds (boxed) typed enum values.
The Console.WriteLine
call resolves to the overload that takes an object
and calls ToString()
on the object, which, for enums, returns the name.
When you iterate over an int
, the compiler implicitly casts the values to int
, and the value
variable holds normal (and non-boxed) int
values.
Therefore, the Console.WriteLine
call resolves to the overload that takes an int
and prints it.
If you change int
to DateTime
(or any other type), it will still compile, but it will throw an InvalidCastException
at runtime.
Enum.GetValues() doesn't return what you (I) would expect
0X16 is a hexadecimal value which is equal to 1*16 + 6 = 22.
Similarly,
0x32 = 3*16 + 2 = 50
0x64 = 6*16 + 4 = 100 and so on.
Try changing hexadecimal values to decimal ones, i.e,
[Flags]
public enum CoinSizes
{
[Display(Name = "0.01")]
OneCent = 1,
[Display(Name = "0.02")]
TwoCent = 2,
[Display(Name = "0.05")]
FiveCent = 4,
[Display(Name = "0.10")]
TenCent = 8,
[Display(Name = "0.20")]
TwentyCent = 16,
[Display(Name = "0.25")]
TwentyFiveCent = 32,
[Display(Name = "0.50")]
FiftyCent = 64,
[Display(Name = "1.00")]
OneDollar = 128,
[Display(Name = "2.00")]
TowDollar = 256,
[Display(Name = "5.00")]
FiveDollar = 512,
[Display(Name = "10.00")]
TenDollar = 1024,
[Display(Name = "25.00")]
TwentyFiveDollar = 2048,
[Display(Name = "50.00")]
FiftyDollar = 4096
}
It would be unwise to use Flags enums with colliding values. If used, Oring or ANDing them would produce undesired results.
How to use the result of Enum.GetValues() method?
If you want an array of names, you can just use Enum.GetNames
Method
Retrieves an array of the names of the constants in a specified enumeration.
using System;
public class Program
{
public static void Main()
{
string[] names = Enum.GetNames(typeof(MyEnumList));
Console.WriteLine("[{0}]", string.Join(",", names));
}
}
public enum MyEnumList
{
egg, apple, orange, potato
}
output:
[egg,apple,orange,potato]
If you want a string[]
of the numeric values:
using System;
using System.Linq;
public class Program
{
public static void Main()
{
string[] enumAsStrings = Enum.GetValues(typeof(MyEnumList)) // strongly typed values
.Cast<int>() // Get the _numeric_ values
.Select(x => x.ToString()) // conv. to string
.ToArray(); // give me an array
Console.WriteLine("[{0}]",string.Join(",", enumAsStrings));
}
}
public enum MyEnumList
{
egg, apple, orange, potato
}
output:
[0,1,2,3]
Difference between Enum.GetValues and Enum.GetNames
GetValues will return an Array of the underlying integer values for each item in the Enum.
GetNames will return a string array of the Names for the items in the enum.
The Array returned by GetValues implements IList while the string[] returned by GetNames does not, which explains the binding differences.
How do I convert an enum to a list in C#?
This will return an IEnumerable<SomeEnum>
of all the values of an Enum.
Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>();
If you want that to be a List<SomeEnum>
, just add .ToList()
after .Cast<SomeEnum>()
.
To use the Cast function on an Array you need to have the System.Linq
in your using section.
How to enumerate an enum?
foreach (Suit suit in (Suit[]) Enum.GetValues(typeof(Suit)))
{
}
Note: The cast to (Suit[])
is not strictly necessary, but it does make the code 0.5 ns faster.
C# HttpGet Enum.GetValues return liste
While commented hints are not wrong, You have here a walk through edition, using xUnit for the extended version first
public enum NotificationTemplateType
{
Undefined = 0,
Pigeon = 1,
Shout = 2,
Mail = 3,
Team = 4,
Skype = 5,
Slack = 6,
Viber = 7,
}
[Fact]
public void VerifyIsAListTest()
{
var undefinedArray = Enum.GetValues(typeof(NotificationTemplateType));
var specificArray = (NotificationTemplateType[])undefinedArray;
var elementsListThoughArrayWillBeFineWhenReturningBesidesForTheController = specificArray.ToList();
Assert.IsType<List<NotificationTemplateType>>(elementsListThoughArrayWillBeFineWhenReturningBesidesForTheController);
}
So Your method could be (please format better with linebreaks)
[HttpGet("GetType")]
public async Task<IActionResult> GetTemplateType()
{
var liste = ((NotificationTemplateType[])Enum.GetValues(typeof(NotificationTemplateType))).ToList();
return Ok(liste);
}
Related Topics
How to Implement Full Row Selecting in Gridview Without Select Button
Linq: Passing Lambda Expression as Parameter to Be Executed and Returned by Method
Getting the Index of a Particular Item in Array
How to Create a Dictionary of Generic Types
Hide Form Instead of Closing When Close Button Clicked
Formatting Datetime in ASP.NET Core 3.0 Using System.Text.JSON
Variable Declarations Following If Statements
Date Difference in Years Using C#
Count Number of Mondays in a Given Date Range
How to Check If Another Instance of the Application Is Running
Is There an Exponent Operator in C#
How to Check If Thread Finished Execution
Determine Number of Pages in a PDF File
Get String Name of Property Using Reflection
Is There a .Net Way to Enumerate All Available Network Printers