c# - How to iterate through a Dictionary using for loop

Dictionary for loop
dictionary-for-loop.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,"Himalayan Monal"},  
            {2,"Great Argus"},  
            {3,"Red Junglefowl"},  
            {4,"Green Junglefowl"},  
            {5,"Ceylon Junglefowl"}  
        };  
  
        Label1.Text = "dictionary elements using for loop..........";  

        //get dictionary elements using for loop
        for(int i=0;i<birds.Count;i++)  
        {  
            //get dictionary element key using index value
            Label1.Text += "<br />" + birds.ElementAt(i).Key;

            //get dictionary element value using index position
            Label1.Text += "......." + birds.ElementAt(i).Value;  
        }  
    }      
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - dictionary for loop</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - dictionary for loop
        </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 for loop"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>
More c# examples