Dictionary to list
dictionary-to-list.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//create a new dictionary object.
Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(1, "Red Birch");
dictionary.Add(2, "River Birch");
dictionary.Add(3, "Silver Birch");
dictionary.Add(4, "Spice Birch");
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<int, string> p in dictionary)
{
Label1.Text += "<br />" + p.Key + " ------- " + p.Value;
}
//convert dictionary to generic list
List<KeyValuePair<int, string>> plants = dictionary.ToList();
Label1.Text += "<br /><br />generic list elements..........";
foreach (KeyValuePair<int, string> pair in plants)
{
Label1.Text += "<br />" + pair.Key + " ------- " + pair.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary to list</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary to list
</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"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to use KeyValuePair in a Dictionary
- Best way to iterate over a Dictionary
- How to sort a Dictionary by key and value
- How to sort a Dictionary by value in descending order
- How to sort a Dictionary in descending order
- How to remove items from a Dictionary while iterating
- How to remove items from a Dictionary in loop
- How to remove range of items from a Dictionary
- How to get a range of items from a Dictionary
- How to remove items from a Dictionary by condition