0

I'm currently working on a C# program that utilizes a fair number of enums. It's organized in such a way that a class contains many enums, and there are several of these classes. However, a couple enums are identical between the two--for organizational purposes, it would be nice to have a kind of 'SharedEnums' class, and then do something like this:

public class FirstEnumCollection {
 public enum Fruits = SharedEnums.Fruits;
 //insert non-shared enums here
}
public class FirstEnumCollection {
 public enum Fruits = SharedEnums.Fruits;
 //insert non-shared enums here
}
public class SharedEnums { 
 public enum Fruits {
 Apple,
 Pear
 }
}

This way, we won't have the same enums declared multiple times across classes, and changing the shared enum will change the enum in both classes. I've tried several variations of my example without luck, and I'm fairly certain it can't be done, but would like confirmation.

Scott Hannen
29.5k4 gold badges53 silver badges69 bronze badges
asked Jul 26, 2016 at 20:57
2
  • 5
    Why not use it directly? Commented Jul 26, 2016 at 20:58
  • you don't even need to put the enums in a class Commented Jul 26, 2016 at 21:05

2 Answers 2

5

An enum doesn't need to "belong" to a class. You can declare it outside of any class:

public enum Fruits {
 Apple,
 Pear
}

and then use it within any class.

answered Jul 26, 2016 at 21:05
Sign up to request clarification or add additional context in comments.

2 Comments

The class is intended to be more of an organizational tool than anything--all the enums are declared within some 'MyEnums' namespace, with each class representing where the enum will be used in the application (I've trivialized the names here). So the enum never gets used directly in the 'FirstEnumCollection' but rather used by other classes.
In that case there's no need to use a class as a namespace. That's necessary with constants but not enums. Namespaces exist largely for that purpose. But primarily they disambiguate when you have two entities with the same name. But if you have two enums that are the same and you want to set one equal to the other there's no need for it. It's just increased maintenance. Better to just have the one.
1

An enum can be defined at namespace scope, you are not required to create an enum inside a class. You can imagine an enum like a struct, or a class, it's a type that you can use in the code. So, define it at namespace scope

namespace MyCode
{
 public enum Days { M, T, W, T, F, Sa, Su }
}
answered Jul 26, 2016 at 21:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.