KGy SOFT

EnumerableExtensionsTryGetElementAt Method (IEnumerable, Int32, Object, 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(
	this IEnumerable collection,
	int index,
	out Object item,
	bool checkBounds = true,
	bool throwError = true
)

Parameters

collection
Type: System.CollectionsIEnumerable
The collection to retrieve the item from.
index
Type: SystemInt32
The zero-based index at which item should be returned.
item
Type: SystemObject
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: .

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

Note Note
This method is similar to the Enumerable.ElementAtOrDefault method. The main difference is that if Enumerable.ElementAtOrDefault returns the default value of the element type, then it cannot be known whether the returned item existed in the collection at the specified position.
See Also

Reference