
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
- Related Articles
- How to convert a list of lists into a single list in R?
- How to clone a generic list in C#?
- How to deserialize a JSON array to list generic type in Java?\n
- What is a generic List in C#?
- Program to count number of consecutive lists whose sum is n in C++
- How to get length of a list of lists in Python?
- How to join list of lists in python?
- How to serialize and de-serialize generic types using the Gson library in Java?\n
- How do make a flat list out of list of lists in Python?
- Different types of operators in C++
- How to find the index of an item in a C# list in a single step?
- What are different types of parameters to a method in C#?
- How to sort a list of complex types using Comparison delegate in C#?
- Convert list into list of lists in Python
- How to count the number of items in a C# list?

Advertisements