Nested enum in C # and classes

I heard that enum not possible in C #. Then how to transform the following structure into a class hierarchy or something else. So I want the class to work like enum

enter image description here

+7
source share
4 answers

nested classes and constant fields

 class Cat1 { public const int Public = 1; public class Private { public const int Abc = 2; public const int Mno = 3; public const int Pqr = 4; } } 
+4
source
 public class Cat1 { public enum Publicpart { Xyz } private enum Privatepart { Abc, Mno, Pqr } } 

then you can call it like this

 Cat1.Publicpart.Xyz 

or if you have personal acces

 Cat1.Privatepart.Abc 
+2
source

you can use hirerchy as a class structure, each class has its own enumeration property

+1
source

You have to rethink whether you want to solve these problems with enumerations, because the first category of enumerations represents some kind of concept of "visibility", and the second category is valid only for instances with the visibility of "public".

How about solving a problem with something like this:

 public enum Visibility { Public, Private } public abstract class VisibilityState { public Visibility Visibility { get; private set; } protected VisibilityState(Visibility visibility) { Visibility = visibility; } } public class PublicVisibilityState : VisibilityState { public PublicVisibilityState() : base(Visibility.Public) { } } public class PrivateVisibilityState : VisibilityState { public PrivateVisibilityState() : base(Visibility.Private) { } public OtherEnum OtherEnumState { get; set; } } public enum OtherEnum { Abc, Mno, Pqr } 
+1
source

All Articles