Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 271253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:07:56+00:00 2026-05-12T00:07:56+00:00

Since enumeration uses integers, what other structure can I use to give me enum-like

  • 0

Since enumeration uses integers, what other structure can I use to give me enum-like access to the value linked to the name:

[I know this is wrong, looking for alternative]

private enum Project
    {
        Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1"),
        Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2"),
        Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1"),
        Sales = new Guid("A5690E7-1111-4AFB-B44D-1DF3AD66D435"),
        Replacement = new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"),
        Modem = new Guid("6F686C73-504B-111-9A0B-850C26FDB25F"),
        Audit = new Guid("30558C7-66D9-4189-9BD9-2B87D11190"),
        Queries = new Guid("9985242-516A-4151-B7DD-851112F562")
    }

EDIT 2014-07-20

This is a newer answer to this question. Using the Attribute class with a helper method, define the extra attributes needed on your enum.

 public enum MultiValueEnum
    {
        [FooAttribute("alpha", 20d, true)]
        First,
        [FooAttribute("beta", 40.91d, false)]
        Second,
        [FooAttribute("gamma", 1.2d, false)]
        Third,
    }     

  public class FooAttribute : Attribute
            {
                internal FooAttribute(string name, double percentage, bool isGood)
                {
                    this.Name = name;
                    this.Percentage = (decimal)percentage;
                    this.IsGood = isGood;
                }
                public string Name { get; private set; }
                public decimal Percentage { get; private set; }
                public bool IsGood { get; private set; }
            }



  public static TAttribute GetAttribute<TAttribute>(this Enum value)
        where TAttribute : Attribute
        {
            var type = value.GetType();
            var name = Enum.GetName(type, value);
            return type.GetField(name)
                .GetCustomAttributes(false)
                .OfType<TAttribute>()
                .SingleOrDefault();
        }

Which makes it this easy:

        MultiValueEnum enumVar = MultiValueEnum.First;
        var enumStringValue = enumVar.GetAttribute<FooAttribute>().Name;
        var enumValueDecimal = enumVar.GetAttribute<FooAttribute>().Percentage;
        var enumBool = enumVar.GetAttribute<FooAttribute>().IsGood;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:07:57+00:00Added an answer on May 12, 2026 at 12:07 am

    Otherwise you could create a custom Attribute for your enum, which can hold the Guid.

    Something alongside these lines:

    class EnumGuid : Attribute
    {
        public Guid Guid;
    
        public EnumGuid(string guid)
        {
            Guid = new Guid(guid);
        }
    }
    

    And you’d then use it like so:

    enum Project
    {
        [EnumGuid("2ED3164-BB48-499B-86C4-A2B1114BF1")]
        Cleanup = 1,
        [EnumGuid("39D31D4-28EC-4832-827B-A11129EB2")]
        Maintenance = 2
        // and so forth, notice the integer value isn't supposed to be used, 
        // it's merely there because not assigning any value is a performance overhead.
    }
    

    And finally you could (I always do this) create an extension for easily getting the guid:

    static Guid GetEnumGuid(this Enum e)
    {
        Type type = e.GetType();
    
        MemberInfo[] memInfo = type.GetMember(e.ToString());
    
        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(EnumGuid),false);
            if (attrs != null && attrs.Length > 0)
                return ((EnumGuid)attrs[0]).Guid;
        }
    
        throw new ArgumentException("Enum " + e.ToString() + " has no EnumGuid defined!");
    }
    

    So in the end all you have to with your enums is:

    Guid guid = Project.Cleanup.GetEnumGuid();
    

    I use this approach to attach descriptions to enums, typically longer strings containing spaces, which thus cannot be used as names.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

.NET supports the FlagsAttribute for enum's, which indicates that an enumeration can be treated
While qualifying an enumeration value with the name of the enumeration is not valid
Basically everyone writing about member enumeration in JavaScript heavily advocates the use of the
From what I can tell DateTickUnitType is an enumeration that cannot be extended or
I really like Last() and would use it all the time for List<T> s.
I'd like to create a copy of an IEnumerator<T> so that I can restart
In the post Enum ToString , a method is described to use the custom
I have a base class that has an enum value that I am using
Possible Duplicate: Enum type constraints in C# Is it possible to use enum types
Possible Duplicate: .NET Enumeration allows comma in the last field public enum SubPackageBackupModes {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.