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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:44:47+00:00 2026-06-19T01:44:47+00:00

In a different question on stackoverflow somebody suggested to write an extension method for

  • 0

In a different question on stackoverflow somebody suggested to write an extension method for an array, but used the this IList<T> interface in the extension method. I commented it should be an array but he declined. I tested it, and of course, he’s right… 🙂

Extension method:

public static void Fill<T>(this IList<T> array, T value)
{
    for(var i = 0; i < array.Count; i++) 
    {
        array[i] = value;
    }
}

Test code:

[Test] 
public void Stackoverflow()
{
    int[] arr = new int[] { 1,2,3,4};
    arr.Fill(2);
    Assert.AreEqual(2, arr[0]);
    Assert.AreEqual(2, arr[1]);
    Assert.AreEqual(2, arr[2]);
    Assert.AreEqual(2, arr[3]);
}

An array is not an IList<T>. Why does this even compile? Let alone, pass?!

  • 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-19T01:44:48+00:00Added an answer on June 19, 2026 at 1:44 am

    From section 12.1.2 of the C# 4 specification:

    A one-dimensional array T[] implements the interface System.Collections.Generic.IList<T> and its base interfaces. Accordingly, there is an implicit conversion from T[] to IList<T> and its base interfaces.

    It’s worth noting that if you create a rectangular array or a one-dimensional array which has a lower bound other than zero, those do not implement IList<T>.

    In the CLI, there are actually two terms: vector and array. A vector is a one-dimensional collection with a lower bound of zero, and is optimized to heck and back by the JIT. An array can have multiple dimensions and different lower bounds, and isn’t subject to as thorough optimization.

    A T[] in C# is always a vector. You can’t cast an object which is a CLI-array to T[] – you end up with an exception like this:

    Unhandled Exception: System.InvalidCastException: Unable to cast object of type
    'System.String[*]' to type 'System.String[]'.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to another question (http://stackoverflow.com/questions/935556/mysql-dump-by-query) but I hope different enough. I want
This is similar to (but different from) this question . Here is some simple
Similar, but different to this question : I use highlight occurrences a lot, but
Well I guess this is not a normal how-to-do-this-or-that stackoverflow question, but perhaps someone
It is a question build upon the previous question (http://stackoverflow.com/questions/6538448/r-how-to-write-a-loop-to-get-a-matrix). It is different from
This is related to another StackOverflow question from a year ago. But, a bit
I saw this question (http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color) and that works fine for normal buttons. Now I
(This is technically an addendum to an earlier StackOverflow question I had posted, but
I have used Jim McCurdy's very helpful answer to this StackOverflow question to avoid
I understand that this isn't necessarily the best style question for stackoverflow but with

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.