1.A dictionary is a collection of(Key.value)pairs
2.Dictionary class is present in System.Collection.Generic namespace.
3.When creating s dictionary , we need to specify the type for key and value.
4.Dictionary provides fast lookups for values using keys.
5.Keys in the dictionary must be unique.
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = “Malla”,
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = “Mark”,
Salary = 5000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = “Mla”,
Salary = 5000
};
Dictionary<int, Customer> dictionaryCustomer = new Dictionary<int, Customer>();
dictionaryCustomer.Add(Customer1.ID, Customer1);
dictionaryCustomer.Add(Customer2.ID, Customer2);
dictionaryCustomer.Add(Customer3.ID, Customer3);
Customer Customer119 = dictionaryCustomer[119];
foreach(KeyValuePair<int,Customer> customerkeyValuePair in dictionaryCustomer)
{
Console.WriteLine(“Key = {0}”, customerkeyValuePair.Key);
Customer cust = customerkeyValuePair.Value;
Console.WriteLine(“ID ={0}, Name ={1},Salary = {2}”, cust.ID, cust.Name, cust.Salary);
Console.WriteLine(“———————————–“);
Console.ReadLine();
}
Console.ReadLine();
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}
=================================================================
Methods of dictionary class
1. TryGetValue()
2.Count()
3.Remove()
4.Clear()
5.Using LINQ extension methods with Dictionary
6.Different ways to convert an array into a dictionary.
=================================================================
Methods of dictionary class
1. TryGetValue()
2.Count()
3.Remove()
4.Clear()
5.Using LINQ extension methods with Dictionary
6.Different ways to convert an array into a dictionary.
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
public class MainClass
{
public static void Main()
{
Customer Customer1 = new Customer()
{
ID = 101,
Name = “Malla”,
Salary = 5000
};
Customer Customer2 = new Customer()
{
ID = 102,
Name = “Mark”,
Salary = 5000
};
Customer Customer3 = new Customer()
{
ID = 119,
Name = “Mla”,
Salary = 5000
};
Dictionary<int, Customer> dictionaryCustomer = new Dictionary<int, Customer>();
dictionaryCustomer.Add(Customer1.ID, Customer1);
dictionaryCustomer.Add(Customer2.ID, Customer2);
dictionaryCustomer.Add(Customer3.ID, Customer3);
Console.WriteLine(“Total Items = {0}”, dictionaryCustomer.Count);
Console.ReadLine();
dictionaryCustomer.Remove(110);
Console.ReadLine();
//Customer cust;
//if (dictionaryCustomer.TryGetValue(101, out cust))
//{
// Console.WriteLine(“ID = {0}, Name = {1}, Salary = {2}”, cust.ID, cust.Name, cust.Salary);
// Console.ReadLine();
//}
//else
//{
// Console.WriteLine(“The Key is not found”);
//}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
}
}