using System.Collections.Generic;
public static void Main()
INestedKeyValueCollection collection = new NestedKeyValueCollection();
collection.Add("Name", "Sandeep");
collection.Add("Age", 35);
var address = new NestedKeyValueCollection();
address.Add("Street", "123 Main St");
address.Add("City", "Springfield");
address.Add("Zip", "12345");
collection.Add("Address", address);
Console.WriteLine($"Name: {collection["Name"]}");
Console.WriteLine($"Age: {collection["Age"]}");
if (collection.TryGetValue("Address", out object addressObj) && addressObj is INestedKeyValueCollection addr)
Console.WriteLine("Address:");
Console.WriteLine($" Street: {addr["Street"]}");
Console.WriteLine($" City: {addr["City"]}");
Console.WriteLine($" Zip: {addr["Zip"]}");
collection.Remove("Age");
Console.WriteLine($"Age after removal: {collection["Age"] ?? "Key not found"}");
public interface INestedKeyValueCollection
public void Add(string key, object value);
public bool Remove(string key);
public bool TryGetValue(string key, out object output);
public bool ContainsKey(string key);
public object this[string key] { get; set; }
public class NestedKeyValueCollection : INestedKeyValueCollection
private Dictionary<string, object> _store;
public NestedKeyValueCollection()
_store = new Dictionary<string, object>();
public void Add(string key, object value)
if (!_store.ContainsKey(key))
public bool Remove(string key)
if (_store.ContainsKey(key))
return _store.Remove(key);
public bool ContainsKey(string key)
if (_store.ContainsKey(key))
public object this[string key]
if (_store.ContainsKey(key))
public bool TryGetValue(string key, out object value)
return _store.TryGetValue(key, out value);