public sealed class MapField<TKey, TValue> : object, IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary, IReadOnlyDictionary<TKey, TValue>
Representation of a map field in a Protocol Buffer message.
Implements
IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary, IReadOnlyDictionary<TKey, TValue>Namespace
Google.Protobuf.CollectionsAssembly
Google.Protobuf.dll
Type Parameters | |
---|---|
Name | Description |
TKey | Key type in the map. Must be a type supported by Protocol Buffer map keys. |
TValue | Value type in the map. Must be a type supported by Protocol Buffers. |
Remarks
For string keys, the equality comparison is provided by .
Null values are not permitted in the map, either for wrapper types or regular messages. If a map is deserialized from a data stream and the value is missing from an entry, a default value is created instead. For primitive types, that is the regular default value (0, the empty string and so on); for message types, an empty instance of the message is created, as if the map entry contained a 0-length encoded value for the field.
This implementation does not generally prohibit the use of key/value types which are not supported by Protocol Buffers (e.g. using a key type of
byte
) but nor does it guarantee
that all operations will work in such cases.
The order in which entries are returned when iterating over this object is undefined, and may change in future versions.
Properties
Count
public int Count { get; }
Gets the number of elements contained in the map.
Property Value | |
---|---|
Type | Description |
Int32 |
IsReadOnly
public bool IsReadOnly { get; }
Gets a value indicating whether the map is read-only.
Property Value | |
---|---|
Type | Description |
Boolean |
Item[TKey]
public TValue this[TKey key] { get; set; }
Gets or sets the value associated with the specified key.
Parameter | |
---|---|
Name | Description |
key | TKey The key of the value to get or set. |
Property Value | |
---|---|
Type | Description |
TValue | The value associated with the specified key. If the specified key is not found, a get operation throws a , and a set operation creates a new element with the specified key. |
Keys
public ICollection<TKey> Keys { get; }
Gets a collection containing the keys in the map.
Property Value | |
---|---|
Type | Description |
ICollection<TKey> |
Values
public ICollection<TValue> Values { get; }
Gets a collection containing the values in the map.
Property Value | |
---|---|
Type | Description |
ICollection<TValue> |
Methods
Add(TKey, TValue)
public void Add(TKey key, TValue value)
Adds the specified key/value pair to the map.
Parameters | |
---|---|
Name | Description |
key | TKey The key to add |
value | TValue The value to add. |
This operation fails if the key already exists in the map. To replace an existing entry, use the indexer.
Add(IDictionary<TKey, TValue>)
public void Add(IDictionary<TKey, TValue> entries)
Adds the specified entries to the map. The keys and values are not automatically cloned.
Parameter | |
---|---|
Name | Description |
entries | IDictionary<TKey, TValue> The entries to add to the map. |
AddEntriesFrom(CodedInputStream, MapField<TKey, TValue>.Codec)
public void AddEntriesFrom(CodedInputStream input, MapField<TKey, TValue>.Codec codec)
Adds entries to the map from the given stream.
Parameters | |
---|---|
Name | Description |
input | CodedInputStream Stream to read from |
codec | MapField.Codec<> Codec describing how the key/value pairs are encoded |
It is assumed that the stream is initially positioned after the tag specified by the codec. This method will continue reading entries from the stream until the end is reached, or a different tag is encountered.
AddEntriesFrom(ref ParseContext, MapField<TKey, TValue>.Codec)
public void AddEntriesFrom(ref ParseContext ctx, MapField<TKey, TValue>.Codec codec)
Adds entries to the map from the given parse context.
Parameters | |
---|---|
Name | Description |
ctx | ParseContext Input to read from |
codec | MapField.Codec<> Codec describing how the key/value pairs are encoded |
It is assumed that the input is initially positioned after the tag specified by the codec. This method will continue reading entries from the input until the end is reached, or a different tag is encountered.
CalculateSize(MapField<TKey, TValue>.Codec)
public int CalculateSize(MapField<TKey, TValue>.Codec codec)
Calculates the size of this map based on the given entry codec.
Parameter | |
---|---|
Name | Description |
codec | MapField.Codec<> The codec to use to encode each entry. |
Returns | |
---|---|
Type | Description |
Int32 |
Clear()
public void Clear()
Removes all items from the map.
Clone()
public MapField<TKey, TValue> Clone()
Creates a deep clone of this object.
Returns | |
---|---|
Type | Description |
MapField<TKey, TValue> | A deep clone of this object. |
ContainsKey(TKey)
public bool ContainsKey(TKey key)
Determines whether the specified key is present in the map.
Parameter | |
---|---|
Name | Description |
key | TKey The key to check. |
Returns | |
---|---|
Type | Description |
Boolean |
|
Equals(MapField<TKey, TValue>)
public bool Equals(MapField<TKey, TValue> other)
Compares this map with another for equality.
Parameter | |
---|---|
Name | Description |
other | MapField<TKey, TValue> The map to compare this with. |
Returns | |
---|---|
Type | Description |
Boolean |
|
The order of the key/value pairs in the maps is not deemed significant in this comparison.
Equals(Object)
public override bool Equals(object other)
Determines whether the specified , is equal to this instance.
Parameter | |
---|---|
Name | Description |
other | Object The to compare with this instance. |
Returns | |
---|---|
Type | Description |
Boolean |
|
GetEnumerator()
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns an enumerator that iterates through the collection.
Returns | |
---|---|
Type | Description |
IEnumerator<KeyValuePair<TKey, TValue>> | An enumerator that can be used to iterate through the collection. |
GetHashCode()
public override int GetHashCode()
Returns a hash code for this instance.
Returns | |
---|---|
Type | Description |
Int32 | A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
Remove(TKey)
public bool Remove(TKey key)
Removes the entry identified by the given key from the map.
Parameter | |
---|---|
Name | Description |
key | TKey The key indicating the entry to remove from the map. |
Returns | |
---|---|
Type | Description |
Boolean |
|
ToString()
public override string ToString()
Returns a string representation of this repeated field, in the same way as it would be represented by the default JSON formatter.
Returns | |
---|---|
Type | Description |
String |
TryGetValue(TKey, out TValue)
public bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Parameters | |
---|---|
Name | Description |
key | TKey The key whose value to get. |
value | TValue When this method returns, the value associated with the specified key, if the key is found;
otherwise, the default value for the type of the |
Returns | |
---|---|
Type | Description |
Boolean |
|
WriteTo(CodedOutputStream, MapField<TKey, TValue>.Codec)
public void WriteTo(CodedOutputStream output, MapField<TKey, TValue>.Codec codec)
Writes the contents of this map to the given coded output stream, using the specified codec to encode each entry.
Parameters | |
---|---|
Name | Description |
output | CodedOutputStream The output stream to write to. |
codec | MapField.Codec<> The codec to use for each entry. |
WriteTo(ref WriteContext, MapField<TKey, TValue>.Codec)
public void WriteTo(ref WriteContext ctx, MapField<TKey, TValue>.Codec codec)
Writes the contents of this map to the given write context, using the specified codec to encode each entry.
Parameters | |
---|---|
Name | Description |
ctx | WriteContext The write context to write to. |
codec | MapField.Codec<> The codec to use for each entry. |