Applying Extension Methods to an Interface in C# #192
-
Hello, Could you please write an example for applying extension methods to an interface in C#? Which allows me to call the extension method on all the classes that implement the interface. Thank you in advance |
Beta Was this translation helpful? Give feedback.
Answered by
christiannagel
Oct 2, 2023
Replies: 1 comment 1 reply
-
Hi Shervan, Page 75 shows an extension method that extends the string type. 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 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ShervanN
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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>
:With this, you can invoke the
Where
method on every object implementingIEnumerable<T>
.