Demonstrate initializing a List <T> from an array - CSharp Collection

CSharp examples for Collection:List

Description

Demonstrate initializing a List <T> from an array

Demo Code

using System;//from w  w w. ja  v a  2 s . c  o  m
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      int[] numbers = { 1, 2, 3 };
      foreach (int i in numbers)
      {
         Console.WriteLine(i);
      }
      List<int> numList = new List<int>(numbers); // Initializing from an array, or...
      Console.WriteLine("Add the same array again via AddRange() method: ");
      // After this call, the list contains duplicate items - that's allowed.
      numList.AddRange(numbers);                  // Using AddRange.
      Console.WriteLine("Resulting list: ");
      foreach (int i in numList)
      {
         Console.WriteLine(i);
      }
   }
}

Result


Related Tutorials