c# - How to convert a string array to a list

Convert a String Array to a List
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can convert a String Array to a List. That means we create a String data type List from String Array elements. But there is no direct method in the Array class to convert a String Array to a String data type List. In this .net c# example code, we will use the Enumerable class OfType() and ToList() methods to create a String type List from a String Array instance.

In the beginning, we will call the Enumerable class OfType() method to filter the String type elements from the String Array instance. After getting the filtered sequence we call the Enumerable class ToList() method to create a String data type List object. So, finally, this will convert the String Array to a String type List instance.

The Enumerable OfType() method filters the elements of an IEnumerable based on a specified type. This method returns an IEnumerable<T> that contains elements from the input sequence of type TResult. The OfType() method throws ArgumentNullException if the source Array is null.

The Enumerable ToList() method creates a List<T> from an IEnumerable<T>. This method returns a List<T> that contains elements from the input sequence. The ToList() method throws ArgumentNullException if the source sequence is null.
convert-string-array-to-list.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string[] birds = new string[] { "Snail Kite", "Black Kite", "Red Kite", "Pied Harrier" };

        List<string> birdslist = birds.OfType<string>().ToList();
        
        Label1.Text = "birds array:  ";
        foreach(string s in birds)
        {
            Label1.Text += s + " | ";
        }

        Label1.Text += "<br /><br />birds list:  ";
        foreach (string s in birdslist)
        {
            Label1.Text += s + " | ";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - convert string array to list</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - convert string array to list
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="convert string array to list"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples