Skip to content

Instantly share code, notes, and snippets.

@treefortress
Last active February 22, 2017 08:06
Show Gist options
  • Save treefortress/de581564b86401d7e935 to your computer and use it in GitHub Desktop.
Save treefortress/de581564b86401d7e935 to your computer and use it in GitHub Desktop.
StringHash Class for C# - For easy creation of string-based Hashtables
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class StringHash<T> {
Dictionary<string, T> dictionary = new Dictionary<string, T>();
public void Set(string key, T value) {
if (HasKey(key)) {
dictionary.Remove(key);
}
dictionary.Add(key, value);
}
public bool HasKey(string key) {
return dictionary.ContainsKey(key);
}
public T Get(string key) {
if (dictionary.ContainsKey(key)) {
T val;
dictionary.TryGetValue(key, out val);
return val;
}
return default(T);
}
public T this[string key] {
get { return Get(key); }
set { Set(key, value); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment