0

I am learning C# throught the C# via CLR book. And I come across the following piece of code (simplified to create minimal example):

using System;

public class Program 
{ 
   public static void Main(String[] args) 
   {
     string text = "Test";
     string reversed = new String(text.Reverse().ToArray());

     Console.WriteLine(reversed);
   }
}

Before trying to run the code above a compiler tells the following:

'string' does not contain a definition for 'Reverse' and no accessible extension method 'Reverse' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) [Console.NET]csharp(CS1061)

I can not wrap my head where the Reverse method come from. Was the Reverse method present in an older version of C#?

Original code is the last page of the Delegates topic (417-418 page).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
qqqqqqq
  • 1,831
  • 1
  • 18
  • 48
  • 2
    It's an extension method in the [`Enumerable` class](https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,3af306c560f8c669). It works because the `String` class implements `IEnumerable`. – 41686d6564 stands w. Palestine Feb 03 '20 at 19:31
  • @AhmedAbdelhameed, thank you for your attention. How was the author able to use the extension method without actually adding the `using`? I tried the same locally with the same `using`s and still with no success. – qqqqqqq Feb 03 '20 at 19:32
  • 4
    [`Reverse`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.reverse?view=netframework-4.8) method from system.linq, it requires `using System.Linq` – Pavel Anikhouski Feb 03 '20 at 19:33
  • 1
    @qqqqqqq Well, you do need `Using System.Linq;` for it to work, AFAIK. – 41686d6564 stands w. Palestine Feb 03 '20 at 19:33
  • @PavelAnikhouski, oh. I see it now. Thank you. I believe that it is a typo. The author just forgot to add the `using System.Linq;`. – qqqqqqq Feb 03 '20 at 19:34
  • The page @Pavel links to shows what versions of .NET the method exists in, answering your last question as well. – Heretic Monkey Feb 03 '20 at 19:36
  • The question I selected as duplicate shows all imaginable variants of "string reverse", including the one from LINQ with detailed examples and performance comparisons. If that information is not enough make sure to [edit] post to clarify. – Alexei Levenkov Feb 03 '20 at 19:42
  • @AlexeiLevenkov This definitely isn't a duplicate, OP isn't asking how to reverse a string. They're asking how the code found in some book works with the usings provided. I don't own the book so the most likely answer is the book neglected to include the appropriate using, i.e. a typo. – Derrick Moeller Feb 03 '20 at 19:58
  • @AlexeiLevenkov, actually I saw the answer you linked before posting my one. I would not expect the answer to my question to be present in the linked question, so I even did not read it. That is because I did not mention in my question that I would like to know the best way to reverse a string. – qqqqqqq Feb 03 '20 at 20:09
  • @AhmedAbdelhameed, I updated my question so that now the issue should be reproducible. – qqqqqqq Feb 03 '20 at 20:12
  • I've updated one of the [answers](https://stackoverflow.com/a/15848573/477420) in the duplicate to be more helpful... I'll let others to decide if it still not enough. – Alexei Levenkov Feb 03 '20 at 20:56
  • @AlexeiLevenkov, ok. Thank you. – qqqqqqq Feb 03 '20 at 20:58
  • And I've edited out all unrelated code/text from the question to make it true [MCVE] similar to original version of the post. If you feel it is too much of the change please edit the way you like, but make sure to show minimal code. – Alexei Levenkov Feb 03 '20 at 21:03

0 Answers0