EnumerableExtensionsTryGetElementAt(IEnumerable, Int32, Object, 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: 8.0.0
C#
public static bool TryGetElementAt(
	this IEnumerable collection,
	int index,
	out Object?? item,
	bool checkBounds = true,
	bool throwError = true
)

Parameters

collection  IEnumerable
The collection to retrieve the item from.
index  Int32
The zero-based index at which item should be returned.
item  Object
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: .

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 IEnumerable. 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 of objects, IReadOnlyListT of objects, 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