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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:58:40+00:00 2026-06-15T15:58:40+00:00

I wrote this EnumHelper method public static IEnumerable<T> AsEnumerable<TEnum, T>(Func<TEnum, T> projection = null)

  • 0

I wrote this EnumHelper method

    public static IEnumerable<T> AsEnumerable<TEnum, T>(Func<TEnum, T> projection = null) where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
            throw new InvalidOperationException("Type parameter TEnum must be an enum");

        if (projection == null)
            return Enum.GetValues(typeof (TEnum)).OfType<TEnum>();

        return Enum.GetValues(typeof (TEnum)).OfType<TEnum>().Select(projection);
    }

I get a compile time error in the first return. That returns an IEnumerable<TEnum>

Error 46 Cannot implicitly convert type System.Collections.Generic.IEnumerable<TEnum> to System.Collections.Generic.IEnumerable<T>

I don’t have any constraints on T, so T is more generic than TEnum. In IEnumerable<out T> T is convariant, so why do I still get the error?

  • 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-06-15T15:58:41+00:00Added an answer on June 15, 2026 at 3:58 pm

    Covariance only applies if there is a polymorphic relationship between the two types. In your case, TEnum and T are not constrained to be related, so covariance does not apply.

    You could trivially fix this issue by casting your enumeration members directly to your target type:

    if (projection == null)
        return Enum.GetValues(typeof(TEnum)).OfType<T>();
    

    Edit: I would suggest eliminating the projection parameter, and define your method more simply like so:

    public static IEnumerable<TEnum> AsEnumerable<TEnum>() where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
            throw new InvalidOperationException("Type parameter TEnum must be an enum");
    
        return Enum.GetValues(typeof(TEnum)).OfType<TEnum>();
    }
    

    If you do need to perform a projection, you could use the standard LINQ Select operation on the returned sequence:

    var optionsA = AsEnumerable<RegexOptions>();
    var optionsB = AsEnumerable<RegexOptions>().Select(o => o.ToString());
    

    This will give you practically the same conciseness as your code, but save you the trouble of maintaining the optional parameter.

    Edit2: If you really want to define an overload for the projection, I would suggest implementing all the logic within it, and then call it using the identity function from the parameterless version:

    public static IEnumerable<TEnum> AsEnumerable<TEnum>() where TEnum : struct
    {
        return AsEnumerable<TEnum, TEnum>(e => e);
    }
    
    public static IEnumerable<TResult> AsEnumerable<TEnum, TResult>(
        Func<TEnum, TResult> projection) where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
            throw new InvalidOperationException("Type parameter TEnum must be an enum");
    
        return Enum.GetValues(typeof(TEnum)).OfType<TEnum>().Select(projection);
    }
    

    Sample calls:

    var optionsA = AsEnumerable<RegexOptions>();
    var optionsB = AsEnumerable<RegexOptions, string>(o => o.ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this method (almost similar in other post) public void update(string fileName, string
I wrote this method to update an excel cell: public void update(string fileName, string
I wrote this n-array tree class. I want to write a method to add
I wrote this HttpRequest method, but for some reason it always goes to 404
I wrote this extension for stringbuilder in VB.NET: Imports System.Runtime.CompilerServices Public Module sbExtension <Extension()>
I wrote this simple linq-to-xml query and it seems that null exception could not
I wrote this code: class Address{ private: std::string street; int house; public: Address(std::string s,
I wrote this code to control whether a char[] is null or not. char[]
I wrote this code for zoom in/out . it works but even with one
I wrote this as a simple dice game. It works as I want except

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.