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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:21:00+00:00 2026-05-30T17:21:00+00:00

I am just starting with Generics in C# but have run into a problem

  • 0

I am just starting with Generics in C# but have run into a problem early on, how can I call .HasFlag() on a generic Enum?

public class Example<TEnum> where TEnum : struct {
}

How can I add the [Flags] attribute to it?

  • 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-30T17:21:01+00:00Added an answer on May 30, 2026 at 5:21 pm

    Calling the instance method will require boxing anyway, so, since you can’t constrain to Enum, just abandon generics and use Enum. For example, instead of:

    void Something(TEnum enumValue, TEnum flags)
    {
        if (enumValue.HasFlags(flags))
            //do something ...
    }
    

    Do this:

    void Something(Enum enumValue, Enum flags)
    {
        if (enumValue.HasFlags(flags))
            //do something ...
    }
    

    In a generic method, you could achieve your goal like this:

    void Something(TEnum enumValue, TEnum flags)
    {
        Enum castValue = (Enum)(object)enumValue;
        Enum castFlags = (Enum)(object)flags;
    
        if (castValue.HasFlags(castFlags))
            //do something ...
    }
    

    This will throw an exception at runtime if you call the method with a value type that isn’t an enum. You could also cast via ValueType rather than object, since the type parameter is known to represent a value type:

    Enum castValue = (Enum)(ValueType)enumValue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting out in asp.net. Have just created a login.aspx page in my site
Just starting out with subversion, have set up repos for 3 current projects and
Im just starting with localization today and need some information. I have a project
I'm just starting to work on a logging library that everyone can use to
I am just starting to get to grips with generics and am (ab)using them
Just starting with php and sorry if this is a newbie question but i'm
I have a custom, generic, singly LinkedList which I built myself. I can add,
Just starting out here, I have a Article.rb model that stores articles. So it
I'm just starting to migrate an app I have to 1.3 from 1.1. I'm
I'm just starting out refactoring my code into multiple assemblies, and feel a bit

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.