How to have a list with different data types in it in the form of lists?

I want the method to return a list that contains two more lists that have two different data types, for example:

List<List<object>> parentList = new List<List<object>>(); List<string> childList1 = new List<string>(); List<DataRow> childList2 = new List<DataRow>(); parentList.Add(childList1); parentList.Add(childList2); return parentList; 

According to the code above, I get an error

The best overloaded method match for 'System.Collections.Generic.List> .Add (System.Collections.Generic.List)' has some invalid arguments

Please can someone suggest me a better approach to handle this.

thanks

+5
source share
2 answers

How to create an object of your class like this?

  public class myParent { public List<string> childList1 = new List<string>(); public List<DataRow> childList2 = new List<DataRow>(); } public void someFun() { List<myParent> parentList = new List<myParent>(); myParent myParentObject = new myParent(); myParentObject.childList1 = new List<string>() { "sample" }; myParentObject.childList2 = new List<DataRow>() { }; parentList.Add(myParentObject); } 
+3
source

I'm not sure why you want to mix objects like this, but you can use an ArrayList for this. Example below:

  List<ArrayList> data = new List<ArrayList>(); data.Add(new ArrayList(){12, "12"}); //Added number and string in ArrayList data.Add(new ArrayList() {"12", new object() }); //Added string and object in ArrayList 

Update

In your case, using a list of arrays as shown below might be better

 var data = new ArrayList(); data.Add(new List<object>()); data.Add(new List<string>()); data.Add(new List<int>()); 
+2
source

Source: https://habr.com/ru/post/1239464/


All Articles