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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:21:36+00:00 2026-06-18T04:21:36+00:00

How can I convert an array of enums into a generic array of enums

  • 0

How can I convert an array of enums into a generic array of enums in c#.

To be clear:

Given:

public enum PrimaryColor
{
    red = 0,
    blue = 1,
    yellow = 3
}

public enum SecondaryColor
{
    green = 0,
    purple = 1,
    orange = 2
}

I want to do something like this:

public class MyClass
{
    public static void Main()
    {
        PrimaryColor[] pca = {PrimaryColor.blue, PrimaryColor.yellow};
        SecondaryColor[] sca = {SecondaryColor.purple, SecondaryColor.orange};

        Enum[] enumArray = pca;
    }

}

which leads to a compiler error of:

Cannot implicitly convert type 'PrimaryColor[]' to 'System.Enum[]'

I could use linq or some more iterative process, but I wonder if there is a better cast I could use instead.

  • 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-18T04:21:37+00:00Added an answer on June 18, 2026 at 4:21 am

    You can do it iteratively only

    Enum[] enumArray = Array.ConvertAll(pca, item => (Enum)item);
    

    Or (less efficient but Linq!)

    Enum[] enumArray = pca.Cast<Enum>().ToArray();
    

    Why you can’t simply cast arrays? Because in C# covariance enabled only for arrays of reference types (enums are value types). So, with class Foo you can do:

    Foo[] foos = new Foo[10];
    object[] array = (object[])foos;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can i convert this into an array? if someone searches for lo he
How can I convert this array ['h', 'b'] into this array [['Home', 'h'], ['Business',
Can we convert a byte array into an InputStream in Java? I have been
How can I convert an array of bytes into a UTF-8 string? I need
How can i convert an array of bitmaps into a brand new image of
How can I convert an array to a SimpleXML object in PHP?
How can i convert an array like Array ( [0] => A [1] =>
How can i convert this JSON array: This is a serialized JSON array 2012-06-18
How can I convert the char array x to an integer 89 in the
How can I convert the following array: (Assume I don't know how many depth

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.