using System.Collections.Generic;
private Switch<int> _switchStatement;
_switchStatement = new Switch<int>(GetCases(), () => Console.WriteLine("Does not equal 3, 30, or 300"));
_switchStatement.AddCase(500, () => Console.WriteLine("Equals 500"));
_switchStatement.UpdateDefaultCase(() => Console.WriteLine("Does not equal 3, 30, 300, or 500"));
private void PrintGetValue(int number)
Console.Write(number + ": ");
_switchStatement.GetValue(number)();
private Dictionary<int, Action> GetCases()
return new Dictionary<int, Action>
{ 3 , () => Console.WriteLine("Equals 3") },
{ 30 , () => Console.WriteLine("Equals 30") },
{ 300 , () => Console.WriteLine("Equals 300") }
private Dictionary<Func<int, bool>, Action> GetComplexCases()
return new Dictionary<Func<int, bool>, Action>
{ x => x < 3 , () => Console.WriteLine("Smaller than 3") },
{ x => x < 30 , () => Console.WriteLine("Smaller than 30") },
{ x => x < 300 , () => Console.WriteLine("Smaller than 300") }
private Dictionary<T, Action> Cases;
private Action DefaultCase;
public Switch(Dictionary<T, Action> cases, Action defaultCase)
this.DefaultCase = defaultCase;
public Action GetValue(T key)
if (Cases.TryGetValue(key, out keyValue))
public void AddCase(T key, Action caseValue)
if (Cases.ContainsKey(key))
Cases.Add(key, caseValue);
public void UpdateDefaultCase(Action defaultCase)
this.DefaultCase = defaultCase;