C# Extension Method question - IEnumerable<char> | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C# Extension Method question - IEnumerable<char>

What does it mean when I try to use an extension method for String called Reverse & it says in the tooltip: IEnumerable<char> IEnumerable<char>.Reverse<char>? Thanks.

11th Jul 2020, 9:51 AM
Scott C
Scott C - avatar
2 Answers
+ 2
(extension) Extension methods are methods that are extending an existing type, without creating a new derived type. https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.reverse?view=netcore-3.1 So the type "string" is extended with this method "Reverse". The code for the Reverse method is in System.Linq. The string class originally had no reverse method. The string class does not have to be recompiled to have this reverse method. Try remove sytem.linq from the using list and you will notice, it disappears from intellisens. IEnumerable<char> -> the source IEnumerbale<char> -> parameter <char> -> return It expects the class that is calling the method to have the IEnumerable interface implemented. Which is ok because the string type has IEnumerable implemented and C# know how to arrange the elements of a string type. https://craigwatson1962.wordpress.com/2010/10/25/linq-on-string-using-the-ienumerablechar-interface/ https://dotnettutorials.net/lesson/linq-reverse-method/ Sorry the explanation is not complete, but I hope it will explain some.
11th Jul 2020, 9:25 PM
sneeze
sneeze - avatar
+ 1
Thank you for the explanation!
12th Jul 2020, 4:01 AM
Scott C
Scott C - avatar