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 652581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:17:18+00:00 2026-05-13T22:17:18+00:00

I have an enumeration for my Things like so: public enum Things { OneThing,

  • 0

I have an enumeration for my Things like so:

public enum Things
{
   OneThing,
   AnotherThing
}

I would like to write an extension method for this enumeration (similar to Prise’s answer here) but while that method works on an instance of the enumeration, ala

Things thing; var list = thing.ToSelectList();

I would like it to work on the actual enumeration instead:

var list = Things.ToSelectList();

I could just do

var list = default(Things).ToSelectList();

But I don’t like the look of that 🙂

I have gotten closer with the following extension method:

public static SelectList ToSelectList(this Type type)
{
   if (type.IsEnum)
   {
      var values = from Enum e in Enum.GetValues(type)
                   select new { ID = e, Name = e.ToString() };
      return new SelectList(values, "Id", "Name");
   }
   else
   {
      return null;
   }
}

Used like so:

var list = typeof(Things).ToSelectList();

Can we do any better than that?

  • 1 1 Answer
  • 2 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-13T22:17:18+00:00Added an answer on May 13, 2026 at 10:17 pm

    Extension methods only work on instances, so it can’t be done, but with some well-chosen class/method names and generics, you can produce a result that looks just as good:

    public class SelectList
    {
        // Normal SelectList properties/methods go here
    
        public static SelectList Of<T>()
        {
            Type t = typeof(T);
            if (t.IsEnum)
            {
                var values = from Enum e in Enum.GetValues(type)
                             select new { ID = e, Name = e.ToString() };
                return new SelectList(values, "Id", "Name");
            }
            return null;
        }
    }
    

    Then you can get your select list like this:

    var list = SelectList.Of<Things>();
    

    IMO this reads a lot better than Things.ToSelectList().

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

Sidebar

Related Questions

I have this enumeration: public enum Translation { English=1, Russian, French, German } I
I have this enumeration: public enum SymbolPair { AUDJPY = 0, AUDNZD, AUDUSD, CADJPY,
I have an enumeration like Enum Complexity { NotSoComplex, LittleComplex, Complex, VeryComplex } And
I have registered an enumeration type ClefType within my header file - this enum
I have an enum : public enum Enumeration { A, B, C } And
I have a class I've written to act like an enumeration: abstract class Enum
I have an enumeration in the form: Public Enum MyCollections As Integer My_Stuff =
I have a Enumeration as shown in below program public class Test { public
So, I have an enumeration that is as follows: +---------+ |TypeCours| | Enum |
I'm using Scala 2.8 and defining an Enumeration like this: object Stooges extends Enumeration

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.