I found below code by web search and I don’t understand why the author uses the enum in that class. Is it for value limitation? Is it so WhichSex can take only ‘Male’ and ‘Female’ string values?
public class Person
{
public enum Sex
{
Male,
Female,
}
public string Name { get; set; }
public bool Moustache { get; set; }
public bool Goatee { get; set; }
public bool Beard { get; set; }
public Sex WhichSex { get; set; }
public double Height { get; set; }
public DateTime BirthDate { get; set; }
public bool Favorite { get; set; }
}
The enum is used as a way of representing numeric data in a way that is more easily recognized in programming.
Behind the enum, is an integer datatype seeded starting at zero, so in this case Male is 0 and Female is 1. This allows you to have the strings Male and Female in your code while storing the result as an integer instead of a string which is easier on storage and bandwidth.
The reason for the enum here is simple. They can ToString it and get the strings “Male” or “Female” for reporting, and they can also add another value for “unknown” or “not specified” later down the road if needed.
http://msdn.microsoft.com/en-us/library/sbbt4032%28v=vs.80%29.aspx