You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Feel/MMTools/Tools/MMExtensions/MMDictionaryExtensions.cs

34 lines
773 B
C#

3 months ago
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace MoreMountains.Tools
{
/// <summary>
/// Dictionary extensions
/// </summary>
public static class MMDictionaryExtensions
{
/// <summary>
/// Finds a key (if there's one) that matches the value set in parameters
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="W"></typeparam>
/// <param name="dictionary"></param>
/// <param name="value"></param>
/// <returns></returns>
public static T KeyByValue<T, W>(this Dictionary<T, W> dictionary, T value)
{
T key = default;
foreach (KeyValuePair<T, W> pair in dictionary)
{
if (pair.Value.Equals(value))
{
key = pair.Key;
break;
}
}
return key;
}
}
}