1

So I am looking at the following example (http://zetcode.com/lang/csharp/arrays/), titled "c# array slices". When I copy and paste the following example in Microsoft Visual Studio:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

    namespace s_Sandbox
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] array = new int[] { 1, 2, 3, 4, 5 };
                array[1..2];
            }
        }
    }

There is a red underline under [1..2] and I get a Syntax error, "," expected ... why is this? What am I missing?

6
  • stackoverflow.com/questions/406485/array-slices-in-c-sharp/… Commented Aug 5, 2020 at 15:01
  • 1
    this is supported starting with c# 8 Commented Aug 5, 2020 at 15:02
  • @Steve that does not answer why I am getting the error; rather an alternate solution. Commented Aug 5, 2020 at 15:03
  • @Homungus I have viewed the property settings for my solution, I am using .NET Framework 4.5.2... that shouldn't be an issue though? Commented Aug 5, 2020 at 15:04
  • 1
    look here how to use c# 8.0 with asp.net project: stackoverflow.com/questions/56651472/… Commented Aug 5, 2020 at 15:06

1 Answer 1

3

That is because the range operator [n..m] is available for version C# 8.0 or greater

This might be useful too

Target framework    version C# language version default
.NET Core   3.x C# 8.0
.NET Core   2.x C# 7.3
.NET Standard   2.1 C# 8.0
.NET Standard   2.0 C# 7.3
.NET Standard   1.x C# 7.3
.NET Framework  all C# 7.3

If upgrading is not an option you can use:

      int[] array = { 1, 2, 3, 4, 5 };
      var t = array.Take(2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.