KGy SOFT

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

KGy SOFT Core Libraries Help
Tries to get an item at the specified index in the collection.

Namespace:  KGySoft.CoreLibraries
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0
Syntax

public static bool TryGetElementAt<T>(
	this IEnumerable<T> collection,
	int index,
	out T item,
	bool checkBounds = true,
	bool throwError = true
)

Parameters

collection
Type: System.Collections.GenericIEnumerableT
The collection to retrieve the item from.
index
Type: SystemInt32
The zero-based index at which item should be returned.
item
Type: T
If this method returns , then this parameter contains the found item.
checkBounds (Optional)
Type: SystemBoolean
 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 (Optional)
Type: SystemBoolean
 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

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

Note Note
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.
See Also

Reference