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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:11:35+00:00 2026-05-25T17:11:35+00:00

Here is my problem: there is a class that contains a inner collection (or

  • 0

Here is my problem: there is a class that contains a inner collection (or list, or array, or something like this) of some some class and It must expose a public read-only collection of items, which are properties (or fields) of relative items in inner collection. For example:

//Inner collection consists of items of this class
class SomeClass
{
  public int _age;

  //This property is needed for exposing
  public string Age { get { return this._age.ToString(); } }
}

//Keeps inner collection and expose outer read-only collection
class AnotherClass
{
  private List<SomeClass> _innerList = new List<SomeClass> ();

  public ReadOnlyCollection<string> Ages 
  {
     get 
     {
       //How to implement what i need?
     }
  }
}

I know a simple way to do this by the use of a pair of inner lists, where the second keeps values of needed properties of first. Something like this:

//Inner collection consists of items of this class
class SomeClass
{
  public int _age;

  //This property is needed for exposing
  public string Age { get { return this._age.ToString(); } }
}

//Keeps inner collection and expose outer read-only collection
class AnotherClass
{
  private List<SomeClass> _innerList = new List<SomeClass> ();
  private List<string> _innerAgesList = new List<string> ();

  public ReadOnlyCollection<string> Ages 
  {
     get 
     {
       return this._innerAgesList.AsreadOnly();
     }
  }
}

But I dislike this overhead. May be there is some way to do what I want with exposing interfaces. Help me, please!

Hurra!
It seems that the best solution has been found. Due to the post of Groo

this problem found its almost universal answer. Here is It (we need to add two entity):

public interface IIndexable<T> : IEnumerable<T>
{
    T this[int index] { get; }
    int Count { get; }
}

class Indexer <Tsource, Ttarget> : IIndexable<Ttarget>
{
    private IList<Tsource> _source = null;
    private Func<Tsource, Ttarget> _func = null;

    public Indexer(IList<Tsource> list, Func<Tsource, Ttarget> projection)
    {
        this._source = list;
        this._func = projection;
    }

    public Ttarget this[int index] { get { return this._func(this._source[index]); } }

    public int Count { get { return _source.Count; } }

    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }

    public IEnumerator<Ttarget> GetEnumerator()
    { foreach (Tsource src in this._source) yield return this._func(src); }     
}

With them, our implementation looks like this:

//Inner collection consists of items of this class
class SomeClass
{
  public int _age;

  //This property is needed for exposing
  public string Age { get { return this._age.ToString(); } }
}

//Keeps inner collection and expose outer read-only collection
class AnotherClass
{
  private List<SomeClass> _innerList = new List<SomeClass> ();
  private Indexer<SomeClass, string> _indexer = null;

  public AnotherClass () 
  { this._indexer = new Indexer<SomeClass, string > (this._innerList, s => s.Age); }

  public IIndexable<string> Ages { get { return this._indexer; } }
}

Thank Groo and the rest who answered. Hope, this helps someone else.

  • 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-25T17:11:35+00:00Added an answer on May 25, 2026 at 5:11 pm

    The overhead is not so significant if you consider that ReadOnlyCollection is a wrapper around the list (i.e. it doesn’t create a copy of all the items).

    In other words, if your class looked like this:

    class AnotherClass
    {
        private ReadOnlyCollection<string> _readonlyList;
        public ReadOnlyCollection<string> ReadonlyList
        {
            get { return _readonlyList; }
        }
    
        private List<string> _list;
        public List<string> List
        {
            get { return _list; }
        }
    
        public AnotherClass()
        {
            _list = new List<string>();
            _readonlyList = new ReadOnlyCollection<string>(_list);
        }
    }
    

    Then any change to the List property is reflected in the ReadOnlyList property:

    class Program
    {
        static void Main(string[] args)
        {
            AnotherClass c = new AnotherClass();
    
            c.List.Add("aaa");
            Console.WriteLine(c.ReadonlyList[0]); // prints "aaa"
    
            c.List.Add("bbb");
            Console.WriteLine(c.ReadonlyList[1]); // prints "bbb"
    
            Console.Read();
        }
    }
    

    You may have issues with thread safety, but exposing IEnumerable is even worse for that matter.

    Personally, I use a custom IIndexable<T> interface with several handy wrapper classes and extension method that I use all over my code for immutable lists. It allows random access to list elements, and does not expose any methods for modification:

    public interface IIndexable<T> : IEnumerable<T>
    {
        T this[int index] { get; }
        int Length { get; }
    }
    

    It also allows neat LINQ-like extension methods like Skip, Take and similar, which have better performance compared to LINQ due to the indexing capability.

    In that case, you can implement a projection like this:

    public class ProjectionIndexable<Tsrc, Ttarget> : IIndexable<Ttarget>
    {
        public ProjectionIndexable
             (IIndexable<Tsrc> src, Func<Tsrc, Ttarget> projection)
        {
            _src = src;
            _projection = projection;
        }
    
        #region IIndexable<Ttarget> Members
    
        public Ttarget this[int index]
        {
            get { return _projection(_src[index]); }
        }
    
        public int Length
        {
            get { return _src.Length; }
        }
    
        #endregion
    
        #region IEnumerable<Ttarget> Members
    
        // create your own enumerator here
    
        #endregion
    }
    

    And use it like this:

    class AnotherClass
    {
        private IIndexable<string> _readonlyList;
        public IIndexable<string> ReadonlyList
        {
            get { return _readonlyList; }
        }
    
        private List<SomeClass> _list;
        public List<SomeClass> List
        {
            get { return _list; }
        }
    
        public AnotherClass()
        {
            _list = new List<SomeClass>();
            _readonlyList = new ProjectionIndexable<SomeClass, string>
                 (_list.AsIndexable(), c => c.Age);
        }
    }
    

    [Edit]

    In the meantime, I posted an article describing such a collection on CodeProject. I saw you’ve implemented it yourself already, but you can check it out nevertheless and reuse parts of the code where you see fit.

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

Sidebar

Related Questions

Here's my problem: there's an internal issue tracking system that has a nice summary
Here is the problem I'm running into. There's a huge legacy application that runs
I have a C++ class that contains a non-copyable handle. The class, however, must
I have created a spring bean that contains a list of other beans, like
here is the problem : there is classic asp app which is calling lame.exe
I'm not really sure if there is a single problem here or I have
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
Hi guys I'm using this wonderful class here to do a bit of code
Assuming I have a schema that describes a root element class Root that contains
.Net framework contains a great class named Convert that allows conversion between simple types,

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.