Enhanced C#
Language of your choice: library documentation
Public Member Functions | List of all members
Loyc.Collections.ITryGet< in in K, out out V > Interface Template Reference

Enables access to TryGet extension methods for retrieving items from a collection without risk of exceptions. More...


Source file:

Remarks

Enables access to TryGet extension methods for retrieving items from a collection without risk of exceptions.

Public Member Functions

TryGet (K key, out bool fail)
 Gets the item for the specified key or index, and does not throw an exception on failure. More...
 

Member Function Documentation

◆ TryGet()

V Loyc.Collections.ITryGet< in in K, out out V >.TryGet ( key,
out bool  fail 
)

Gets the item for the specified key or index, and does not throw an exception on failure.

Parameters
keyA lookup key that might be associated with a value in this object. If K is an integer, this value could be an index into a list.
failTryGet sets this to true on failure or false on success.
Returns
The element at the specified index, or default(V) if the index is not valid.

This method should never intentionally throw (e.g. don't throw if key == null) although it may use third-party methods that throw (e.g. Object.Equals()).

Ideally the return type would be Maybe<T> but that design would not allow variance on the output type (out V). Instead, an extension method TryGetExt.TryGet<K, V>(ITryGet<K, V>, K) is provided that returns Maybe<V>.