EnumerableExtensionsTryGetElementAtT(IEnumerableT, Int32, T, Boolean, Boolean) Method

Tries to get an item at the specified index in the collection.

Definition

Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
C#
public static bool TryGetElementAt<T>(
	this IEnumerable<T> collection,
	int index,
	out T item,
	bool checkBounds = true,
	bool throwError = true
)

Parameters

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

Type Parameters

T
The type of the elements in the collection.

Return Value

Boolean
if item could be retrieved; otherwise, .

Usage Note

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

Remarks

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.

See Also