feat: Added ToCollection extension method for copying an enumerable to a new collection

This commit is contained in:
Chris Cameron
2020-03-23 16:35:16 -04:00
parent d07373b69e
commit 75ddc37cf1
2 changed files with 23 additions and 0 deletions

View File

@@ -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.

View File

@@ -838,6 +838,26 @@ namespace ICD.Common.Utils.Extensions
}
}
/// <summary>
/// Creates a new collection and fills it with the items of the enumerable.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TCollection"></typeparam>
/// <param name="extends"></param>
/// <returns></returns>
public static TCollection ToCollection<T, TCollection>([NotNull] this IEnumerable<T> extends)
where TCollection : ICollection<T>
{
if (extends == null)
throw new ArgumentNullException("extends");
TCollection collection = ReflectionUtils.CreateInstance<TCollection>();
foreach (T item in extends)
collection.Add(item);
return collection;
}
/// <summary>
/// Returns the sequence as a IcdHashSet.
/// </summary>