From 6fa3cc03ad6f53775884e92819241ca5fa909aaf Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 5 Jun 2018 13:40:53 -0400 Subject: [PATCH] feat: Adding ElementAtOrDefault extension method that takes a default value --- .../Extensions/EnumerableExtensions.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 71061ed..1321284 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -174,8 +174,23 @@ namespace ICD.Common.Utils.Extensions { return false; } + } - return false; + /// + /// Gets the element at the given index. Returns the specified default value if the index does not exist. + /// + /// + /// + /// + /// + /// + public static T ElementAtOrDefault(this IEnumerable extends, int index, T defaultValue) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + T output; + return extends.TryElementAt(index, out output) ? output : defaultValue; } ///