Collections

The Energy.Base.Collection namespace contains thread-safe, serializable, and specialized collection classes.

Array

Energy.Base.Collection.Array is a thread-safe wrapper around List<T>. It overrides the indexer, Count, Add, Remove, and other methods to synchronize access with an internal lock.

Energy.Base.Collection.Array<string> list = new Energy.Base.Collection.Array<string>();
list.Add("one");
list.Add("two");
list[0] = "first";
Console.WriteLine(list.First);
Console.WriteLine(list.Last);
Console.WriteLine(list.Count);

Associative

Energy.Base.Collection.Associative is a Dictionary<string, T> that supports XML serialization. It is useful when you need to store key/value pairs in a serializable object.

StringDictionary

Energy.Base.Collection.StringDictionary is a case-insensitive or case-sensitive string-keyed dictionary. It supports XML serialization, filtering, and a TotalLength property.

Energy.Base.Collection.StringDictionary<string> settings
    = new Energy.Base.Collection.StringDictionary<string>();

settings["Host"] = "localhost";
settings["Port"] = "3306";

Console.WriteLine(settings["host"]); // localhost (case-insensitive by default)
Console.WriteLine(settings.ToString("\n"));

StringDictionary and StringList

  • StringDictionary is a non-generic alias for StringDictionary<string>.

  • StringList is a List<string> that also implements Energy.Interface.IStringList.

SerializableDictionary<TKey, TValue>

A Dictionary<TKey, TValue> that implements IXmlSerializable.

Circular

Energy.Base.Collection.Circular is a circular buffer that implements IList<T> as well as IArray<T> and IStack<T>. It supports push, pull, limit, and first/last access.

Energy.Base.Collection.Circular<int> buffer = new Energy.Base.Collection.Circular<int>();
buffer.Push(1);
buffer.Push(2);
buffer.Push(3);
buffer.Limit = 3;

Console.WriteLine(buffer.Pull()); // 1

KeyValuePairList<TKey, TValue>

A List<KeyValuePair<TKey, TValue>> for ordered key/value pairs.

Table

The Energy.Base.Collection.Table family provides generic typed tables with rows and columns.

  • Energy.Base.Collection.Table<TKey, TValue>

  • Energy.Base.Collection.Table<TValue>

Record

Energy.Base.Record is a StringDictionary<object> alias. It represents a single row of data where column names are mapped to object values.

Energy.Base.Record record = new Energy.Base.Record();
record["Id"] = 1;
record["Name"] = "Alice";
record["Active"] = true;

Console.WriteLine(record["Name"]);

Table

Energy.Base.Table is a list of Energy.Base.Record objects. It can be converted to a System.Data.DataTable or plain text.

Energy.Base.Table table = new Energy.Base.Table();

Energy.Base.Record row1 = table.New();
row1["Name"] = "Alice";
row1["Age"] = 30;

Energy.Base.Record row2 = table.New();
row2["Name"] = "Bob";
row2["Age"] = 25;

System.Data.DataTable dataTable = table.ToDataTable();
Console.WriteLine(table.ToPlain());

Summary of classes

Class Purpose
Collection.Array<T> Thread-safe list.
Collection.Associative<T> XML-serializable dictionary.
Collection.StringDictionary<T> String-keyed dictionary with extras.
Collection.StringList List of strings.
Collection.SerializableDictionary<TKey, TValue> XML-serializable dictionary.
Collection.Circular<T> Circular buffer.
Collection.KeyValuePairList<TKey, TValue> Ordered key/value list.
Collection.Table<TKey, TValue> Generic typed table.
Record Row of string/object pairs.
Table List of records.