A Realm dictionary is an implementation of IDictionary<string, TValue> where the key must be a string. The value TValue can be any Realm-supported type except for collections.
To define a dictionary in your model, use a getter-only IDictionary<string, TValue> property.
Key Constraints:
- Keys must be of type
string. - Realm disallows the use of
. or $ characters in map keys. If you need to use these characters, you must use percent encoding/decoding to store them.
Nullability:
- Dictionaries of objects can contain null objects.
- Dictionaries of primitive types can contain null values if using nullable types (e.g.,
IDictionary<string, double?>). - To disallow null values, use non-nullable types (e.g.,
IDictionary<string, double>). - If you are using the older
RealmObject base class or do not have nullability enabled, use the [Required] attribute for nullable reference types like string or byte[] to ensure they are treated as required.
public partial class Inventory : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public string Id { get; set; }
// Value can be objects inheriting from RealmObject or EmbeddedObject
public IDictionary<string, Plant?> Plants { get; }
public IDictionary<string, bool> BooleansDictionary { get; }
public IDictionary<string, int?> NullableIntDictionary { get; }
public IDictionary<string, string> RequiredStringsDictionary { get; }
}