Enums are strongly typed constants and make the program readable and maintainable / value type
----------------------------------------------
using System;
public class Enums
{
public static void Main(string[] args)
{
Customer[] customer = new Customer[3];
customer[0] = new Customer
{
Name = "Mark",
Gender = Gender.Male
};
customer[1] = new Customer
{
Name = "Mary",
Gender = Gender.Female
};
customer[2] = new Customer
{
Name = "Sam",
Gender = Gender.Unknown
};
foreach (Customer eachCustomer in customer)
{
Console.WriteLine("Name = {0}, Gender = {1}", eachCustomer.Name, GetGender(eachCustomer.Gender));
}
}
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";
}
}
}
//enum - value type
public enum Gender
{
Unknown, Male, Female
}
public class Customer
{
public string Name { get; set; }
public Gender Gender { get; set; } //using enum type
}
=====================================================
using System;
public class Enums
{
public static void Main(string[] args)
{
short[] values = (short[]) Enum.GetValues(typeof(Gender));
foreach (short value in values)
{
Console.WriteLine(value);
}
string[] Names = Enum.GetNames(typeof(Gender));
foreach (string Name in Names)
{
Console.WriteLine(Name);
}
}
}
//enum - value type
public enum Gender : short //can be changed the type
{
Unknown = 1,
Male,
Female
}
===================================================
using System;
/*
* 1. Enums are enumerations
* 2. strongly typed constant
* 3. default underlying type of an enum is int.
* 4. default value for first element is zero and gets incremented by 1
* 5. possible to customize the underlying type and values
* 6. value types
* 7. using enum keyword - GetValues() , GetNames()
* */
public class Enums
{
public static void Main(string[] args)
{
//strongly typed constants - need to be converted
//Gender gender = (Gender)3;
//int Num = (int)Gender.Unknown;
Gender gender = (Gender)Season.Spring;
}
}
//enum - value type
public enum Gender : short //can be changed the type
{
Unknown = 1,
Male,
Female
}
public enum Season
{
Winter = 1,
Spring = 2,
Summer = 3
}