Define enum type member field in a class - CSharp Custom Type

CSharp examples for Custom Type:Enum

Description

Define enum type member field in a class

Demo Code

using System;/*www.j  a  va2 s.  c om*/
using System.Collections.Generic;
using System.Text;
public enum MovieGenre
{
   Action,
   Comedy,
   Scifi,
   Horror
}
public class Movie
{
   public string Title { get; set; }
   public int Year { get; set; }
   public MovieGenre Genre { get; set; }
   public Movie(string title, int year, MovieGenre genre)
   {
      Title = title;
      Year = year;
      Genre = genre;
   }
}
class Program
{
   static void Main(string[] args)
   {
      Movie movie = new Movie("Star Wars", 1977, MovieGenre.Scifi);
      printMovieGenre(movie);
      Console.ReadKey();
   }
   private static void printMovieGenre(Movie movie)
   {
      switch (movie.Genre)
      {
         case MovieGenre.Action:
         Console.WriteLine("Action Movie");
         break;
         case MovieGenre.Comedy:
         Console.WriteLine("Comedy Movie");
         break;
         case MovieGenre.Scifi:
         Console.WriteLine("Science Fiction Movie");
         break;
         case MovieGenre.Horror:
         Console.WriteLine("Horror Movie");
         break;
         default:
         Console.WriteLine("Invalid Movie Genre");
         break;
      }
   }
}

Result


Related Tutorials