Skip to main content

C# Dictionary

> In this post, we will see how to use a dictionary in C#.
> A dictionary in C# is used to store collection of values with key values as pair.
> So whenever user wants to get the value then he will have to pass the key value to the dictionary and it will return the value to the user.
> The namespace of the C# dictionary is System.Collections.Generic.
> Let’s take a simple example of storing digits from 0 to 9 as key value and these digits in the word form as the collection values.
> Here, the user will type a digit from 0 to 9 and on clicking the button, it gives the digit in the word form.
> To keep this example simple, here the dictionary will be having digits from 0 to 9 only and if user enters a value less than 0 or greater than 9 then he will get the appropriate message.
> Following is the code for demonstrating the use of the Dictionary:


HTML Code:

<body>
   <form id="form1" runat="server">
       <div>
           Enter a number:
           <asp:TextBox ID="txtDigit" runat="server"></asp:TextBox>
           <asp:Button ID="btnGet" runat="server" Text="Get" OnClick="btnGet_Click" />
       </div>
       <br />
       <div id="output" runat="server"></div>
   </form>
</body>

C# code:

//method to get the digit in the word form using the C# dictionary
   protected string GetDigitInWord(int digit)
   {
       string word="Enter a single digit from 0 to 9 only";
      
       //declaring the dictionary
       Dictionary<int, string> dict = new Dictionary<int, string>();
       if (digit >= 0 && digit <= 9)
       {
           //adding values to dictionary with key
           dict.Add(0, "Zero");
           dict.Add(1, "One");
           dict.Add(2, "Two");
           dict.Add(3, "Three");
           dict.Add(4, "Four");
           dict.Add(5, "Five");
           dict.Add(6, "Six");
           dict.Add(7, "Seven");
           dict.Add(8, "Eight");
           dict.Add(9, "Nine");

           //getting digit in word by passing digit as key value
           word = dict[digit];
       }
       
       return word;
   }

protected void btnGet_Click(object sender, EventArgs e)
   {
       int digit = Convert.ToInt32(txtDigit.Text);
       //calling above method by passing the entered digit and getting its word form
       output.InnerText= this.GetDigitInWord(digit);
   }

Output:

Output 1: Entering digit greater than 9 and getting message.


Output 2: Entering 3 as value and getting its word form:

That's it....

Comments

Popular posts from this blog

datatable compute sum in c# | datatable compute() function

datatable compute sum in c# | datatable compute() function Hi Readers, Today I am going to explain you the use of compute() function of Data table using C#.NET .  Let's begin.. datatable column sum in c# datatable compute sum in c# | datatable compute() function Let's say that we have the following rows filled in the Data table with the columns ID,NAME,AGE and SALARY as shown in snapshot below:- employees detail in datatable In order to calculate the figures like total of salary, average of salary, maximum salary and minimum salary, we can use the compute() function of the Data table. The compute() function loops through each value of the column's cell and then perform the calculation. For the above data table's salary column, lets find the total, average, maximum and minimum of salary one by one using compute() function:- datatable compute sum in c# | datatable compute() function Calculate the total of sala

Adding rows to Datatable using C#.NET

Adding rows to Datatable using C#.NET Hi Readers, Today I am going to explain you 2 ways that we can use to add rows to a Data table using C#.NET.  So lets begin: Add row in datatable in c# Adding rows to Datatable using C#.NET > Lets say, we have a data table with the columns ID, NAME, AGE AND SALARY and I want to add rows to this data table using C# coding . > First of all, add columns to data table using the following lines of code: DataTable dtEmployee = new DataTable("Employee"); dtEmployee.Columns.Add("ID", typeof(int)); dtEmployee.Columns.Add("NAME", typeof(String)); dtEmployee.Columns.Add("AGE", typeof(int)); dtEmployee.Columns.Add("SALARY", typeof(Decimal)); Add row in datatable in c# > Now if we want to add rows to the above data table at run time, we have the following 2 ways to do so: Adding rows to Datatable using C#.NET 1. Using DataRows :- In this, we create an o

Project available: IPMS

Following project is ready available for academic purpose and business requirements: In-Patient Management System (IPMS): Objective: This project fulfills the requirements of a clinic or hospital to manage in-patient activities i.e. patients who are admitted to the hospital. The objective of “ In-Patient Management ” is to provide such a system that can make the in-patient process automated.  The system will help the users for the following operations:- Ø   The patients’ detail, their medical history information, tests reports that have been done during their stay in the hospital. Ø    The system will even help the users to keep details for medical prescriptions given to the patients. Ø   The system will be able to keep the staff detail (doctors and nurses) detail, their schedule of work etc. Ø   The system will help the nursing staff to allot bed to the patient after having look on the current booking status of  bed i.e. whether it is being booked for o