WHY ENUMS
Enums are strongly typed constants
If a program uses set of integral numbers, consider replacing them with enums. otherwise the program becomes less
> Readable
> Maintainable
———————————————————————————————————————–
using System;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
public static void Main()
{
Customer[] customers = new Customer[3];
customers[0] = new Customer
{
Name = “Malla”,
Gender = 1
};
customers[1] = new Customer
{
Name = “mary”,
Gender = 2
};
customers[2] = new Customer
{
Name = “hyyjjj”,
Gender = 0
};
foreach (Customer customer in customers)
{
Console.WriteLine(“Name = {0} && Gender = {1}”, customer.Name, GetGender(customer.Gender));
Console.ReadLine();
}
}
public static string GetGender(int gender)
{
switch(gender)
{
// INTEGRAL NUMBERS BESIDE CASES SHOULD BE REPLACES WITH ENUMS TO //BETTER READABLE
case 0:
return “Unknown”;
case 1:
return “Male”;
case 2:
return “Female”;
default:
return “Invalid data detected”;
}
}
//0 Unknown
//1 Male
//2 Female
public class Customer
{
public string Name { get; set; }
public int Gender { get; set; }
}
}
}
————————————————————————————————————————
USE ENUMS FOR THE ABOVE PROGRAM MORE READABLE
using System;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
public static void Main()
{
Customer[] customers = new Customer[3];
customers[0] = new Customer
{
Name = “Malla”,
Gender = Gender.Male
};
customers[1] = new Customer
{
Name = “mary”,
Gender = Gender.Female
};
customers[2] = new Customer
{
Name = “hyyjjj”,
Gender = Gender.Unknown
};
foreach (Customer customer in customers)
{
Console.WriteLine(“Name = {0} && Gender = {1}”, customer.Name, GetGender(customer.Gender));
Console.ReadLine();
}
}
public static string GetGender(Gender gender)
{
switch(gender)
{
case Gender.Unknown:
return “Unknown”;
case Gender.Male:
return “Male”;
case Gender.Female:
return “Female”;
default:
return “Invalid data detected”;
}
}
//0 Unknown
//1 Male
//2 Female
public enum Gender
{
Unknown,
Male,
Female
};
public class Customer
{
public string Name { get; set; }
public Gender Gender { get; set; }
}
}
}
————————————————————————————————————————-
ENUMS USAGE
If a program uses set of integral numbers, consider replacing them with enums. otherwise the program becomes less
> Readable
> Maintainable
1.Enums are enumerations
2.Enums are strongly typed constants, Hence, an explicit cast is needed to convert from enum type to an integral type and vice versa.Also an enum of one type cannot be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
3. The default underlying type of an enum is int.
4.The default value for firt element is zero and gets incremently by 1.
5 it is possible to customize the underlying type and values.
6. Enums are value types.
7. Enum keyword (all small letters) is used to create enumerations. where as Enum class, contains staticGetValues() and GetNames() methods which can be used to list Enum underlying type values and Names.
Enum(with capital Eis a class)
enum (with small e is to create enumerations)