DuckTypingCollection

Ecng.Collections

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

DuckTypingCollection
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

Count
public int Count { get; }
value = duckTypingCollection.Count

Gets the number of elements contained in the collection.

IsReadOnly
public bool IsReadOnly { get; }
value = duckTypingCollection.IsReadOnly

Gets a value indicating whether the collection is read-only.

Source
public ICollection<TSource> Source { get; }
value = duckTypingCollection.Source

Gets the wrapped source collection.

Methods

Add
public void Add(TTarget item)
duckTypingCollection.Add(item)

Adds an item to the collection.

item
The item to add.
Clear
public void Clear()
duckTypingCollection.Clear()

Removes all items from the collection.

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

CopyTo
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.
GetEnumerator
public IEnumerator<TTarget> GetEnumerator()
result = duckTypingCollection.GetEnumerator()

Returns an enumerator that iterates through the collection.

Returns: An enumerator for the collection.

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