InterviewSolution
Saved Bookmarks
| 1. |
How to get int value from enum? |
|
Answer» The int value can be obtained from enum by simply casting the enum. This process is successful because int is the default underlying type for enum. Similarly, for enums with different underlying types such as uint, ulong etc. the cast should be the type of enum. A program that gets the int value from Enum is given as follows: using System; public CLASS Demo { public enum Fruits { Apple, MANGO, Orange, Peach, Melon } public static void Main() { int num1 = (int)Fruits.Apple; int num2 = (int)Fruits.Mango; int num3 = (int)Fruits.Orange; int num4 = (int)Fruits.Peach; int num5 = (int)Fruits.Melon; Console.WriteLine("Apple: " + num1); Console.WriteLine("Mango: " + num2); Console.WriteLine("Orange: " + num3); Console.WriteLine("Peach: " + num4); Console.WriteLine("Melon: " + num5); } }The OUTPUT of the above program is as follows: Apple: 0 Mango: 1 Orange: 2 Peach: 3 Melon: 4 |
|