Dictionary to list of values
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>
- How to use KeyValuePair in a Dictionary
- How to sort a Dictionary in ascending and descending order
- How to sort a Dictionary by DateTime value
- How to remove first element from Dictionary
- How to remove items from Dictionary in foreach loop
- How to remove range of items from a Dictionary
- How to get a range of items from a Dictionary
- How to convert Dictionary keys to list
- How to access Dictionary items by index
- How to get index of an item by key from a Dictionary