Created
March 17, 2015 06:43
-
-
Save dnasca/e6bd6d9ac5f502dd85d3 to your computer and use it in GitHub Desktop.
Why Enums?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| /* Enumeration Notes | |
| * | |
| * Enums are value types | |
| * | |
| * Enums are strongly-typed constants | |
| * An explicit cast is needed to convert from enum type to an integral type and vice-versa. | |
| * 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 | |
| * | |
| * The default underlying value for an enum is an integer | |
| * You CAN customize the underlying type and value of an enum | |
| * | |
| * If a program uses a set of integral numbers, one might consider replacing them with enums. | |
| * | |
| * Enums promote Readability and Maintainability | |
| * | |
| * Why Enums? | |
| * In the first region, the code will not use Enums | |
| * In the second region, the code will be modified to use Enums | |
| * | |
| */ | |
| #region Without Enums | |
| class NoEnums | |
| { | |
| public static void WithoutEnums() | |
| { | |
| var customers = new Customer[3]; | |
| customers[0] = new Customer | |
| { | |
| Name = "Derrik", | |
| Gender = 1 //without documentation, how would we know what "1" even means? | |
| }; | |
| customers[1] = new Customer | |
| { | |
| Name = "Anthony", | |
| Gender = 0 | |
| }; | |
| customers[2] = new Customer | |
| { | |
| Name = "Harold", | |
| Gender = 2 | |
| }; | |
| foreach (var customer in customers) | |
| { | |
| Console.WriteLine("Name: {0} and Gender: {1}",customer.Name, GetGender(customer.Gender)); | |
| } | |
| } | |
| public static string GetGender(int gender) | |
| //this is bad, imagine if there were many cases throughout the program. it would be very hard to track which integer corrsponds to which value. | |
| { | |
| switch (gender) | |
| { | |
| case 0: | |
| return "Unknown"; | |
| case 1: | |
| return "Male"; | |
| case 3: | |
| return "Female"; | |
| default: | |
| return "Invalid data"; | |
| } | |
| } | |
| } | |
| public class Customer | |
| { | |
| public string Name { get; set; } | |
| public int Gender { get; set; } | |
| } | |
| #endregion | |
| #region With Enums | |
| class UsingEnums | |
| { | |
| public static void Main() | |
| { | |
| int[] genderValues = (int[]) Enum.GetValues(typeof (Gender)); | |
| foreach (int value in genderValues) | |
| { | |
| Console.WriteLine(value); | |
| } | |
| string[] genderNames = (string[]) Enum.GetNames(typeof (Gender)); | |
| { | |
| foreach (string value in genderNames) | |
| { | |
| Console.WriteLine(value); | |
| } | |
| } | |
| Console.ReadKey(); | |
| } | |
| } | |
| public enum Gender //defaults to int | |
| { | |
| Unknown, //the underlying value, by default, is an int of 0. If we set Unknown = 1, then Male and Female will become subsequent values (2, 3 respectively) | |
| Male, | |
| Female | |
| } | |
| #endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment