Copy the entire LinkedList to Array in C#

CsharpServer Side ProgrammingProgramming

To copy the entire LinkedList to Array, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      int[] strArr = new int[5];
      list.CopyTo(strArr, 0);
      foreach(int str in strArr){
         Console.WriteLine(str);
      }
   }
}

Output

This will produce the following output −

100
200
300
0
0

Example

Let us now see another example −

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      int[] strArr = new int[10];
      list.CopyTo(strArr, 4);
      foreach(int str in strArr){
         Console.WriteLine(str);
      }
   }
}

Output

This will produce the following output −

0
0
0
0
100
200
300
0
0
0
raja
Published on 10-Dec-2019 15:00:04
Advertisements