C# Tutorial

C# Tutorial C# First Application C# Variables C# Data Types C# Operators C# Keywords

C# Control Statement

C# If Statements C# Switch Statements C# for Loop C# While Loop C# do While loop C# Jump Statements C# Function C# functions with out variable

C# Arrays

C# Arrays

C# Function

C# Function call by value C# Call by reference C# Passing array to function C# Multidimensional Arrays C# Jagged Arrays C# Params C# Array Class C# Command Line Arguments

C# Object Class

C# Object and Classes C# Constructors C# Destructor C# this Keyword C# static field C# static class C# Static Constructor C# Structs C# enum C# Properties

C# Inheritance

C# Inheritance C# Multilevel Inheritance C# Aggregation C# Member overloading C# Method Overriding C# Base

C# Polymorphism

C# Polymorphism C# Sealed

C# Abstraction

C# Abstraction C# Interface

C# Namespace

C# Namespace C# Access Modifiers C# Encapsulation

C# Strings

C# String

C# Misc

C# Design Patterns Dictionary in C# Boxing and Unboxing in C# Ref and Out in C# Serialization in C# The Foreach Loop in C# Exception in catch block C# C# Framework CLR in C# Garbage Collection in C# Collections in C# OOPS Concept in C# Palindrome in C# Abstract class Example in C# Linq Query in C# Sealed Class in C# What is the Net Framework in C# C# Hash Table With Examples Can We Create An Object For Abstract Class in C# Setting The Location Of The Label in C# Reverse Of String in C# Sorteddictionary Implementation in C# String And Stringbuilder in C# Virtual Keyword in C# Delegates And Events In C# C# Encription C# Error Logging Const and Readonly Variables in C# Difference Between Ref and Out in C# Singleton Class in C# Asqueryable in C# Filter Table in C# Operator Overloading in C# Difference between Delegates and Events in C# Finally Block in C# Reverse a String in C# Type Casting in C# What is Generics in C# Basic Database Operations Using C# C# Anonymous Methods C# Events C# Generics C# indexers C# Sortedlist With Examples C# Stack With Examples Hashset in C# With Examples List Implementation in C# Sortedset in C# With Examples C# Queue With Examples C# Multidimensional Indexers C# Multithreading C# New Features C# Overloading of Indexers C# Unsafe Code File Handling in C# C# in Depth Loggers in C# Nullable Types in C# How to Split String in C# Authentication and Authorization in C# Await in C# Binaryreader and Binarywriter in C# C# Attributes C# Delegates Directoryinfo Class in C# Export and Import Excel Data in C# File Class in C# Fileinfo Class in C# How to Cancel Parallel Operations in C# Maximum Degree of Parallelism in C# Parallel Foreach Loop in C# Parallel Invoke in C# Streamreader and Streamwriter in C# Textreader and Textwriter in C# JSON Serialization in C# Abstract Class in C# Application of Pointer in C# How to Convert String Data Type to Int in C# Readonly And Constant in C# Type Safe in C# Types of Variables in C# Use of Delegates in C#

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