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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:54:43+00:00 2026-05-26T06:54:43+00:00

It appears that, in .NET, array of enum is not a strongly-typed concept. MyEnum[]

  • 0

It appears that, in .NET, “array of enum” is not a strongly-typed concept. MyEnum[] is considered to implement not just IEnumerable<MyEnum>, but also IEnumerable<YourEnum>. (I didn’t believe it at first either.)

// Returns true:
typeof(IEnumerable<DayOfWeek>).IsAssignableFrom(typeof(AttributeTargets[]))

// Outputs "3":
var listOfLists = new List<object> {
    new[] { AttributeTargets.All },
    new[] { ConsoleColor.Blue },
    new[] { PlatformID.Xbox, PlatformID.MacOSX }
};
Console.WriteLine(listOfLists.OfType<IEnumerable<DayOfWeek>>().Count());

So when I go looking through a list for everything that implements IEnumerable<T>, I’m getting the T[]s and the List<T>s and the iterator-generated IEnumerable<T>s, but I’m also getting the SomethingElse[]s that I do not want.

What’s the easiest way to find out whether a given Type (as with IsAssignableFrom above) or a given instance (as with OfType<T> above) really and truly implements, say, IEnumerable<DayOfWeek>?


I gave Mr. Skeet the green checkmark, but once I got his code into Visual Studio, ReSharper suggested a more concise version. Use whichever version you prefer.

public static IEnumerable<IEnumerable<T>> OfSequenceType<T>
    (this IEnumerable source) where T : struct
{
    return from sequence in source.OfType<IEnumerable<T>>()
           let type = sequence.GetType()
           where !type.IsArray || type.GetElementType() == typeof (T)
           select sequence;
}
  • 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-26T06:54:44+00:00Added an answer on May 26, 2026 at 6:54 am

    I believe this is basically due to section 8.7 of ECMA 335.

    Basically we’re looking at the assignable-to relationship between the two array-of-enum types. As far as I can tell, 8.7.2 is applicable:

    A location type T is compatible-with a location type U if and only if one of the following holds.

    1. T and U are not managed pointer types and T is compatible-with U according to the definition in §8.7.1.

    So we look to 8.7.1 and find bullet 5:

    T is a zero-based rank-1 array V[], and U is a zero-based rank-1 array W[], and V is array-element-compatible-with W.

    So now we’re interested in whether the two enum types have an array-element-compatible-with relationship… That then leads to:

    A signature type T is array-element-compatible-with a signature type U if and only if T has underlying type V and U has underlying type W and either:

    1. V is compatible-with W; or
    2. V and W have the same reduced type.

    Now the underlying type of an enum is defined by this:

    The underlying type of a type T is the following:

    1. If T is an enumeration type, then its underlying type is the underlying type declared in the enumeration‘s definition.
    2. [Irrelevant to us]

    So in the case of, say:

    enum Foo : int {}
    enum Bar : int {}
    

    the underlying types of both are int.

    Now we can go back to our array-element-compatible-with definition, and see that both V and W are int. A type is compatible-with itself due to the first bullet of 8.7.1:

    A signature type T is compatible-with a signature type U if and only if at least one of the following holds.

    1. T is identical to U.

    Therefore, the arrays are compatible.

    They’re also compatible with an array of the underlying type itself:

    enum Foo {}
    ...
    object x = new int[10];
    Foo[] y = (Foo[]) x; // No exception
    

    Note that x has to be declared as object here to persuade the C# compiler that this might be legal – otherwise it follows the rules of the C# language, which aren’t quite so forgiving…


    Now as for your second question:

    What’s the easiest way to find out whether a given Type (as with IsAssignableFrom above) or a given instance (as with OfType above) really and truly implements, say, IEnumerable?

    You could just special case arrays, as they behave a little oddly. Frankly that’s probably the easiest approach. I tried using Type.GetInterfaceMap but that gives a problem too:

    Unhandled Exception: System.ArgumentException: Interface maps for generic interf
    aces on arrays cannot be retrived.

    (Yes, the typo at the end is really in the error message. Can’t be bothered to raise a Connect issue for that though…)

    I strongly suspect that special-casing is the way forward… for example, assuming that you know you’re dealing with a value type (covariance of reference type arrays is a separate matter…)

    public static IEnumerable<IEnumerable<T>> OfSequenceType<T>
        (this IEnumerable source) where T : struct
    {
        // Nullity check elided...
        foreach (object x in source)
        {
            IEnumerable<T> sequence = x as IEnumerable<T>;
            if (sequence == null)
            {
                continue;
            }
            // Work around odd value type array variance
            Type type = sequence.GetType();
            if (type.IsArray && type.GetElementType() != typeof(T))
            {
                continue;
            }
            yield return sequence;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an event that fires in vb.net just before a contextMenuStrip appears when
Is that .NET related? It appears to be a pointer of some sort, what
It appears that calling Html.RenderAction in Asp.Net MVC2 apps can alter the mime type
I've got an older assembly (Office XP PIA) that appears to target the .NET
Appears that WAS does not support the integration binding. I've tried setting it up
It appears that both jQuery and ASP.Net MVC have ways of performing AJAX functions.
It is clear that the T[] array type is not covariant as the elements
From some googling, it appears that .NET supports asynchronous operations with SQL Server 2005+.
It appears that using perldoc perl gives the list of, e.g. perlre, perlvar, etc.
It appears that in PHP objects are passed by reference. Even assignment operators do

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.