Thursday, July 1, 2021

C# Dictionary Collection


namespace ConsoleDixn

{

   public class Student

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string Gender { get; set; }

        public int Age { get; set; }

    }

}

using System;

using System.Collections.Generic;

namespace ConsoleDixn

{

    class Program

    {

        static void Main(string[] args)

        {

            Student student1 = new Student() { Id = 1, Name = "Amit", Gender = "Male", Age = 16 };

            Student student2 = new Student() { Id = 2, Name = "Rajat", Gender = "Male", Age = 17 };

            Student student3 = new Student() { Id = 3, Name = "Mohan", Gender = "Male", Age = 14 };

            Student student4 = new Student() { Id = 4, Name = "Tara", Gender = "Female", Age = 13 };

           

            Dictionary dxStudents = new Dictionary();

            dxStudents.Add(student1.Id, student1);

            dxStudents.Add(student2.Id, student2);

            dxStudents.Add(student3.Id, student3);

            dxStudents.Add(student4.Id, student4);

            //List all students of dictionary collection

            foreach (var item in dxStudents)

            {

                Console.WriteLine("Student Id:{0}",item.Key);

                Console.WriteLine("Name:{0} Gender:{1} Age:{2}", item.Value.Name, item.Value.Gender, item.Value.Age);

            }

            //Alternative: List all students of dictionary collection

            foreach (KeyValuePair kvp in dxStudents)

            {

                Console.WriteLine("Student Id:{0}", kvp.Key);

                Console.WriteLine("Name:{0} Gender:{1} Age:{2}", kvp.Value.Name, kvp.Value.Gender, kvp.Value.Age);

            }

 

            Console.WriteLine("Keys List:");

            foreach (var key in dxStudents.Keys)

            {

                Console.Write(key + " ");

            }

            Console.WriteLine();

            Console.WriteLine("Values List:");

            foreach (var item in dxStudents.Values)

            {

                Console.WriteLine("Name:{0} Gender:{1} Age:{2}", item.Name, item.Gender, item.Age);

            }

            //check  if the key already exists

            if (dxStudents.ContainsKey(2))

            {

                Console.WriteLine("Key 2 already exists.");

            }

            //check  if the key already exists

            if (dxStudents.ContainsKey(5))

            {

                Console.WriteLine("Key 5 already exists.");

            }

            else

            {

                Console.WriteLine("Key 5 does not exist.");

            }

            //get value from its key

            Student student = dxStudents[1]; // get value of key 1

            Console.WriteLine("Name:{0} Gender:{1} Age:{2}",student.Name,student.Gender,student.Age);

           

            //get value from its key will throw exception if key is not found

            //so, better use TryGetValue() method

            Student std;

            bool success = dxStudents.TryGetValue(5, out std);

            if (success)

            {

                Console.WriteLine(std.Name);

                Console.WriteLine("Key 5 found");

            }

            else

            {

                Console.WriteLine("Key 5 not found");

            }

            Console.WriteLine("Key-value pairs in student dictionary collection is {0}", dxStudents.Count);

            dxStudents.Remove(2); //key 2 removed

            Console.WriteLine("Key-value pairs in student dictionary collection is {0}", dxStudents.Count);

            dxStudents.Clear(); //removed all key values pairs

            Console.WriteLine("Key-value pairs in student dictionary collection is {0}", dxStudents.Count);

            Console.ReadKey();

        }

    }

}

OUTPUT

Student Id:1

Name:Amit Gender:Male Age:16

Student Id:2

Name:Rajat Gender:Male Age:17

Student Id:3

Name:Mohan Gender:Male Age:14

Student Id:4

Name:Tara Gender:Female Age:13

Student Id:1

Name:Amit Gender:Male Age:16

Student Id:2

Name:Rajat Gender:Male Age:17

Student Id:3

Name:Mohan Gender:Male Age:14

Student Id:4

Name:Tara Gender:Female Age:13

Keys List:

1 2 3 4

Values List:

Name:Amit Gender:Male Age:16

Name:Rajat Gender:Male Age:17

Name:Mohan Gender:Male Age:14

Name:Tara Gender:Female Age:13

Key 2 already exists.

Key 5 does not exist.

Name:Amit Gender:Male Age:16

Key 5 not found

Key-value pairs in student dictionary collection is 4

Key-value pairs in student dictionary collection is 3

Key-value pairs in student dictionary collection is 0

No comments:

Post a Comment

Hot Topics