From 75ddc37cf1b30d2c55a3384c426e1def6504d381 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 23 Mar 2020 16:35:16 -0400 Subject: [PATCH] feat: Added ToCollection extension method for copying an enumerable to a new collection --- CHANGELOG.md | 3 +++ .../Extensions/EnumerableExtensions.cs | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a1411c..33321e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + - Added ToCollection extension method for copying an enumerable to a new collection + ## [11.0.0] - 2020-03-20 ### Added - Added Not null tag for ICDUriBuilder Constructor that takes a URI as an argument. diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 2fc8418..eed86f3 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -838,6 +838,26 @@ namespace ICD.Common.Utils.Extensions } } + /// + /// Creates a new collection and fills it with the items of the enumerable. + /// + /// + /// + /// + /// + public static TCollection ToCollection([NotNull] this IEnumerable extends) + where TCollection : ICollection + { + if (extends == null) + throw new ArgumentNullException("extends"); + + TCollection collection = ReflectionUtils.CreateInstance(); + foreach (T item in extends) + collection.Add(item); + + return collection; + } + /// /// Returns the sequence as a IcdHashSet. ///