Tuesday, 5 February 2013

Enums

An enum is basically a value type with a set of related named constants often which is referred to as an enumerator list.
  • The enum keyword is for declaring an enumeration. An Enumeration is a primitive data type which is user defined data type.
  • An Enum is basically used to create numeric constants in .NET framework. All the member of an enum are of enum type and there must be a numeric value for an each enum type.
  • Enums type can be integer (float, int, byte, double etc.). But if you used beside int it has to be cast.
  • Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
class Program { public enum DayoftheWeek { Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7 } static void Main(string[] args) { string[] values = Enum.GetNames(typeof(DayofWeek)); int total = 0; foreach (string s in values) { Console.WriteLine(s); total++; } Console.WriteLine ("Total values in enum type is : {0}", total); Console.WriteLine (); int[] n = (int[])Enum.GetValues(typeof(DayofWeek)); foreach (int x in n) { Console.WriteLine(x); } Console.ReadLine(); } }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.