Here is a solution that will:
1. create the dictionary and load the data
2. backup (serialize) the dictionary to a string as we don't need to access it
3. remove an item
4. restores the original dictionary
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-- start --");
var dict1 = new Dictionary<int, string>
{
[0] = "ABC",
[1] = "DEF",
[2] = "GHI",
};
foreach (var item in dict1)
Console.WriteLine($"{item.Key} = {item.Value}");
Console.WriteLine("-- serialize --");
var bkup = JsonConvert.SerializeObject(dict1);
Console.WriteLine(bkup);
Console.WriteLine("--remove item --");
dict1.Remove(1);
foreach (var item in dict1)
Console.WriteLine($"{item.Key} = {item.Value}");
Console.WriteLine("--restore --");
dict1 = JsonConvert.DeserializeObject<Dictionary<int, string>>(bkup);
foreach (var item in dict1)
Console.WriteLine($"{item.Key} = {item.Value}");
Console.ReadKey();
}
}
And the output is:
-- start --
0 = ABC
1 = DEF
2 = GHI
-- serialize --
{"0":"ABC","1":"DEF","2":"GHI"}
--remove item --
0 = ABC
2 = GHI
--restore --
0 = ABC
1 = DEF
2 = GHI
For this eample I used NewtonSoft JSON library. You can learn more about this library here:
[
^]