DuckTypingCollection
Represents a collection adapter that converts between ICollection and ICollection using duck typing. Allows working with collections of different element types by providing conversion functions.
Implements: ICollection<T>, IEnumerable<T>, IEnumerable
Constructors
public DuckTypingCollection(ICollection<TSource> source, Func<TSource, TTarget> sourceToTarget, Func<TTarget, TSource> targetToSource)
duckTypingCollection = DuckTypingCollection(source, sourceToTarget, targetToSource)
Initializes a new instance of the DuckTypingCollection class.
- source
- The source collection to wrap.
- sourceToTarget
- Function to convert from source type to target type.
- targetToSource
- Function to convert from target type to source type.
Properties
public int Count { get; }
value = duckTypingCollection.Count
Gets the number of elements contained in the collection.
public bool IsReadOnly { get; }
value = duckTypingCollection.IsReadOnly
Gets a value indicating whether the collection is read-only.
public ICollection<TSource> Source { get; }
value = duckTypingCollection.Source
Gets the wrapped source collection.
Methods
public void Add(TTarget item)
duckTypingCollection.Add(item)
Adds an item to the collection.
- item
- The item to add.
public bool Contains(TTarget item)
result = duckTypingCollection.Contains(item)
Determines whether the collection contains a specific item.
- item
- The item to locate.
Returns: true if the item is found; otherwise, false.
public void CopyTo(TTarget[] array, int arrayIndex)
duckTypingCollection.CopyTo(array, arrayIndex)
Copies the elements of the collection to an array, starting at a particular array index.
- array
- The destination array.
- arrayIndex
- The zero-based index in the array at which copying begins.
public IEnumerator<TTarget> GetEnumerator()
result = duckTypingCollection.GetEnumerator()
Returns an enumerator that iterates through the collection.
Returns: An enumerator for the collection.
public bool Remove(TTarget item)
result = duckTypingCollection.Remove(item)
Removes the first occurrence of a specific item from the collection.
- item
- The item to remove.
Returns: true if the item was successfully removed; otherwise, false.
System#Collections#IEnumerable#GetEnumerator()
Returns an enumerator that iterates through the collection.
Returns: An enumerator for the collection.