- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 clone a generic list in C#?
A list is a Generic collection to hold elements of same datatypes.
To clone a list, you can use the CopyTo method.
Declare a list and add elements −
List < string > myList = new List < string > (); myList.Add("Programming"); myList.Add("Web Dev"); myList.Add("Database");
Now create a new array and clone the list into it −
string[] arr = new string[10]; myList.CopyTo(arr);
Here is the complete code −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { List < string > myList = new List < string > (); myList.Add("Programming"); myList.Add("Web Dev"); myList.Add("Database"); Console.WriteLine("First list..."); foreach(string value in myList) { Console.WriteLine(value); } string[] arr = new string[10]; myList.CopyTo(arr); Console.WriteLine("After cloning..."); foreach(string value in arr) { Console.WriteLine(value); } } }
Output
First list... Programming Web Dev Database After cloning... Programming Web Dev Database
- Related Articles
- How to copy or clone a C# list?
- How to Clone a List in Java?
- What is a generic List in C#?
- C# program to clone or copy a list
- How to clone or copy a list in Python?
- How to clone or copy a list in Kotlin?
- How to deserialize a JSON array to list generic type in Java?\n
- How to store n number of lists of different types in a single generic list in C#?
- Python program to clone or copy a list.
- Convert array to generic list with Java Reflections
- How to Clone a Map in Java
- How to create a generic array in java?
- How to clone a GitHub repository?
- Generic keyword in C ?
- How to clone a Date object in JavaScript?

Advertisements