0

I'm trying to get the console to print the last word in the following string array and added the code below:

static void Main(string[] args)
{
    string[] words = new string[]
    {
        "Richard",
        "Of",
        "York",
        "Gave",
        "Battle",
        "In",
        "Vain"
    };

    Console.WriteLine($"The last word is {words[^1]}");

}

The compiler throws the error

Invalid expression term '^' for the '^' operator.

Any help will be much appreciated.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 3
    Range operators were new in C# 8 - are you definitely using that language version with your compiler/IDE? – Damien_The_Unbeliever Jun 04 '20 at 12:15
  • `Console.WriteLine($"The last word is {words[words.Length - 1]}");`; you're not using at least `C#8`[http://www.devsanon.com/c/c-8-introducing-index-struct-and-a-brand-new-usage-for-the-hat-operator/] to use this new operator. Or you may use `linq` by doing `Console.WriteLine($"The last word is {words.Last()}");` – Trevor Jun 04 '20 at 12:35
  • Does this answer your question? [Finding the last index of an array](https://stackoverflow.com/a/54735094/1797425) – Trevor Jun 04 '20 at 12:41

2 Answers2

0

Install latest .Net SDK for your platform and/or update Visual Studio Download .NET SDKs

0

Make sure to have the latest .NET Core SDK installed, and also check the settings of your project.

  • If it's a class library, chances are that it'll be a .NET Standard library. Make sure to have a .NET Standard 2.1 version specified in your project.
  • If it's another type, you should have a .NET Core project. Make sure that it's 3.0+
Sotiris Panopoulos
  • 1,523
  • 1
  • 13
  • 18