Skip to content

Applying Extension Methods to an Interface in C# #192

Answered by christiannagel
ShervanN asked this question in Q&A
Discussion options

You must be logged in to vote

Hi Shervan,

Page 75 shows an extension method that extends the string type.
On page 230 you can see an extension method for IEnumerable<TSource>:

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate> predicate)
{
    foreach (TSource item in source)
    {
        if (predicate(item))
            yield return item;
    }
}

With this, you can invoke the Where method on every object implementing IEnumerable<T>.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ShervanN
Comment options

Answer selected by ShervanN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants