Click here to Skip to main content
13,596,867 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


15 bookmarked
Posted 20 Jul 2012
Licenced CPOL

Using enum to define a new enum

, 10 Aug 2012
Rate this:
Please Sign up or sign in to vote.
This tip describes how to use an existing enum to define a new, for example a context specific enum.


As you know, <a title="Enumeration Types (C# Programming Guide)" href="">enum</a> is used to define a static set of values to use in different kinds of situations. Often enums define a single, self-explanatory set of values, but sometimes the same values may be needed in different contexts; Overlapping partially or none at all.

This tip describes how to use a master set of values to define context specific enums.

A simple enum

Let's take animals as an example. You can buy a pet and you can go to zoo to see different kinds of animals. Depending on the logic, you may need a different set of animals in your code, but you need to ensure that they all are unique in the sense of enum definition. So regardless of the context, dog would always be 1 and cat always 2 and so on.

First, let's create a master set of animals:

public enum MasterEnumAnimal {
   Dog       = 1,
   Cat       = 2,
   Bird      = 3,
   Alligator = 4,
   Camel     = 5

Now depending, what you're going to do, buy a pet or go to a zoo to see an animal, you need a different set of possible enum values.

To define the subsets, simply use the original master definition in your subset definitions. Like the following:

public enum Pet {
   Dog  = MasterEnumAnimal.Dog,
   Cat  = MasterEnumAnimal.Cat,
   Bird = MasterEnumAnimal.Bird

public enum ZooAnimal {
   Bird      = MasterEnumAnimal.Bird,
   Alligator = MasterEnumAnimal.Alligator,
   Camel     = MasterEnumAnimal.Camel

As you can see both enums have different animals except both include a bird.

Now consider the following methods:

public static void BuyAPet(Pet pet) {
   System.Console.WriteLine(string.Format("You bought a {0}", pet));

public static void SeeAnAnimal(ZooAnimal animal) {
   System.Console.WriteLine(string.Format("You saw {0}", animal));

Both of the methods accept a certain set of animals, but the possible animals are different based on the context. You see different animals in the zoo compared to what you buy as a pet. However, you can always tell which kind of animal is in question.

An example of using these context specific enums:


Running the above example would give an output like:

You bought a Dog
You saw Bird

Using master enum with flags

Using a flagged enum is no different. Let's take another kind of example with different kinds of materials. You use partly different materials when building a house or making a furniture, but some of the material types are overlapping.

To define the master set of materials, consider the following:

public enum MasterEnumMaterial {
   Wood           = 1 << 0,
   Metal          = 1 << 1,
   Glass          = 1 << 2,
   Leather        = 1 << 3,
   SyntheticFiber = 1 << 4,
   Concrete       = 1 << 5,
   Tile           = 1 << 6,
   Stone          = 1 << 7

To define proper subsets of materials for each purpose, you can use the following definitions:

public enum FurnitureMaterial {
   Wood           = MasterEnumMaterial.Wood,
   Metal          = MasterEnumMaterial.Metal,
   Glass          = MasterEnumMaterial.Glass,
   Leather        = MasterEnumMaterial.Leather,
   SyntheticFiber = MasterEnumMaterial.SyntheticFiber

public enum BuildingMaterial {
   Wood     = MasterEnumMaterial.Wood,
   Metal    = MasterEnumMaterial.Metal,
   Glass    = MasterEnumMaterial.Glass,
   Concrete = MasterEnumMaterial.Concrete,
   Tile     = MasterEnumMaterial.Tile,
   Stone    = MasterEnumMaterial.Stone

As you can see, several materials are common, but some are not. For example, who wants a sofa made of concrete and stone Smile | :) .

Now, to use these materials in different contexts, consider following methods:

public static MasterEnumMaterial BuildAHouse(BuildingMaterial usedMaterials) {
   System.Console.WriteLine("You built a house made of:");
   foreach (BuildingMaterial material in System.Enum.GetValues(typeof(BuildingMaterial))) {
      if (usedMaterials.HasFlag(material)) {
         System.Console.WriteLine(string.Format("  - {0}", material));
   return (MasterEnumMaterial)usedMaterials;

public static MasterEnumMaterial BuyAFurniture(FurnitureMaterial materials) {
   System.Console.WriteLine("You bought a furniture made of:");
   foreach (FurnitureMaterial material in System.Enum.GetValues(typeof(FurnitureMaterial))) {
      if (materials.HasFlag(material)) {
         System.Console.WriteLine(string.Format("  - {0}", material));
   return (MasterEnumMaterial)materials;

Now, to test these methods, let's build a house and buy a furniture:

MasterEnumMaterial houseMaterials;
MasterEnumMaterial furnitureMaterials;

houseMaterials = BuildAHouse(BuildingMaterial.Concrete | BuildingMaterial.Glass | BuildingMaterial.Wood);
furnitureMaterials = BuyAFurniture(FurnitureMaterial.Wood | FurnitureMaterial.Leather);

Executing the previous methods would give an output like the following:

You built a house made of:
  - Wood
  - Glass
  - Concrete

You bought a furniture made of:
  - Wood
  - Leather

But more importantly, if you want to know the common materials in the house you built and the furniture you bought, you could resolve this with

foreach (MasterEnumMaterial material in System.Enum.GetValues(typeof(MasterEnumMaterial))) {
   if (houseMaterials.HasFlag(material) && furnitureMaterials.HasFlag(material)) {
      System.Console.WriteLine(string.Format("  - {0}", material));

This would result in following output:

You have following common materials in your house and furniture:
  - Wood 


As you can see, using a single, predefined set of enum values can help to keep a single, uniquelly defined set of values and using subsets of this master enum helps to define context specific sets from the master list of values.

The download contains a small test project for the examples included in this tip.

While you can define a subset from a single enum as seen in this example, you can also define an enum combining several different enums. But that's another story Smile | :)


  • July 21, 2012: Tip created.
  • August 11, 2012: Small corrections.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Europe Europe
This member does have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

QuestionMessage Closed Pin
21-Dec-17 7:08
memberMember 1358848921-Dec-17 7:08 
QuestionMessage Closed Pin
21-Dec-17 4:43
memberMember 1358848921-Dec-17 4:43 
QuestionMessage Closed Pin
10-Aug-17 11:11
memberSalman Sajjad10-Aug-17 11:11 
QuestionWhat about data annotations? Pin
Member 1097567020-Oct-14 21:28
memberMember 1097567020-Oct-14 21:28 
AnswerRe: What about data annotations? Pin
Mika Wendelius21-Oct-14 9:31
mentorMika Wendelius21-Oct-14 9:31 
GeneralMy vote of 4 Pin
Milan Mathew12-Aug-12 5:14
memberMilan Mathew12-Aug-12 5:14 
GeneralRe: My vote of 4 Pin
Mika Wendelius12-Aug-12 5:59
mvpMika Wendelius12-Aug-12 5:59 
GeneralMy vote of 5 Pin
Nildo Soares de Araujo23-Jul-12 7:13
memberNildo Soares de Araujo23-Jul-12 7:13 
GeneralRe: My vote of 5 Pin
Mika Wendelius23-Jul-12 20:56
mvpMika Wendelius23-Jul-12 20:56 
GeneralGood Job Pin
Amol_B22-Jul-12 23:17
memberAmol_B22-Jul-12 23:17 
GeneralRe: Good Job Pin
Mika Wendelius22-Jul-12 23:25
mvpMika Wendelius22-Jul-12 23:25 
GeneralMessage Closed Pin
21-Dec-17 4:24
memberMember 1358848921-Dec-17 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 11 Aug 2012
Article Copyright 2012 by Wendelius
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid