EnumerableExtensionsTryAdd(IEnumerable, Object, Boolean, Boolean) Method

Tries to add the specified item to the collection.


Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.0.0
public static bool TryAdd(
	this IEnumerable collection,
	Object? item,
	bool checkReadOnly = true,
	bool throwError = true


collection  IEnumerable
The collection to add the item to.
item  Object
The item to add.
checkReadOnly  Boolean  (Optional)
to return if the collection is read-only; to attempt adding the element without checking the read-only state. This parameter is optional.
Default value: .
throwError  Boolean  (Optional)
to forward any exception thrown by a found add method; to suppress the exceptions thrown by the found add method and return on failure. This parameter is optional.
Default value: .

Return Value

if an adding method could be successfully called; if such method was not found, or checkReadOnly is and the collection was read-only, or throwError is and the adding method threw an exception.

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


The item can be added to the collection if that is either an IList, IDictionary (when item is a DictionaryEntry instance), ICollectionT or IProducerConsumerCollectionT implementation.


If it is known that the collection implements only the supported generic interfaces, then for better performance use the generic TryAdd<T> overload if possible.

See Also