×

Dictionary in C#

C #:

We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework.

C # is related to other widely used languages like C++ and Java and has roots in the C family. In 2002, the initial version was made available. In September 2019, C # 8 is one of the most recent version which was made available.

C # programming language is among the most widely used programming languages worldwide. It is simple to use and simple to learn. Huge community support exists for it. C # programming language  is an object-oriented language that provides programmers with a clear structure and enables code reuse, which lowers development costs.

Because C # is so similar to C, C++, and Java, programmers can easily switch from one to the other. One of the most popular languages for developing system backends is C # programming language. It is as a result of its amazing features, including Windows server automation.

Dictionary in C #:

A generic collection in C# called Dictionary is typically used to store key/value pairs. Dictionary functions quite similarly to non-generic hashtables. The fact that Dictionary is generic in nature is an advantage.

The definition of dictionary can also be defined as  system . collection . generic namespace. Because of its dynamic nature, the dictionary's size expands to meet demand.

The Dictionary class implements the IDictionary < TKey , TValue > Interface and the IReadOnlyCollection  < KeyValuePair TKey , TValue >> interface. Interface IReadOnlyDictionary < TKey , TValue > a dictionary of interface terms

The value in a dictionary can be null, but not the key. A dictionary requires a unique key. If you try to utilise a duplicate key, the compiler will throw an exception because it is not permitted.

You can only store the same types of components in a dictionary. The number of elements that a dictionary can store is referred to as its capacity.

Creating Dictionary :

We know that the dictionary class has seven constructors, we only use the dictionary TKey , TValue ( ) function Object ( ) here.

Dictionary TKey , TValue > ( ) :

Using this function Object ( ) a dictionary TKey , TValue > instance is created that is empty and has the default initial capacity , and employs the default equality comparer for the key type.

Syntax to define a dictionary :

Dictionary < data type 1 , data type 2 > variable = new Dictionary < data type 1 , data type 2 > ( ) ;

    We can access the elements in a dictionary in three ways. They are:

  1. Using foreach loop
  2. Using for loop
  3. Using index

Now let us see few examples using the following code as show below :

In this example we can see accessing of elements using for each loop.

Example 1 :

using System;  
using System . Collections . Generic ;  
public class Demo
{
  public static void Main ( string args [ ] )
{ 
Dictionary < string , string > names = new Dictionary < string , string > ( ) ;
names . Add ( “ 1A “ , “ Om “ );
names . Add ( “ 1B “ , “ Sai “ );
names . Add ( “ 1C “ , “ Ram “ );
names . Add ( “ 1D “ , “ Kiran “ );
names . Add ( “ 1E “ , “ Latha “ );
foreach ( KeyValuePair < string , string > kv in names)  
{
Console . WriteLine ( kv . Key + “ “ + kv . Value ) ; 
}
}
} 

Output 1:

1A Om
1B Sai
1C Ram
1D Kiran
1E Latha

In this example we can see accessing of elements using for loop and also indexing.

Example 2:

using System;
using System . Linq;
using System . Collections . Generic;
public class Demo2
{
public static void PrintDict < K , V > ( Dictionary < K , V > dict )
{
for ( int i = 0 ; i < dict . Count ; i++ ) 
{
KeyValuePair < K , V > name = dict . ElementAt ( i );
Console . WriteLine ( name . Key + “ : “ + name . Value ) ;
}
}
public static void Main ( )
{
Dictionary < string , string > dict = new Dictionary < string , string >
{
      { “ Key 1 “ , “ val1 “ } , { “ Key 2 “ , “ val2 “ }
};
PrintDict ( dict );
}
}

Output 2:

Key 1 : val 1
Key 2 : val 2

Related Topics

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.