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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:39:08+00:00 2026-05-13T05:39:08+00:00

I have a DataSource in my control which is always a List<T> where T

  • 0

I have a DataSource in my control which is always a List<T> where T has to inherit from IEntity.

public class MyClass<T> where T : IEntity
{
    public List<T> DataSource
    {
        get;
        set;
    }
}

Now, obviously you can’t cast a List<T> to a List<IEntity> doing the following:

List<IEntity> wontWork = (List<IEntity>)this.DataSource;

How can I get the DataSource as a List of IEntity, whilst still being able to add and remove items from the DataSource? I.e. I could do the following, but removing from the List it returns would not remove from the DataSource:

public List<TOut> GetDataSourceCopyAsUnderlyingType<TOut>()
{

    if (this.DataSource == null)
    {
        return new List<TOut>();
    }
    else
    {

        // Get the list and the enumerator
        IList list = (IList)this.DataSource;
        IEnumerator enumerator = list.GetEnumerator();

        // Build the target list
        List<TOut> targetList = new List<TOut>();

        int i = 0;
        while (enumerator.MoveNext())
        {
            TOut entity = (TOut)list[i];
            targetList.Add(entity);
            i++;
        }

        return targetList;

        }

    }

Basically, I need some way of doing the following:

List<IEntity> interfaceList = this.GetDataSourceAsAnotherType<IEntity>();
int dataSourceCount = this.DataSource.Count;   // Equals 5
int interfaceCount = interfaceList.Count;      // Equals 5

interfaceList.RemoveAt(0);
int dataSourceCount = this.DataSource.Count;   // Equals 4
int interfaceCount = interfaceList.Count;      // Equals 4

And just to add, I don’t mind if it means I’ve got to use a different type instead of a List.

EDIT: Sorry, forgot to say I’m using .Net2.0 and cannot move to .Net 3.5.

  • 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-13T05:39:08+00:00Added an answer on May 13, 2026 at 5:39 am

    It would be a monumentally bad idea if this were allowed, which is why it isn’t. I can add any old IEntity to a List<IEntity> which will blow up if that IEntity can’t be cast to T. Whilst all Ts are IEntities, not all IEntities are Ts.

    This works with arrays because arrays have a deliberate subtyping hole (as they do in Java). Collections do not have a subtyping hole.

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

Sidebar

Related Questions

I have a listview which has its datasource changed after update of a search
I have a DataGridView to which I've set a list of objects to the
I have a user control which has a number of properties. This user control
I have an unbound XtraReport that has a subreport control which contains another report.
I have an asp:FormView control bound to a datasource. Everything is working fine. If
I currently have a datasource from a client in the form of XML, this
I have a Form with a DataGridView which DataSource is a BindingSource to a
I have a usercontrol which has a combobox inside it. This UserControl is the
I have a template A which has several sublayouts and web controls assigned to
I have created a web user control called Activity. I have defined a public-facing

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.