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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:35:16+00:00 2026-06-06T06:35:16+00:00

So as you may know, arrays in C# implement IList<T> , among other interfaces.

  • 0

So as you may know, arrays in C# implement IList<T>, among other interfaces. Somehow though, they do this without publicly implementing the Count property of IList<T>! Arrays have only a Length property.

Is this a blatant example of C#/.NET breaking its own rules about the interface implementation or am I missing something?

  • 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-06T06:35:18+00:00Added an answer on June 6, 2026 at 6:35 am

    New answer in the light of Hans’s answer

    Thanks to the answer given by Hans, we can see the implementation is somewhat more complicated than we might think. Both the compiler and the CLR try very hard to give the impression that an array type implements IList<T> – but array variance makes this trickier. Contrary to the answer from Hans, the array types (single-dimensional, zero-based anyway) do implement the generic collections directly, because the type of any specific array isn’t System.Array – that’s just the base type of the array. If you ask an array type what interfaces it supports, it includes the generic types:

    foreach (var type in typeof(int[]).GetInterfaces())
    {
        Console.WriteLine(type);
    }
    

    Output:

    System.ICloneable
    System.Collections.IList
    System.Collections.ICollection
    System.Collections.IEnumerable
    System.Collections.IStructuralComparable
    System.Collections.IStructuralEquatable
    System.Collections.Generic.IList`1[System.Int32]
    System.Collections.Generic.ICollection`1[System.Int32]
    System.Collections.Generic.IEnumerable`1[System.Int32]
    

    For single-dimensional, zero-based arrays, as far as the language is concerned, the array really does implement IList<T> too. Section 12.1.2 of the C# specification says so. So whatever the underlying implementation does, the language has to behave as if the type of T[] implements IList<T> as with any other interface. From this perspective, the interface is implemented with some of the members being explicitly implemented (such as Count). That’s the best explanation at the language level for what’s going on.

    Note that this only holds for single-dimensional arrays (and zero-based arrays, not that C# as a language says anything about non-zero-based arrays). T[,] doesn’t implement IList<T>.

    From a CLR perspective, something funkier is going on. You can’t get the interface mapping for the generic interface types. For example:

    typeof(int[]).GetInterfaceMap(typeof(ICollection<int>))
    

    Gives an exception of:

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

    So why the weirdness? Well, I believe it’s really due to array covariance, which is a wart in the type system, IMO. Even though IList<T> is not covariant (and can’t be safely), array covariance allows this to work:

    string[] strings = { "a", "b", "c" };
    IList<object> objects = strings;
    

    … which makes it look like typeof(string[]) implements IList<object>, when it doesn’t really.

    The CLI spec (ECMA-335) partition 1, section 8.7.1, has this:

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

    …

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

    (It doesn’t actually mention ICollection<W> or IEnumerable<W> which I believe is a bug in the spec.)

    For non-variance, the CLI spec goes along with the language spec directly. From section 8.9.1 of partition 1:

    Additionally, a created vector with element type T, implements the interface System.Collections.Generic.IList<U>, where U := T. (§8.7)

    (A vector is a single-dimensional array with a zero base.)

    Now in terms of the implementation details, clearly the CLR is doing some funky mapping to keep the assignment compatibility here: when a string[] is asked for the implementation of ICollection<object>.Count, it can’t handle that in quite the normal way. Does this count as explicit interface implementation? I think it’s reasonable to treat it that way, as unless you ask for the interface mapping directly, it always behaves that way from a language perspective.

    What about ICollection.Count?

    So far I’ve talked about the generic interfaces, but then there’s the non-generic ICollection with its Count property. This time we can get the interface mapping, and in fact the interface is implemented directly by System.Array. The documentation for the ICollection.Count property implementation in Array states that it’s implemented with explicit interface implementation.

    If anyone can think of a way in which this kind of explicit interface implementation is different from “normal” explicit interface implementation, I’d be happy to look into it further.

    Old answer around explicit interface implementation

    Despite the above, which is more complicated because of the knowledge of arrays, you can still do something with the same visible effects through explicit interface implementation.

    Here’s a simple standalone example:

    public interface IFoo
    {
        void M1();
        void M2();
    }
    
    public class Foo : IFoo
    {
        // Explicit interface implementation
        void IFoo.M1() {}
    
        // Implicit interface implementation
        public void M2() {}
    }
    
    class Test    
    {
        static void Main()
        {
            Foo foo = new Foo();
    
            foo.M1(); // Compile-time failure
            foo.M2(); // Fine
    
            IFoo ifoo = foo;
            ifoo.M1(); // Fine
            ifoo.M2(); // Fine
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You may know this recommendation from Microsoft about the use of exceptions in .NET:
As you may know, when we have this code in Javascript : function getName()
You may know the scriptaculous SlideUp effect . Well, It slides up a div
As you may know, Silverlight 3 doesn't support IMultiValueConverter and... I badly need it.
As some of you may know, use of the LIMIT keyword in MySQL does
As some of you may know in python2.7/3.2 we'll get OrderedDict with PEP372 however
As you may know, in many occasions, there is a need to flag some
Some of you may know the method in Ruby that allows you to reverse
If you've used javadoc and then come to doxygen, you may know what I
I'm trying to implement swiftmailer into this mailing system. my client has about 300k

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.