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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:12:05+00:00 2026-05-14T00:12:05+00:00

One of the data structures in my current project requires that I store lists

  • 0

One of the data structures in my current project requires that I store lists of various types (String, int, float, etc.). I need to be able to dynamically store any number of lists without knowing what types they’ll be.

I tried storing each list as an object, but I ran into problems trying to cast back into the appropriate type (it kept recognizing everything as a List<String>).

For example:

List<object> myLists = new List<object>();

public static void Main(string args[])
{
    // Create some lists...

    // Populate the lists...

    // Add the lists to myLists...

    for (int i = 0; i < myLists.Count; i++)
    {
        Console.WriteLine("{0} elements in list {1}", GetNumElements(i), i);
    }
}

public int GetNumElements(int index)
{
    object o = myLists[index];

    if (o is List<int>)
        return (o as List<int>).Count;

    if (o is List<String>)                  // <-- Always true!?
        return (o as List<String>).Count;   // <-- Returning 0 for non-String Lists

    return -1;
}

Am I doing something incorrectly? Is there a better way to store a list of lists of various types, or is there a better way to determine if something is a list of a certain type?

  • 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-14T00:12:06+00:00Added an answer on May 14, 2026 at 12:12 am

    The type List<T> inherits from the non-generic interface IList. Since all of the values in the myList type are bound intsance of List<T> you can use them as the non-generic IList. So you can use that to greatly simplify your logic

    public int GetNumElements(int index) {
      object o = myLists[index];
      var l = o as IList;
      return l.Count;
    }
    

    Or alternatively since you know that all of the values stored in myList will be a bound version of List<T>, use IList as the type instead of object.

    List<IList> myLists = new List<IList>();
    ...
    public int GetNumElements(int index) {
      return myList[index].Count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just finished the main part of the current data structures project, and am
For some current projects, I'm working with several data structures that are pretty large
One of the basic data structures in Python is the dictionary, which allows one
HashTables/HashMaps are one of the most (if not the most) useful of data-structures in
I want to store a data structure thats a table with one column Strings
How does one read a data file in an iPhone project? For example, lets
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
On JSON.org the essential data structures that JSON represents are given as A collection
I have a set of data in one JSON structure: [[task1, 10, 99], [task2,
I am starting to migrate a custom serialization mechanism to Protocol Buffers. One data

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.