Multiple Ways to Create and initialize data Dictionary in C# Examples
Dictionary is a data structure in C#, that stores key and value pairs. This tutorial explains multiple ways to create a dictionary class in C# Example.
How to declare and initialize Dictionary object
Dictionary object created using two syntaxes.
- First, the new operator
An object is created using a new operator with Generics of key and value types.
Below accepts the key as Integer, and the value as a string.
Dictionary < int, string > employees = new Dictionary < int, string > ();
After, you can initialize the data using the Add
function using the below syntax
employees.Add(1, "one");
employees.Add(2, "four");
employees.Add(3, "two");
employees.Add(4, "three");
- Secondly, Inline Collect Initializer Literal Syntax
The below syntax allows you to create a Dictionary with initialized values, Also called Dictionary Literal initialization Syntax.
Dictionary<int, string> dict = new Dictionary<int, string>
{
{ 1, "one" },
{ 2, "two" },
{ 3, "three" },
{ 4, "four" }
};
- Third, Inline Initializer Literal assignment Syntax
Create a dictionary, add the data inline inside {}
, and value data assigned using the assignment for each key. This works from the C# 6.0
Version onwards
var map = new Dictionary < string,
string > {
["1"] = "one",
["2"] = "two",
["3"] = "three"
};
- Fourth, using Target-typed new expressions in C# 9.0 Version
Target-typed new expressions dictionary creation involves without a new Dictionary, instead using new expressions of dictionary data assignment.
This works in the C# 9.0
version.
Dictionary Initialize Inline Examples
The dictionary was created in multiple ways with all 4 syntaxes as given below
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
// π Dictionary Create using a new keyword
Dictionary < int, string > employees = new Dictionary < int, string > ();
// π add data using Add method
employees.Add(1, "one");
employees.Add(2, "four");
employees.Add(3, "two");
employees.Add(4, "three");
// π Inline Dictionary Collect Initializer Syntax
Dictionary < string, string > students = new Dictionary < string, string > {
{
"1",
"one"
},
{
"2",
"two"
},
{
"3",
"three"
},
{
"4",
"four"
}
};
// π Inline Dictionary Initializer Literal assignment Syntax Syntax in C# 6.0 Version
var map = new Dictionary < string,
string > {
["1"] = "one",
["2"] = "two",
["3"] = "three"
};
// π Target-typed new expressions in C# 9.0 Version
Dictionary < string, string > data = new() {
{
"one",
"1"
}, {
"two",
"2"
}
};
// π Iterate each object
foreach(var item in data) {
Console.WriteLine(item.Key + ": " + item.Value);
}
}
}
How to Create and Initilize a Dictionary with object classes
This example shows how to create a Dictionary of objects.
- Letβs create a class that contains id, optional name and salary, setters and getters
- Create a Dictionary object using a new keyword
- Initialise an object of a class with data
Here is an example
using System;
using System.Collections.Generic;
public class DictionaryTest {
// π Create a Class,
class Employee {
public int id {
get;
set;
}
public string? name {
get;
set;
}
public int salary {
get;
set;
}
}
public static void Main() {
// π Create a Dictionary object, with Key as primitive and Value as an object inline syntax
var employees = new Dictionary < int,
Employee > () {
{
1,
new Employee {
id = 1, name = "john", salary = 4000,
}
}, {
2,
new Employee {
id = 2, name = "franc", salary = 6000,
}
}, {
3,
new Employee {
id = 3, name = "eric", salary = 10000,
}
},
};
// π Create a Dictionary object, with Key as primitive and Value as an object inline syntax
foreach(KeyValuePair < int, Employee > item in employees) {
var emp = item.Value;
Console.WriteLine($" {emp.id} - {emp.name} - {emp.salary} ");
}
}
}
Conclusion
Multiple ways can create a dictionary and initialize data using multiple syntaxes. target type syntax expression is best used for simplicity and less code with inline expressions.