-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Dictionary.cs
More file actions
131 lines (114 loc) · 6.58 KB
/
Copy path01_Dictionary.cs
File metadata and controls
131 lines (114 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
namespace DataStructures
{
/// <summary>
/// Demonstrates the Dictionary<TKey, TValue> collection in C#.
/// A dictionary stores key-value pairs and provides O(1) average-time lookups by key.
///
/// Common use cases: phone books, configuration settings, caching, data lookups.
/// </summary>
public class DictionaryExamples
{
/// <summary>
/// Simple Employee (Personel) class used to demonstrate value-type storage in dictionaries.
/// </summary>
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; }
public string Department { get; set; }
public Employee(int id, string fullName, string department)
{
Id = id;
FullName = fullName;
Department = department;
}
public override string ToString()
{
return $"[{Id}] {FullName} — {Department}";
}
}
// ─── Entry Point ───────────────────────────────────────────────
public static void Main()
{
Console.WriteLine("═══════════════════════════════════════════════");
Console.WriteLine(" C# Dictionary<TKey, TValue> Examples ");
Console.WriteLine("═══════════════════════════════════════════════\n");
// ── 1. City Area-Code Lookup (int key → string value) ───────
// Maps international city names to their telephone area codes.
var phoneCodes = new Dictionary<int, string>
{
{ 212, "New York" },
{ 213, "Los Angeles" },
{ 312, "Chicago" },
{ 415, "San Francisco" },
{ 202, "Washington D.C." },
{ 617, "Boston" },
{ 713, "Houston" },
{ 206, "Seattle" }
};
Console.WriteLine("── City Phone Codes ──────────────────────────");
foreach (var entry in phoneCodes)
{
Console.WriteLine($" Area code {entry.Key,4} → {entry.Value}");
}
Console.WriteLine();
// ── 2. Adding & Removing Entries ────────────────────────────
Console.WriteLine("── Add / Remove Operations ───────────────────");
// Adding a new key-value pair
phoneCodes.Add(305, "Miami");
Console.WriteLine(" Added: 305 → Miami");
// Removing an entry by key
phoneCodes.Remove(213);
Console.WriteLine(" Removed: 213 (Los Angeles)");
// Safely adding with TryAdd (.NET Core 2.0+)
bool added = phoneCodes.TryAdd(305, "Miami Again"); // won't add — key exists
Console.WriteLine($" TryAdd 305 again: succeeded = {added}");
Console.WriteLine();
// ── 3. Lookup: ContainsKey & ContainsValue ──────────────────
Console.WriteLine("── Lookup Operations ─────────────────────────");
Console.WriteLine($" ContainsKey(312) : {phoneCodes.ContainsKey(312)}");
Console.WriteLine($" ContainsKey(999) : {phoneCodes.ContainsKey(999)}");
Console.WriteLine($" ContainsValue(\"Boston\"): {phoneCodes.ContainsValue("Boston")}");
Console.WriteLine($" ContainsValue(\"Paris\") : {phoneCodes.ContainsValue("Paris")}");
// TryGetValue — avoids KeyNotFoundException
if (phoneCodes.TryGetValue(202, out string city))
{
Console.WriteLine($" TryGetValue(202) : {city}");
}
Console.WriteLine();
// ── 4. Iterating with foreach ───────────────────────────────
Console.WriteLine("── Iterating All Entries ──────────────────────");
foreach (KeyValuePair<int, string> kvp in phoneCodes)
{
Console.WriteLine($" {kvp.Key,4} ↔ {kvp.Value}");
}
Console.WriteLine();
// ── 5. Dictionary with Custom Object Values ────────────────
Console.WriteLine("── Employee Directory (int → Employee) ───────");
var employees = new Dictionary<int, Employee>
{
{ 1001, new Employee(1001, "Alice Johnson", "Engineering") },
{ 1002, new Employee(1002, "Bob Smith", "Marketing") },
{ 1003, new Employee(1003, "Charlie Davis", "Engineering") },
{ 1004, new Employee(1004, "Diana Prince", "Human Resources") }
};
foreach (var emp in employees.Values)
{
Console.WriteLine($" {emp}");
}
Console.WriteLine();
// ── 6. Useful Properties ────────────────────────────────────
Console.WriteLine("── Dictionary Properties ─────────────────────");
Console.WriteLine($" Total phone codes : {phoneCodes.Count}");
Console.WriteLine($" Total employees : {employees.Count}");
Console.WriteLine($" Keys (phone codes): {string.Join(", ", phoneCodes.Keys)}");
Console.WriteLine($" Values (cities) : {string.Join(", ", phoneCodes.Values)}");
Console.WriteLine("\n═══════════════════════════════════════════════");
Console.WriteLine(" Complexity: Add O(1) | Remove O(1) | Lookup O(1)");
Console.WriteLine(" (Average case; worst case O(n) due to hash collisions)");
Console.WriteLine("═══════════════════════════════════════════════");
}
}
}