C# ToDictionary Safe

Date: 2019-11-27
public Dictionary<Y, T> ToDictionarySafe<T, Y>(IEnumerable<T> items, Func<T, Y> keySelector)
{
	var dict = new Dictionary<Y, T>();
	foreach (var item in items) {
		var key = keySelector(item);
		if (key != null && !dict.ContainsKey(key)) {
			dict[key] = item;
		}
	}
	return dict;
}
29460cookie-checkC# ToDictionary Safe