Home > ASP.NET > How to use dictionary key value pair in C#



How to use dictionary key value pair in C#

In this article, we have described how to use dictionary key-value pairs in C# with example code.


In this article , we have described how to use dictionary key value pair in C# with example code.

the dictionary contains a generic collection of key value pairs in C#.

dictionary is great feature in C# and extremely fast for filters where aggregate function is used having so many shotcut keys.

 

Required Namespaces

We must use the following namespaces.

using System.Collections.Generic;

using System.Linq;

Aspx Page

We have used the dictionary code below. You can have a look at it and learn.

<html>

<head runat="server">

    <title></title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>

<body>

    <form id="form1" runat="server">

        <div class="form-group">

            <label class="form-group">

                <h5>Find value based on Key</h5>

            </label>

            <asp:TextBox ID="txtDickey" runat="server" />

            <asp:Button Text="FindByKey" runat="server" OnClick="Key_Click" />

        </div>

        <div class="form-group">

            <label class="form-group">

                <h5>Find Key based on value </h5>

            </label>

            <asp:TextBox ID="txtdicValue" runat="server" />

            <asp:Button Text="FindByvalue" runat="server" OnClick="value_Click" />

        </div>

        <asp:Label ID="lblValueResult" Text="" runat="server" />

    </form>

</body>

</html>

C#

We have explained below C# code with examples of some user information in this article .You can have a look at it and learn.

private Dictionary<int, string> Dictionaryfun()

    {

        Dictionary<int, string> userData = new Dictionary<int, string>();

        userData.Add(1, "Rjboy");

        userData.Add(2, "abhi");

        userData.Add(3, "Peter");

        userData.Add(4, "John");

        return userData;

    }

 

    private object findbasedonkey(int id, Dictionary<int, string> Data)

    {

        return Data.Single(R => R.Key == id).Value;

    }

 

    private object findbasedonvalue(string value, Dictionary<int, string> Data)

    {

        return Data.Single(R => R.Value == value).Key;

    }

 

    protected void Key_Click(object sender, EventArgs e)

    {

        lblValueResult.Text = (this.findbasedonkey(Convert.ToInt32(txtDickey.Text.Trim()), Dictionaryfun())).ToString();

    }

 

    protected void value_Click(object sender, EventArgs e)

    {

        lblValueResult.Text = Convert.ToString(this.findbasedonvalue(txtdicValue.Text.Trim(), Dictionaryfun()));

    }




Note: All contents are copyright of their authors.



Comments



Leave a Reply

Comment:*
Email ID:*


Name:*