Files
adas-core/adas-core.Domain/Utils/ComparableDictionary.cs
T

34 lines
1.5 KiB
C#

using System.Text;
namespace adas_core.Domain.Utils;
/// <summary>
/// Represents a comparable collection of key/value pairs that extends the standard dictionary functionality.
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionary, constrained to non-nullable types.</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionary, constrained to non-nullable types.</typeparam>
/// <remarks>
/// Inherits from <see cref="Dictionary{TKey, TValue}"/> and enforces that both keys and values are non-nullable.
/// </remarks>
/// <!-- aidoc:v1 sig=1d1f5d7 -->
public class ComparableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TKey : notnull where TValue : notnull
{
/// <summary>
/// Generates a hash code for the collection by concatenating the string representations of all key-value pairs, separated by underscores and double percent signs, and returning the hash code of the resulting string.
/// </summary>
/// <returns>An integer hash code derived from the concatenated key-value pairs of the collection.</returns>
/// <!-- aidoc:v1 sig=eb26c4a body=e3340b4 -->
public override int GetHashCode()
{
StringBuilder str = new();
foreach (var item in this)
{
str.Append(item.Key);
str.Append('_');
str.Append(item.Value);
str.Append("%%");
}
return str.ToString().GetHashCode();
}
}