using System.Collections.Generic;
namespace CustomCollection.Tests
public static void Main() {
var stringMap = new StringMap<TestClass>();
var exception = Record.Exception(() => result = stringMap.AddElement(null, new TestClass()));
Assert.IsType<ArgumentNullException>(exception);
exception = Record.Exception(() => result = stringMap.AddElement("test", null));
Assert.IsType<ArgumentNullException>(exception);
exception = Record.Exception(() => result = stringMap.AddElement("", new TestClass()));
Assert.IsType<ArgumentException>(exception);
exception = Record.Exception(() => result = stringMap.AddElement("test", new TestClass()));
exception = Record.Exception(() => result = stringMap.AddElement("test", new TestClass()));
Console.WriteLine("All passed");
public class StringMap<TValue> : IStringMap<TValue>
private Dictionary<string, TValue> _mapper = new Dictionary<string, TValue>();
public int Count => _mapper.Count;
public TValue DefaultValue { get; set; }
public bool AddElement(string key, TValue value)
bool overrideNeeded = _mapper.ContainsKey(key);
public bool RemoveElement(string key)
if (_mapper.ContainsKey(key)) {
public TValue GetValue(string key)
if (_mapper.ContainsKey(key)) {
private void ValidateKey(string key) {
throw new ArgumentNullException("Key must not be null");
throw new ArgumentException("Key must not be empty string");
private void ValidateValue(TValue value) {
throw new ArgumentNullException("Value must not be null");
public interface IStringMap<TValue> where TValue: class
TValue DefaultValue { get; set; }
bool AddElement(string key, TValue value);
bool RemoveElement(string key);
TValue GetValue(string key);