EnumerableExtensionsTryGetElementAtT(IEnumerableT, Int32, T, Boolean, Boolean) Method
Tries to get an item at the specified index in the collection.
Namespace: KGySoft.CoreLibrariesAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
public static bool TryGetElementAt<T>(
this IEnumerable<T> collection,
int index,
out T item,
bool checkBounds = true,
bool throwError = true
)
<ExtensionAttribute>
Public Shared Function TryGetElementAt(Of T) (
collection As IEnumerable(Of T),
index As Integer,
<OutAttribute> ByRef item As T,
Optional checkBounds As Boolean = true,
Optional throwError As Boolean = true
) As Boolean
public:
[ExtensionAttribute]
generic<typename T>
static bool TryGetElementAt(
IEnumerable<T>^ collection,
int index,
[OutAttribute] T% item,
bool checkBounds = true,
bool throwError = true
)
[<ExtensionAttribute>]
static member TryGetElementAt :
collection : IEnumerable<'T> *
index : int *
item : 'T byref *
?checkBounds : bool *
?throwError : bool
(* Defaults:
let _checkBounds = defaultArg checkBounds true
let _throwError = defaultArg throwError true
*)
-> bool
- collection IEnumerableT
- The collection to retrieve the item from.
- index Int32
- The zero-based index at which item should be returned.
- item T
- If this method returns , then this parameter contains the found item. This parameter is passed uninitialized.
- checkBounds Boolean (Optional)
- to return if the index is invalid; to attempt getting the element via the possible interfaces without checking bounds. This parameter is optional.
Default value: . - throwError Boolean (Optional)
- to forward any exception thrown by a found getting member; to suppress the exceptions thrown by the found getting member and return on failure. This parameter is optional.
Default value: .
- T
- The type of the elements in the collection.
Boolean if
item could be retrieved; otherwise,
.In Visual Basic and C#, you can call this method as an instance method on any object of type
IEnumerableT. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
If collection is neither an IListT, IReadOnlyListT, nor an IList implementation, then the collection will be iterated.
In this case the checkBounds argument is ignored and the method returns if the index is invalid.
This method is similar to the
Enumerable.ElementAtOrDefault method.
The main difference is that if
Enumerable.ElementAtOrDefault
returns the default value of
T, then it cannot be known whether the returned item existed in the collection at the specified position.