public static class Tracking
{
public static List<TrackList<T>> Lists = new List<TrackList<T>>();
public static List<TrackedDictionary<K,V>> Dictionaries =new List<TrackedDictionary<K,V>>()
public static void Register<K,V>(TrackedDictionary<K,V> dictionary)
{
Dictionaries.Add(dictionary);
}
public static void Register<T> (TrackList<T> list)
{
Lists.Add(list);
}
}
public class TrackList<T> : List<T>
{
private string ListName = null;
private int AvgSize;
public TrackList ()
{ }
public TrackList (string listname, int avgsize)
{
this.ListName = listname;
this.AvgSize = avgsize;
Tracking.Register(this);
}
public int GetListSize ()
{
return this.Count * this.AvgSize;
}
}
public class TrackedDictionary<K, V> : Dictionary<K, V>
{
public string DictionaryName = null;
public byte AvgSize;
public TrackedDictionary ()
{ }
public TrackedDictionary (string dictionaryname, byte avgsize)
{
this.DictionaryName = dictionaryname;
this.AvgSize = avgsize;
Tracking.Register(this);
}
public int GetDictionarySize ()
{
return this.Count * this.AvgSize;
}
}
public static class Tracking { public static List<TrackList<T>> Lists = new List<TrackList<T>>(); public static
Share
You haven’t declared
Trackingas a generic type, soT,KandVhave no meaning. You can do so easily enough:Note that you’ll now have separate
Lists,Dictionariesetc fields per concrete type. You’ll need to make the methods non-generic though.An alternative is to have a top-level non-generic class, with generic methods and generic nested types:
I’d also strongly advise against public fields like this… and against using statics widely to start with, given the problems they cause for testability.