Files
sbox-public/engine/Sandbox.System/Extend/DictionaryExtensions.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

35 lines
831 B
C#

namespace Sandbox;
public static partial class SandboxSystemExtensions
{
/// <summary>
/// If the key doesn't exist it is created and returned
/// </summary>
public static TValue GetOrCreate<TKey, TValue>( this IDictionary<TKey, TValue> dict, TKey key ) where TValue : new()
{
TValue val;
if ( dict.TryGetValue( key, out val ) )
return val;
val = new TValue();
dict.Add( key, val );
return val;
}
/// <summary>
/// Clones the dictionary. Doesn't clone the values.
/// </summary>
public static Dictionary<TKey, TValue> Clone<TKey, TValue>( this Dictionary<TKey, TValue> dict )
{
Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>( dict.Count, dict.Comparer );
foreach ( KeyValuePair<TKey, TValue> entry in dict )
{
ret.Add( entry.Key, (TValue)entry.Value );
}
return ret;
}
}