c# - How to convert Dictionary values to list

Convert dictionary values to list
The Dictionary class represents a collection of keys and values. The .net framework’s Dictionary is located under the System.Collections.Generic namespace. The Dictionary object constructor is Dictionary<TKey,TValue>. The TKey is the data type of the keys in the Dictionary and the TValue is the data type of the values in the Dictionary. We can initialize an empty Dictionary instance and add elements to it using its Add() method.

The following .net c# tutorial code demonstrates how we can convert Dictionary values to a list. Dictionary keys are unique but the values can be duplicated.

The Enumerable ToList() method creates a List<T> from an IEnumerable<T>. The ToList() method returns a List<T> that contains elements from the input sequence. The Enumerable ToList() method throws ArgumentNullException if the source is null.

The Enumerable ToList() method is located in System.Linq namespace. The ToList() method forces immediate query evaluation and returns a List<T> that contains the query results.
dictionary-to-list-of-values.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a dictionary with keys and values.
        Dictionary<int, string> birds = new Dictionary<int, string>() {
            {1,"Spotted Antbird"},
            {2,"Ocellated Antbird"},
            {3,"Squamate Antbird"},
            {4,"Barred Antshrike"},
            {5,"Great Antshrike"}
        };

        Label1.Text = "dictionary keys and values..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }

        //this line create a generic list by dictionary values.
        List<string> valuesList = birds.Values.ToList();

        Label1.Text += "<br /><br />list elements..........";
        foreach (string bird in valuesList)
        {
            Label1.Text += "<br />" + bird;
        }
    }    
</script>    

<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - dictionary to list of values</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - dictionary to list of values
        </h2>    
        <hr width="550" align="left" color="Gainsboro" />    
        <asp:Label     
            ID="Label1"     
            runat="server"    
            Font-Size="Large"  
            >    
        </asp:Label>    
        <br /><br />  
        <asp:Button     
            ID="Button1"     
            runat="server"     
            Text="dictionary to list of values"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>