开发者

List of Lists of different types

开发者 https://www.devze.com 2022-12-23 06:31 出处:网络
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

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 a开发者_如何学Gorgs[])
{
    // 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?


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;
}


I realise this is an oldy, but .NET 4 gave us another way around this. What if you wanted a List where X is another List, and y is any type at all?

In other words, a List> you might try object, but then you'd forever be trying to cast something back to it's original type, as above.

So, this is a rare scenario where the new dynamic data type can be used. Of course, it allows you to break type safety, but in this case, as we're always going to be dealing with a list of some sort, it's fair to say it's going to have a ".Count" property.

So something like :

var myMainList = new List<List<dynamic>>();
var myInnerList1 = new List<string> { "A", "B", "C" };
var myInnerList2 = new List<int> { 1, 2, 3, 4, 5 };
myMainList.Add(myInnerList1);
myMainList.Add(myInnerList2);

// when we come to try and read the number of values in the list..
Console.WriteLine("inner list 1 : {0}", myMainList[0].Count);
Console.WriteLine("inner list 2 : {0}", myMainList[1].Count);

So using dynamic allows us to drop the type safety checks. Many developers are against the use of dynamic. I actually decided to actively look for something regarding a list of lists of different types, and came here. So I added my own solution to my own problem. it's actually being used as a list of test data, where an outer list os a list of parameter names, and a collection 9inner list) of values to use in testing. These are different per parameter, and I didn't want to constantly cast back from object to types I know of during source code creation. I found this to be a convenient solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消