
- 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 copy or clone a C# list?
To copy or clone a C# list, firstly set a list −
List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); list1.Add("Three"); list1.Add("Four");
Now declare a string array and use the CopyTo() method to copy.
string[] arr = new string[20]; list1.CopyTo(arr);
Let us see the complete code to copy a list into a one-dimensional array.
Example
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); list1.Add("Three"); list1.Add("Four"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } string[] arr = new string[20]; list1.CopyTo(arr); Console.WriteLine("After copy..."); foreach(string value in arr) { Console.WriteLine(value); } } }
- Related Articles
- How to clone or copy a list in Python?
- How to clone or copy a list in Kotlin?
- C# program to clone or copy a list
- Python program to clone or copy a list.
- How to copy or clone a Java ArrayList?
- How to Clone a List in Java?
- How to clone a generic list in C#?
- How to copy a list to another list in Java?
- Python How to copy a nested list
- How to copy a List collection to an array?
- How to clone a GitHub repository?
- How do you copy a list in Java?
- How to Clone a Map in Java
- How to clone a canvas using FabricJS?
- How do I copy items from list to list without foreach in C#?

Advertisements