CSharp - LINQ ToList

Introduction

The ToList operator creates a List of type T from an input sequence of type T.

Prototypes

public static List<T> ToList<T>(
  this IEnumerable<T> source);

This operator takes an input sequence named source, of type T elements, and returns a List of type T elements.

Exceptions

ArgumentNullException is thrown if the source argument is null.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from  w  ww .ja  v  a  2 s .  co m*/
{
    static void Main(string[] args)
    {
         string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
    
         List<string> names = codeNames.ToList();
    
         foreach (string name in names)
           Console.WriteLine(name);
    }
}

Result

In the code above, we use the array from the previous example.