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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:51:43+00:00 2026-05-24T01:51:43+00:00

I noticed something strange and there is a possibility I am wrong. I have

  • 0

I noticed something strange and there is a possibility I am wrong.
I have an interface IA and class A:

interface IA { .... }
class A : IA { .... }

In other class I have this:

private IList<A> AList;

public IList<IA> {
    get { return AList; }
}

But I get compilation error.
But if I change it to:

public IList<IA> {
    get { return AList.ToArray(); }
}

Everything is fine.

Why is it?

  • 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-24T01:51:45+00:00Added an answer on May 24, 2026 at 1:51 am

    Why this doesn’t work

    private IList<A> AList;  
    public IList<IA> { get { return AList; } } 
    

    Exposing the property as IList<IA> would allow you to try to add class B : IA to the list, but the underlying list is really IList<A>, B is not A, so this would blow up in your face. Thus, it is not allowed.

    Why this works:

    public IList<IA> { get { return AList.ToArray(); } } 
    

    Array variance is broken. You can return the list as an array, it will still blow up in your face at runtime if you tried an Add operation (or try to replace an object at a given index with something other than an object of type A, but it’s legal at compile time. A different example of this variance at play:

    string[] array = new string[10];
    object[] objs = array; // legal
    objs[0] = new Foo(); // will bite you at runtime 
    

    From comments:

    So what you suggest to use? How can I make the property return valid
    object? How can I make the return value read only?

    If consumers only need to iterate over the sequence and not have random, indexed access to it, you can expose the property as an IEnumerable<IA>.

    public IEnumerable<IA> TheList
    {
         get { return AList.Select(a => a); }
    }
    

    (The Select is actually not technically needed, but using this will prevent consumers from being able to cast the result to its true underlying List<> type.) If the consumers decide they want a list or an array, they are free to call ToList() or ToArray() on it, and whatever they do with it (in terms of adding, removing, replacing items) will not affect your list. (Changes to the items’ properties would be visible.) Similarly, you could also expose the collection an IList<IA> yourself in a safe way

    public IList<IA> TheList
    {
        get { return AList.ToList<IA>(); }
    }
    

    Again, this would return a copy of the list, so any changes to it would not affect your underlying list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Converting my current code project to TDD, I've noticed something. class Foo { public
I have created a type like this: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttributes.Public,
I am using SQL Server 2005 and I have noticed something strange when I
I have been doing spring cleaning in my app. I noticed something strange, that,
Possible Duplicate: Puzzling Enumerable.Cast InvalidCastException Hi, I just noticed something quite strange with the
I noticed something in Chrome the other day- I had opened Developer Tools, and
I've noticed something about my coding that is slightly undefined. Say we have a
For a long time now I have noticed something annoying when working on Web
Ok, this may be a dumb question but here goes. I noticed something the
I have this very strange issue with my MVC 2 project. Often times, I'll

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.