
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialThe dictionary collection represents Key-Value pairs. It is similar to an English dictionary with different words and their meanings. The dictionary collection is included in the System.Collections.Generic namespace.
The different constructors and their description is given as follows:
Constructors | Description |
|---|---|
Dictionary<TKey,TValue>() | This constructor initializes a new instance of the Dictionary<TKey,TValue> class that is empty, has the default initial capacity, and uses the default equality comparer for the key type. |
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>) | This constructor initializes a new instance of the Dictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the default equality comparer for the key type. |
Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>) | This constructor initializes a new instance of the Dictionary<TKey,TValue> class that contains elements copied from the specified IDictionary<TKey,TValue> and uses the specified IEqualityComparer<T> |
Dictionary<TKey,TValue>(IEqualityComparer<TKey>) | This constructor initializes a new instance of the Dictionary<TKey,TValue> class that is empty, has the default initial capacity, and uses the specified IEqualityComparer<T>. |
Dictionary<TKey,TValue>(Int32) | This constructor initializes a new instance of the Dictionary<TKey,TValue> class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type. |
Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>) | This constructor initializes a new instance of the Dictionary<TKey,TValue> class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer<T>. |
Dictionary<TKey,TValue>(SerializationInfo, StreamingContext) | This constructor initializes a new instance of the Dictionary<TKey,TValue> class with serialized data. |
Table: Constructors in Dictionary Collection in C#
Source: MSDN
The different properties and their description is given as follows:
Properties | Description |
|---|---|
Comparer | This property gets the IEqualityComparer<T> that is used to determine equality of keys for the dictionary. |
Count | This property gets the number of key/value pairs contained in the Dictionary<TKey,TValue>. |
Item[TKey] | This property gets or sets the value associated with the specified key |
Item[TKey] | This property gets a collection containing the keys in the Dictionary<TKey,TValue>. |
Values | This property gets a collection containing the values in the Dictionary<TKey,TValue>. |
Table: Properties in Dictionary Collection in C#
Source: MSDN
The different methods and their description is given as follows:
Methods | Description |
|---|---|
Add(TKey, TValue) | This method adds the specified key and value to the dictionary. |
Clear() | This method removes all keys and values from the Dictionary<TKey,TValue>. |
ContainsKey(TKey) | This method determines whether the Dictionary<TKey,TValue> contains the specified key. |
ContainsValue(TValue) | This method determines whether the Dictionary<TKey,TValue> contains a specific value. |
Equals(Object) | This method determines whether the specified object is equal to the current object. |
GetEnumerator() | This method returns an enumerator that iterates through the Dictionary<TKey,TValue> |
GetHashCode() | This method serves as the default hash function. |
GetObjectData(SerializationInfo, StreamingContext) | This method implements the ISerializable interface and returns the data needed to serialize the Dictionary<TKey,TValue> instance. |
GetType() | This method gets the Type of the current instance. |
MemberwiseClone() | This method creates a shallow copy of the current Object. |
OnDeserialization(Object) | This method implements the ISerializable interface and raises the deserialization event when the deserialization is complete. |
Remove(TKey) | This method removes the value with the specified key from the Dictionary<TKey,TValue>. |
ToString() | This method returns a string that represents the current object. |
TryGetValue(TKey, TValue | This method gets the value associated with the specified key. |
Table: Methods in Dictionary Collection in C#
Source: MSDN
Some of the Dictionary operations are given as follows:
Elements can be added to the dictionary using the Add() method. It adds the specified key and value pairs to the dictionary. The program that demonstrates this is given as follows:
using System;
using System.Collections.Generic;
namespace DictionaryDemo
{
class Example
{
static void Main(string[] args)
{
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1,"Harry");
d.Add(2,"Sally");
d.Add(3,"Clarke");
d.Add(4,"James");
d.Add(5,"Emma");
d.Add(6,"Susan");
Console.WriteLine("Dictionary elements are as follows:");
foreach (KeyValuePair<int, string> i in d)
{
Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value);
}
}
}
}
Source Code: Program to add elements in Dictionary in C#
The output of the above program is given below:
Dictionary elements are as follows:
Key: 1 Value: Harry
Key: 2 Value: Sally
Key: 3 Value: Clarke
Key: 4 Value: James
Key: 5 Value: Emma
Key: 6 Value: Susan
An element can be deleted from the Dictionary using the Remove() method. This method deletes the key, value pair with the specific key that is provided. The program that demonstrates this is given as follows:
using System;
using System.Collections.Generic;
namespace DictionaryDemo
{
class Example
{
static void Main(string[] args)
{
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1,"Harry");
d.Add(2,"Sally");
d.Add(3,"Clarke");
d.Add(4,"James");
d.Add(5,"Emma");
d.Add(6,"Susan");
Console.WriteLine("Original dictionary elements:");
foreach (KeyValuePair<int, string> i in d)
{
Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value);
}
d.Remove(3);
d.Remove(6);
Console.WriteLine("Dictionary elements after deletion:");
foreach (KeyValuePair<int, string> i in d)
{
Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value);
}
}
}
}
Source Code: Program to delete elements from Dictionary in C#
The output of the above program is given below:
Original dictionary elements:
Key: 1 Value: Harry
Key: 2 Value: Sally
Key: 3 Value: Clarke
Key: 4 Value: James
Key: 5 Value: Emma
Key: 6 Value: Susan
Dictionary elements after deletion:
Key: 1 Value: Harry
Key: 2 Value: Sally
Key: 4 Value: James
Key: 5 Value: Emma
The ContainsKey() method finds if a key is present in the Dictionary or not. If it is present, ContainsKey() returns TRUE and otherwise it returns FALSE. Similarly, ContainsValue() finds if a value is present in the Dictionary or not. If it is present, ContainsValue() returns TRUE and otherwise it returns FALSE.
The program that demonstrates this is given as follows:
using System;
using System.Collections.Generic;
namespace DictionaryDemo
{
class Example
{
static void Main(string[] args)
{
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1,"Harry");
d.Add(2,"Sally");
d.Add(3,"Clarke");
d.Add(4,"James");
d.Add(5,"Emma");
d.Add(6,"Susan");
Console.WriteLine("Dictionary elements:");
foreach (KeyValuePair<int, string> i in d)
{
Console.WriteLine("Key: {0} Value: {1}", i.Key, i.Value);
}
Console.WriteLine();
Console.WriteLine("The key 3 is present in Dictionary: " + d.ContainsKey(3));
Console.WriteLine("The value Lucy is present in Dictionary: " + d.ContainsValue("Lucy"));
}
}
}
Source Code: Program to find an element in Dictionary in C#
The output of the above program is given below:
Dictionary elements:
Key: 1 Value: Harry
Key: 2 Value: Sally
Key: 3 Value: Clarke
Key: 4 Value: James
Key: 5 Value: Emma
Key: 6 Value: Susan
The key 3 is present in Dictionary: True
The value Lucy is present in Linked List: False
Use the Clear() method to initialize a dictionary to an empty dictionary. Let us see an example:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Example {
public class Demo {
public static void Main(string[] args) {
var d = new Dictionary < string,string > ();
d.Clear();
if (d.Count == 0) {
Console.WriteLine("Empty...");
}
}
}
}
Source Code: Program to initialize a dictionary to an empty dictionary in C#
The above example gives the following output:
Empty...
To iterate over a C# Dictionary, you can consider using another collection i.e. List. Let us see how to implement it:
using System;
using System.Collections.Generic;
public class Example {
public static void Main() {
IDictionary < int, int > dict = new Dictionary < int, int > ();
dict.Add(1, 189);
dict.Add(2, 145);
dict.Add(3, 178);
dict.Add(4, 199);
dict.Add(5, 123);
dict.Add(6, 156);
dict.Add(7, 166);
dict.Add(8, 185);
List < int > l = new List < int > (dict.Keys);
foreach(int res in l) {
Console.WriteLine("{0}, {1}", res, dict[res]);
}
}
}
Source Code: Program to iterate over a C# Dictionary in C#
The above program gives the following output:
1, 189
2, 145
3, 178
4, 199
5, 123
6, 156
7, 166
8, 185
Let us see the C# example to get the list of keys:
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "One");
dict.Add(2, "Two");
dict.Add(3, "Three");
dict.Add(4, "Four");
dict.Add(5, "Five");
List<int> k = new List<int>(dict.Keys);
foreach (int keys in k)
{
Console.WriteLine(keys);
}
}
}
Source Code: Program to get the list of keys from a Dictionary in C#
The above program gives the following output:
1
2
3
4
5
To convert a collection into Dictionary, use the ToDictionary() method as shown in the below example:
using System;
using System.Linq;
using System.Collections.Generic;
class Example
{
static void Main()
{
string[] s = new string[] {"Chair", "Table"};
var dict = s.ToDictionary(i => i, i => true);
foreach (var res in dict)
{
Console.WriteLine("{0}, {1}", res.Key, res.Value);
}
}
}
Source Code: Program to convert a collection into Dictionary in C#
In the above example, the following is the output:
Chair, True
Table, True
To combine Dictionary, use another C# Collections i.e. HashSet. With that, implement the UnionsWith() method.
Let us see an example:
using System;
using System.Collections.Generic;
public class Example {
public static void Main() {
Dictionary < string, int > d1 = new Dictionary < string, int > ();
d1.Add("AB", 1);
d1.Add("BC", 2);
d1.Add("CD", 3);
d1.Add("EF", 4);
d1.Add("GH", 5);
d1.Add("IJ", 6);
Dictionary < string, int > d2 = new Dictionary < string, int > ();
d2.Add("KL", 7);
d2.Add("MN", 8);
d2.Add("OP", 9);
d2.Add("QR", 10);
HashSet < string > h = new HashSet < string > (d1.Keys);
h.UnionWith(d2.Keys);
foreach(string res in h) {
Console.WriteLine(res);
}
}
}
Source Code: Program to combine Dictionary of two keys in C#
The above program gives the following output:
using System;
using System.Collections.Generic;
public class Example {
public static void Main() {
Dictionary < string, int > d1 = new Dictionary < string, int > ();
d1.Add("AB", 1);
d1.Add("BC", 2);
d1.Add("CD", 3);
d1.Add("EF", 4);
d1.Add("GH", 5);
d1.Add("IJ", 6);
Dictionary < string, int > d2 = new Dictionary < string, int > ();
d2.Add("KL", 7);
d2.Add("MN", 8);
d2.Add("OP", 9);
d2.Add("QR", 10);
HashSet < string > h = new HashSet < string > (d1.Keys);
h.UnionWith(d2.Keys);
foreach(string res in h) {
Console.WriteLine(res);
}
}