Skip to content

Commit

Permalink
try get
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Feb 13, 2024
1 parent bce7847 commit 9ba1dc2
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions CSUtilities/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSUtilities.Extensions
{
Expand Down Expand Up @@ -31,6 +29,26 @@ public static Queue<T> ToQueue<T>(this IEnumerable<T> enumerable)
return new Queue<T>(enumerable);
}

/// <summary>
/// Gets the element in an specific index or it's default value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable"></param>
/// <param name="index"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryGet<T>(this IEnumerable<T> enumerable, int index, out T result)
{
if (enumerable.Count() < index)
{
result = default(T);
return false;
}

result = enumerable.ElementAt(index);
return true;
}

public static IEnumerable<T> RemoveLastEquals<T>(this IEnumerable<T> enumerable, T element)
{
List<T> lst = new List<T>(enumerable);
Expand Down

0 comments on commit 9ba1dc2

Please sign in to comment.