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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:14:41+00:00 2026-05-11T06:14:41+00:00

As a follow on from this question. How can I call a function and

  • 0

As a follow on from this question.

How can I call a function and pass in an Enum?

For example I have the following code:

enum e1 {     //... }  public void test() {     myFunc( e1 ); }  public void myFunc( Enum e ) {     var names = Enum.GetNames(e.GetType());      foreach (var name in names)     {         // do something!     }  } 

Although when I do this I am getting the ‘e1’ is a ‘type’ but is used like a ‘variable’ Error message. Any ideas to help?

I am trying to keep the function generic to work on any Enum not just a specific type? Is this even possible?… How about using a generic function? would this work?

  • 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. 2026-05-11T06:14:42+00:00Added an answer on May 11, 2026 at 6:14 am

    You can use a generic function:

        public void myFunc<T>()     {         var names = Enum.GetNames(typeof(T));          foreach (var name in names)         {             // do something!         }     } 

    and call like:

        myFunc<e1>(); 

    (EDIT)

    The compiler complains if you try to constraint T to Enum or enum.

    So, to ensure type safety, you can change your function to:

        public static void myFunc<T>()     {         Type t = typeof(T);         if (!t.IsEnum)             throw new InvalidOperationException('Type is not Enum');          var names = Enum.GetNames(t);         foreach (var name in names)         {             // do something!         }     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-on question from the one I asked here . Can constraints
This is a follow-up to my question from yesterday . I have Scott Meyers'
As a follow up for this question Returning from rails controller how can I
This is a follow-on from this question, in which I was trying to suppress
This question is a follow on from this one ... I am binding to
This question is a follow-up from How to indicate that a method was unsuccessful
This is a follow on from my previous question although this is about something
This is a follow up from a question of mine that was just answered
This is a follow on question to How do I delete 1 file from
This question follows on from this vim search question I have a setting in

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.