×

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# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

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# 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# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 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.

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# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

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.

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# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

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...

3 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.