Let's say I have such lists :
var firstList = new List<ofsometype>();
var secondList = new List<ofsomeanothertype>();
var thirdList = new List<anothertype>();
How can I make a list that accepts those lists? Like
var mainList = new List<???>();
mainList.Add(firstlist);
mainList.Add(secondlist);
mainList.Add(thirdlist);
Thanks.
I'd probably use a Dictionary collection instead :
var firstList = new List<ofsometype>();
var secondList = new List<ofsomeanothertype>();
var thirdlist = new List<anothertype>();
var listsDict = new Dictionary<Type, object>();
listsDict.Add(typeof(ofsometype), firstlist);
listsDict.Add(typeof(ofsomeanothertype), secondlist);
listsDict.Add(typeof(anothertype), thirdlist);
The advantage here is that it gives you the information regarding the type of a list. This could be used for two things :
List<object>
later by simply using the keyP.S.
Depending on what the solution is and what you'd need to achieve you can use generics (if type is known) or dynamic
s - if type is unknown, but still a dynamic operation at run-time is required if compiler doesn't know the type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With