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

  • Home
  • SEARCH
  • 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 4273486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:43:10+00:00 2026-05-21T07:43:10+00:00

I need to convert enum to dictionary and after that format each name (value)

  • 0

I need to convert enum to dictionary and after that format each name (value) of dictionary.

public static Dictionary<int, string> EnumToDictionary<TK>(Func<TK, string > func)
      {
        if (typeof(TK).BaseType != typeof(Enum))
              throw new InvalidCastException();


           return Enum.GetValues(
                typeof(TK))
                .Cast<Int32>()
                .ToDictionary(
                        currentItem => currentItem => Enum.GetName(typeof(TK), currentItem))
                 /*. func for each name */;
           }



      public enum Types {

            type1 = 0,
            type2 = 1,
            type3 = 2
         }


       public string FormatName(Types t) {
             switch (t) {
                         case Types.type1:
                                return "mytype1";

                         case Types.type2:
                              return "mytype2";

                         case Types.type3:
                              return "mytype3";

                           default:
                                return string.Empty;
                 }
           }

After that I need to do something like this:

var resultedAndFormatedDictionary = 
EnumToDictionary<Types>(/*delegate FormatName() for each element of dictionary() */);

How do I define and implement delegate (Func<object, string > func), which performs some action for each value of dictionary?

UPDATE:
Correspoding result is

var a = EnumToDictionary<Types>(FormatName);

   //a[0] == "mytype1"
   //a[1] == "mytype2"
   //a[2] == "mytype3"
  • 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-21T07:43:11+00:00Added an answer on May 21, 2026 at 7:43 am

    Guessing from your question you want to achieve to create a dictionary from an enum where the enum value as int is the key and some formatted name for it is the value, right?

    Well, first the function you pass in should take TK as argument, shouldn’t it?
    Second, throwing a InvalidCastException seems a bit strange. An InvalidOperationException might be more appropriate (see also this question)
    Third ToDictionary is already quite close:

    public static Dictionary<int, string> EnumToDictionary<TK>(Func<TK, string> func)
    {
         if (typeof(TK).BaseType != typeof(Enum))
            throw new InvalidOperationException("Type must be enum");
    
         return Enum.GetValues(typeof(TK)).Cast<TK>().ToDictionary(x => Convert.ToInt32(x), x => func(x));
    }
    

    Now you can call it like this:

    public enum Types 
    {
        type1 = 0,
        type2 = 1,
        type3 = 2
    }
    
    public string FormatName(Types t) 
    {
        switch (t) 
        {
            case Types.type1:
                return "mytype1";
    
            case Types.type2:
                return "mytype2";
    
            case Types.type3:
                 return "mytype3";
    
            default:
                return string.Empty;
         }
    }
    
    var resultedAndFormatedDictionary = EnumToDictionary<Types>(FormatName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to convert RGB or HEX colors to Long Int for another program that
I need to convert a value which is in a DateTime variable into a
I need to convert from a SQLVARCHAR to a string data type Variable definitions
I need to convert an enumeration member (its value, not the identifier) to a
I got an Int16 value, from the database, and need to convert this to
I need to convert a navigable map to a 2D String array. Below given
I need to convert a string like this: A &#039;quote&#039; is <b>bold</b> into: A
I need to convert a String representation of a nested List back to a
I need to convert a date from this format: 2002-10-10T12:00:00-05:00 (xs:dateTime as defined in
I need to convert a VARCHAR with a hex number to INT , I

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.