using System.Collections.Generic; /// /// A simple static class to get and set globally accessible variables through a key-value approach. /// /// /// Uses a key-value approach (dictionary) for storing and modifying variables. /// It also uses a lock to ensure consistency between the threads. /// public static class GlobalVariables { private static readonly object lockObject = new object(); private static Dictionary variablesDictionary = new Dictionary(); /// /// The underlying key-value storage (dictionary). /// /// Gets the underlying variables dictionary public static Dictionary VariablesDictionary => variablesDictionary; /// /// Retrieves all global variables. /// /// The global variables dictionary object. public static Dictionary GetAll() { return variablesDictionary; } /// /// Gets a variable and casts it to the provided type argument. /// /// The type of the variable /// The variable key /// The casted variable value public static T Get(string key) { if (variablesDictionary == null || !variablesDictionary.ContainsKey(key)) { return default(T); } return (T)variablesDictionary[key]; } /// /// Sets the variable, the existing value gets overridden. /// /// It uses a lock under the hood to ensure consistensy between threads /// The variable name/key /// The variable value public static void Set(string key, object value) { lock (lockObject) { if (variablesDictionary == null) { variablesDictionary = new Dictionary(); } variablesDictionary[key] = value; } } }