- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to store n number of lists of different types in a single generic list in C#?
We can store n number of lists of different types in a single generic list by creating a list of list of objects as shown below.
List<List<object>> list = new List<List<object>>();
Example
using System; using System.Collections.Generic; namespace MyApplication{ public class Program{ public static void Main(){ List<List<object>> list = new List<List<object>>(); List<object> list1 = new List<object>(); list1.Add(101); list1.Add(102); list1.Add(103); list.Add(list1); List<object> list2 = new List<object>(); list2.Add("Test1"); list2.Add("Test2"); list2.Add("Test3"); list.Add(list2); foreach (List<object> objectList in list){ foreach (object obj in objectList){ Console.WriteLine(obj); } Console.WriteLine(); } } } }
Output
The output of the above code is as follows.
101 102 103 Test1 Test2 Test3
Advertisements
To Continue Learning Please Login
Login with Google