c# - How to convert a Dictionary to a string

Dictionary to string
dictionary-to-string.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,"White Stork"},  
            {2,"Jabiru"},  
            {3,"Marabou Stork"},  
            {4,"Scarlet Ibis"}  
        };

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

        //create a stringbuilder.
        StringBuilder sb = new StringBuilder();
        
        //append dictionary key value to stringbuilder.
        foreach (KeyValuePair<int, string> pair in birds)
        {
            sb.Append(pair.Key);
            sb.Append("[");
            sb.Append(pair.Value);
            sb.Append("]");
            sb.Append(",");
        }

        Label1.Text += "<br /><br />dictionary elements to string.....<br />";
        Label1.Text += sb.ToString().TrimEnd(',');
    }      
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - dictionary to string</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - dictionary to string
        </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 string"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>
More c# examples