C# program to convert an array to an ordinary list with the same items


Set an array −

int[] arr = { 23, 66, 96, 110 };

Now, create a new list −

var list = new List<int>();

Use the Add method and add the array elements to the list −

for (int i = 0; i < arr.Length; i++) {
   list.Add(arr[i]);
}

The following is the complete code −

Example

 Live Demo

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      int[] arr = { 23, 66, 96, 110 };
      var list = new List<int>();

      for (int i = 0; i < arr.Length; i++) {
         list.Add(arr[i]);
      }
      foreach(int res in list) {
         Console.WriteLine(res);
      }
   }
}

Output

23
66
96
110

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements