stefan1919

Dictionary/Tutorial

Aug 31st, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // this is how to add information to dictionary
  14.             Customer customer1 = new Customer()
  15.  
  16.             {
  17.                 ID = 101,
  18.                 name = "Stefan",
  19.                 salary = 10000
  20.  
  21.  
  22.             };
  23.             Customer customer2 = new Customer()
  24.             {
  25.                 ID = 102,
  26.                 name = "Harvey",
  27.                 salary = 9000
  28.             };
  29.  
  30.  
  31.             // this is how to put the information in the dictionary
  32.             // pay attention we cannot add a value more than once
  33.             Dictionary<int, Customer> dictionaryCustomers = new Dictionary<int, Customer>();
  34.             dictionaryCustomers.Add(customer1.ID, customer1);
  35.             dictionaryCustomers.Add(customer2.ID, customer2);
  36.             //Customer customer119 = dictionaryCustomers[101];
  37.             // check for key containing
  38.  
  39.  
  40.             //if (!dictionaryCustomers.ContainsKey(customer1.ID))
  41.             //{
  42.             //    dictionaryCustomers.Add(customer1.ID, customer1);
  43.             //}
  44.  
  45.  
  46.             // this is how we could use the information
  47.             Console.WriteLine("ID-{0} name-{1} salary-{2}", customer1.ID, customer1.name, customer1.salary);
  48.  
  49.  
  50.  
  51.             // this is how to print a dictionary in a list
  52.             foreach (KeyValuePair<int, Customer> someValue in dictionaryCustomers)
  53.             {
  54.                 Console.WriteLine("Key: {0}", someValue.Key);
  55.                 Customer cust = someValue.Value;
  56.                 Console.WriteLine("ID: {0} Name: {1} Salary:{2}", cust.ID, cust.name, cust.salary);
  57.                 Console.WriteLine("--------------------------------------------------------------");
  58.             }
  59.  
  60.  
  61.             // print only the value which we need
  62.             foreach (Customer cust in dictionaryCustomers.Values)
  63.             {
  64.                 Console.WriteLine("ID: {0} Name: {1} Salary:{2}", cust.ID, cust.name, cust.salary);
  65.                 Console.WriteLine("--------------------------------------------------------------");
  66.             }
  67.  
  68.             //how to get value
  69.             Customer cast;
  70.             dictionaryCustomers.TryGetValue(101, out cast);
  71.             for (int i = 101; i < 103; i++)
  72.             {
  73.                 if (dictionaryCustomers.TryGetValue(i, out cast))
  74.                 {
  75.                     cast.salary += 100;
  76.                 }
  77.             }
  78.             foreach (KeyValuePair<int, Customer> item in dictionaryCustomers)
  79.             {
  80.                 Customer customerTest = item.Value;
  81.                 Console.WriteLine("ID: {0} Name: {1} Salary:{2}", customerTest.ID, customerTest.name, customerTest.salary);
  82.                 Console.WriteLine("###################################################################");
  83.             }
  84.            
  85.            
  86.  
  87.         }
  88.         // this is how to create a new dictionary
  89.         public class Customer
  90.         {
  91.             public int ID { get; set; }
  92.             public string name { get; set; }
  93.             public int salary { get; set; }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment