locked
Is it possible to define string Enum type or other better alternative in c# ? RRS feed

  • Question

  • Dear All,

    Is it possible to define string Enum type in c#?

    something like

    public enum Type

    {

        Type1 = "Light",

        Type2 = "DoorLock",

        Type2 = "ExtensionCord",

    }

    Or there is other similar and better alternative?

    Thanks and Best regards,

    E-John


    • Edited by E-John Friday, May 4, 2018 8:18 AM
    Friday, May 4, 2018 7:52 AM

All replies

  • Hi,

    No an enum is only an integer value, you can specify the kind of integer to use if you but only integer.

    public enum MyEnum : short
    {
      Enum1 = 1,
      Enum2
    }
    

    Regards,


    Yan Grenier

    Merci de bien vouloir "Marquer comme réponse", les réponses qui ont répondues à votre question, et de noter les réponses que vous avez trouvé utiles.

    Friday, May 4, 2018 8:16 AM
  • If you want to associate a description with each enum value you could define an 'EnumDescriptionAttribute'.

    For example: http://wmwood.net/2015/12/18/quick-tip-enum-to-description-in-csharp/

    There are other examples that do the same thing if you just search for "enum description attribute c#"

    This would allow you to define your enum something like:

    public enum Type
    {
        [EnumDescription("Light")]
        Type1,
        [EnumDescription("DoorLock")]
        Type2,
        [EnumDescription("ExtensionCord")]
        Type3,
    }

    With a helper method (as shown in the link) that would allow you to extract the description from the enum value.


    • Edited by RJP1973 Friday, May 4, 2018 9:54 AM
    • Proposed as answer by Fei Hu Monday, May 7, 2018 9:35 AM
    Friday, May 4, 2018 8:28 AM
  • Investigate the next alternative too:

    public sealed class MyEnum
    {
        public static readonly MyEnum
            Type1 = new MyEnum( "Light" ),
            Type2 = new MyEnum( "DoorLock" ),
            Type3 = new MyEnum( "ExtensionCord" );
    
    
        private readonly string Text;
    
        private MyEnum( string t )
        {
            Text = t;
        }
    
        public override string ToString()
        {
            return base.ToString();
        }
    }


    • Edited by Viorel_MVP Friday, May 4, 2018 12:29 PM
    Friday, May 4, 2018 12:29 PM
  • Enums simply eliminate some magic literals in your code. If you want descriptive names consider using constants. If you want to associate descriptive information with an enum value then use the Description attribute.

    public enum ElementType
    {
       [Description("Light")]
       Light = 1,
    
       [Description("DoorLock")]
       DoorLock,
    
       [Description("ExtensionCord")]
       ExtensionCord
    }

    But be very careful here. Enums don't provide any more compiler safety than regular ints. They simply make your code easier to read. Additionally enums are fixed at compile time so if you're trying to create an enum to represent a dynamic set of data (elements that could appear on a factor floor for example) then an enum isn't a good choice.

    In some cases a combination of enums and regular values is better. For example the available Windows groups is completely dynamic and wouldn't make sense as an enum. However there are always pre-defined Windows groups available so MS exposes an enum of well-known groups and also allows for specifying one of the arbitrary ones.

    //Not the actual MS types...
    public enum WellKnownRole
    {
       Administrators,
       Users,
    }
    
    public class RoleManager
    {
       public WindowGroup GetRole ( WellKnownRole role ) { ... }
       public WindowGroup GetRole ( string role ) { ... }
    }
    
    //Usage
    var admins = roleManager.GetRole(WellKnownRole.Administrators);
    var customers = roleManager.GetRole("Customers");


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 4, 2018 2:10 PM
  • I have done essentially the same thing as RJP's 'EnumDescriptionAttribute' using a dictionary.

    Define the enum and the make a dictionary with associated text.

    Text can be accessed by getting the dictionary entry for the enum

    internal enum IdentificationType { FeatureId, ErrorMessage, NpRFeaturesOnly }

                QueryStringIdentifiersByIdentifierType = newDictionary<IdentificationType, string>();

                QueryStringIdentifiersByIdentifierType.Add(IdentificationType.ErrorMessage, "Error");

                QueryStringIdentifiersByIdentifierType.Add(IdentificationType.FeatureId, "Id");

                QueryStringIdentifiersByIdentifierType.Add(IdentificationType.NpRFeaturesOnly, "nprOnly");

    Ethan


    Ethan Strauss



    Friday, May 4, 2018 5:27 PM
  • Hello E-John,

    >>Or there is other similar and better alternative?

    You could use static class instead as below code.

       public static class CustomType
        {
            public static String Type1 = "Light";
            public static String Type2 = "DoorLock";
            public static String Type3 = "ExtensionCord";
        }

    How to define an enum with string value? Hope this would be helpful.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 7, 2018 9:40 AM