Junior — Middle
Can you give an example of applying a specific data structure in practice?
sobes.tech AI
Answer from AI
One of the frequently used data structures in C# practice is the Dictionary<TKey, TValue>, which implements an associative array — it stores key-value pairs and provides quick access by key.
Example of usage: storing and quickly searching for information about users by their unique identifier.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<int, string> users = new Dictionary<int, string>();
users.Add(1, "Ivan");
users.Add(2, "Maria");
int userId = 1;
if (users.TryGetValue(userId, out string userName))
{
Console.WriteLine($"User with ID {userId}: {userName}");
}
else
{
Console.WriteLine("User not found");
}
}
}
The dictionary allows efficient storage and retrieval of data by key, which is useful in many applications, for example, for caching, configuration, storing settings, etc.