C# - Enum, Struct

Created:2019-06-19 🎂  Last modified:2019-06-29


  1. Enum

    Enum is one of the value type, the default underlying type is int.

    1. Define and use a enum

      Be default, enum start element from 0. And enum's default value is 0; So if just declaring a enum member (local variable must be assigned a value) without assigning value, its value will be the first element.

                                  public enum Day{
                                      Sun,
                                      Mon,
                                      Tue,
                                      Wed,
                                      Thu,
                                      Fri,
                                      Sat,
                                  }
                                  Day day = Day.Sun;
      
                                  Day day2; // because enum is value type, it must have a default value, which is 0;
                                  Console.WriteLine(day2);
                              

      Even though the underlying data type is int, you cannot directly assign a integer to it.

    2. Check it has a valid value.

                                  public enum State{
                                      GA = 1, MN, CO
                                  }
                                  State s = default(State); // 0
                                  // but 0 is not a valid member.
                                  Console.WriteLine(Enum.IsDefined(typeof(State), s)); // false
                              
    3. string -- Enum

                                  public enum Rating{
                                      G, PG, PG13, R, NC17 
                                  }
                                  Console.WriteLine("Hello World!");
      
                                  Rating r = Rating.PG13;
                                  Console.WriteLine(r); // PG13;
                                  Console.WriteLine(r.ToString()); // PG13;
                                  string pg13 = "PG-13";
                                  try{
                                      r = (Rating)Enum.Parse(typeof(Rating), pg13);
                                  }catch(Exception e){
                                      Console.WriteLine(e.Message); // Requested value 'PG-13' was not found.
                                  }
      
                                  bool result = Enum.TryParse<Rating>(pg13, out r);
                                  Console.WriteLine($"{result}, {r}"); // false, G      default value.
                              
    4. int -- enum

                                  int number = (int)Rating.PG13;
                                  Rating r = (Rating)1;
                                  Console.WriteLine(r); // PG
                                  r = (Rating)100;
                                  Console.WriteLine(r); // 100
                              
    5. int --> string

                                  // public static string GetName (Type enumType, object value);
                                  Console.WriteLine(Enum.GetName(typeof(Rating), 1)); // PG
                                  string s = Enum.GetName(typeof(Rating), 100); // return null
                              
  2. Struct

    1. ref
    2. constructor & destructor
    3. nullable
    4. inheritance
  3. References