Dictionaries

Learn how Ice dictionaries are mapped to C#

A field, an element in a sequence, or a value in another dictionary with a dictionary type is mapped to an IDictionary<TKey, TValue>.

TKey resp. TValue is the mapped C# type for the Ice key type resp. value type. For example:

ice
dictionary<string, double> StringDoubleDict;
dictionary<int, StringDoubleDict> DictOfDict;
dictionary<string, Value> StringValueDict;
struct DictionaryExample
{
DictOfDict x;
StringValueDict y;
}

maps to:

C#
public partial record struct DictionaryExample
{
public required IDictionary<int, IDictionary<string, double>> X { get; set; }
public required IDictionary<string, IceClass?> Y { get; set; }
}

By default, when the generated code decodes a dictionary, it creates a C# Dictionary<TKey, TValue> that is transmitted to you (the application) as an IDictionary<TKey, TValue>. You can safely cast this IDictionary<TKey, TValue> to a Dictionary<TKey, TValue> after decoding.

You can override this default with the "cs:generic" metadata directive described below. This directive only changes the type that the generated code uses during decoding to fill-in the field: the C# field type itself remains an IDictionary<TKey, TValue>.

A dictionary parameter has one mapping when it's sent and a different mapping when it's received. This distinction between incoming and outgoing values makes sending dictionaries more convenient and occasionally faster.

Mapping for outgoing valuesDefault mapping for incoming values
IEnumerable<KeyValuePair<TKey, TValue>>
Dictionary<TKey, TValue>

You can override the default mapping for incoming values with the "cs:generic" metadata directive (see below). This gives you the C# type you specified for incoming values. "cs:generic" doesn't change the mapping for outgoing values.

You can use the "cs:generic" metadata directive to customize the mapping of your dictionary. This attribute accepts a single string argument: "SortedDictionary" or "SortedList".

For example:

ice
dictionary<long, Employee> EmployeeMap;

Was this page helpful?

CookiesYour privacy
This website uses cookies to analyze traffic and improve your experience.
By clicking "Accept," you consent to the use of these cookies. You can learn more about our cookies policy in our Privacy Policy.