Class PropertyBag<TValue>
PropertyBag class represents a collection of key-value pairs that allows to store any object for a given unique key. All keys are represented by PropertyBagKey but values may be of any type. Null values are not permitted, since a null entry represents the absence of the key.
[Serializable]
public sealed class PropertyBag<TValue> : ISerializable, IEquatable<PropertyBag<TValue>>
Type Parameters
TValue
- Inheritance
-
PropertyBag<TValue>
- Implements
-
IEquatable<PropertyBag<TValue>>
- Inherited Members
Constructors
PropertyBag(IDictionary<PropertyBagKey, TValue>?)
Initializes a new instance of the PropertyBag class.
public PropertyBag(IDictionary<PropertyBagKey, TValue>? properties = null)
Parameters
properties
IDictionary<PropertyBagKey, TValue>An optional dictionary of properties to initialize the property bag with.
Remarks
Null values are disallowed as they signify a non-existent key. Any attempt to add a null value will result in it being disregarded.
PropertyBag(SerializationInfo, StreamingContext)
Initializes a new instance of the PropertyBag class with serialized data.
public PropertyBag(SerializationInfo info, StreamingContext context)
Parameters
info
SerializationInfoThe SerializationInfo that holds the serialized object data.
context
StreamingContextThe StreamingContext that contains contextual information about the source or destination.
Remarks
The PropertyBag class implements the ISerializable interface and can be serialized and deserialized using binary or XML formatters.
Properties
Count
Gets the number of items in the collection.
public int Count { get; }
Property Value
Keys
Gets a read-only collection that contains the keys of the collection.
public IReadOnlyCollection<PropertyBagKey> Keys { get; }
Property Value
Values
Gets a read-only collection that contains the values of the collection.
public IReadOnlyCollection<TValue> Values { get; }
Property Value
- IReadOnlyCollection<TValue>
Methods
AddOrUpdateProperties(IDictionary<PropertyBagKey, TValue>)
Adds or updates a collection of key-value pairs to the collection.
public void AddOrUpdateProperties(IDictionary<PropertyBagKey, TValue> properties)
Parameters
properties
IDictionary<PropertyBagKey, TValue>A collection of a key-value pairs, where key is represented as PropertyBagKey type.
Remarks
If a key is not found in the collection, a new key-value pair is created. If a key is already present in the collection, the value associated with the key is updated.
AddOrUpdateProperty(PropertyBagKey, TValue)
Adds a new key-value pair to the property bag or updates the value of an existing key.
public void AddOrUpdateProperty(PropertyBagKey key, TValue value)
Parameters
key
PropertyBagKeyA key represented as PropertyBagKey type.
value
TValueAny value which should be stored in a collection.
Remarks
If a key is not found in the collection, a new key-value pair is created. If a key is already present in the collection, the value associated with the key is updated. Null values are disallowed as they signify a non-existent key. Any attempt to add a null value will result in it being disregarded.
Exceptions
- ArgumentNullException
Thrown when the key is null.
Clear()
Removes all items from the collection.
public void Clear()
Clear(PropertyBagKey)
Attempts to remove the property associated with the specified key from the property bag.
public bool Clear(PropertyBagKey key)
Parameters
key
PropertyBagKeyThe key of the property to remove.
Returns
- bool
true if the property is successfully found and removed; otherwise, false.
ContainsKey(PropertyBagKey)
Determines whether the collection contains an element that has the specified key.
public bool ContainsKey(PropertyBagKey key)
Parameters
key
PropertyBagKeyA key represented as PropertyBagKey type.
Returns
- bool
Boolean value determining whether the key is found in a collection.
Equals(object?)
Determines whether the current PropertyBag<TValue> object is equal to a specified object.
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current object.
Returns
- bool
true
if the current object and obj are both PropertyBag<TValue> objects and have the same value; otherwise,false
.
Equals(PropertyBag<TValue>?)
Determines whether the current PropertyBag<TValue> object is equal to another PropertyBag<TValue> object.
public bool Equals(PropertyBag<TValue>? other)
Parameters
other
PropertyBag<TValue>The PropertyBag<TValue> object to compare with the current object.
Returns
- bool
true
if the current object and other have the same value; otherwise,false
.
GetHashCode()
Returns the hash code for the current PropertyBag<TValue> object.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer hash code.
GetProperty(PropertyBagKey)
Gets the value associated with the specified key from the collection.
public TValue GetProperty(PropertyBagKey key)
Parameters
key
PropertyBagKeyA key represented as PropertyBagKey type.
Returns
- TValue
The value associated with the specified key. If the specified key is not found, this operation throws a KeyNotFoundException exception.
Exceptions
- KeyNotFoundException
If the specified key is not found.
GetProperty<T>(PropertyBagKey)
Gets the value associated with the specified key from the collection and casts it to the specified type
T
.
public T GetProperty<T>(PropertyBagKey key) where T : TValue
Parameters
key
PropertyBagKeyA key represented as PropertyBagKey type.
Returns
- T
The value associated with the specified key. If the specified key is not found, this operation throws a KeyNotFoundException exception. If the specified key is found, however its type does not match with
T
it throws InvalidCastException.
Type Parameters
T
The value type associated with the specified key.
Exceptions
- KeyNotFoundException
If the specified key is not found.
- InvalidCastException
If the specified value type
T
does not match with a value type stored in the collection.
TryGetProperty(PropertyBagKey, out TValue?)
This method attempts to get the value associated with the specified key from the collection.
public bool TryGetProperty(PropertyBagKey key, out TValue? value)
Parameters
key
PropertyBagKeyA key represented as PropertyBagKey type.
value
TValueWhen this method returns, contains the object value stored in the collection, if the key is found, or null if the key is not found.
Returns
- bool
true if a key was found successfully; otherwise, false
TryGetProperty<T>(PropertyBagKey, out T?)
This method attempts to get the value associated with the specified key from the collection and cast it to the specified type T.
public bool TryGetProperty<T>(PropertyBagKey key, out T? value) where T : TValue
Parameters
key
PropertyBagKeyA key represented as PropertyBagKey type.
value
TWhen this method returns, contains the value of specified
T
type stored in the collection, if the key is found, or default value ofT
if the key is not found.
Returns
- bool
true if a key was found successfully and its type matches with
T
; otherwise, false
Type Parameters
T
The value type associated with the specified key.
Remarks
This method does not throw exception when type of a value associated with a given key does not match
with T
.
Operators
operator ==(PropertyBag<TValue>?, PropertyBag<TValue>?)
Determines whether two PropertyBag<TValue> objects have the same value.
public static bool operator ==(PropertyBag<TValue>? lhs, PropertyBag<TValue>? rhs)
Parameters
lhs
PropertyBag<TValue>The first PropertyBag<TValue> object to compare.
rhs
PropertyBag<TValue>The second PropertyBag<TValue> object to compare.
Returns
- bool
true
if lhs and rhs have the same value; otherwise,false
.
operator !=(PropertyBag<TValue>?, PropertyBag<TValue>?)
Determines whether two PropertyBag<TValue> objects have different values.
public static bool operator !=(PropertyBag<TValue>? lhs, PropertyBag<TValue>? rhs)
Parameters
lhs
PropertyBag<TValue>The first PropertyBag<TValue> object to compare.
rhs
PropertyBag<TValue>The second PropertyBag<TValue> object to compare.
Returns
- bool
true
if lhs and rhs have different values; otherwise,false
.