Menu Close

Category: Blog

DICTIONARY IN C#

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.


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; }
      }
    }

}

   

Share this:

CODE SNIPPETS IN VISUAL STUDIO

CODE SNIPPET TYPES

Expansion : These snippets allows the code snippet to be inserted at the cursor.

SurroundsWith: These snippets allows the code snippets to be placed around a selected piece of code

Refactoring: These snippets are used during code refactoring.

Control K X for intellesense in visual studio

right click in visual studio program and select the insert snippets > visual c# > for loop

CODE SNIPPET MANAGER

Tool > code snippet manager

Share this:

HOW TO MAKE METHOD PARAMETERS OPTIONAL IN C#

OPTIONAL PARAMETERS

FOUR WAYS

1) Use parameter arrays
2) Method overloading
3) Specify parameter defaults
4) Use OptionalAttribute that is present in System.Runtime.InteropService namespace


1)Use parameter arrays


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20 new object[] {30,50,40});
        }

        public static void AddNumber(int firstNumber, int SecondNumber, params object[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}


   

2) Making method parameters optional using Method overloading 


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20, new int[] {30,40});
        }
// over loading method
      public static void AddNumber(int firstNumber, int SecondNumber)
        {
            AddNumber(firstNumber, SecondNumber, null);
        }
       

        public static void AddNumber(int firstNumber, int SecondNumber, params int[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}

3) Specify parameter defaults


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20, new int[] {30,40});
        }

        // optional parameters appears after all the parameters
     
        public static void AddNumber(int firstNumber, int SecondNumber,  int[] restofNumbers = null)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}


   NAMED PARAMETERS

using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Test(1,c:20);
        }

        // optional parameters appears after all the parameters

        public static void Test(int a, int b = 10, int c = 20)
        {
            Console.WriteLine(“a = ” + a);
            Console.WriteLine(“b = ” + b);
            Console.WriteLine(“c = ” + c);
            Console.ReadLine();
        }

    }

}

 4)  Making Method parameters optional by using OptionalAttributes


using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20);
        }
        
        public static void AddNumber(int firstNumber, int SecondNumber,[Optional] int[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if (restofNumbers != null)
            {
                foreach (int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }
    }
}
   


Share this: